-
-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Saga pattern #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Saga pattern #1062
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a204383
init repo for role object
c365610
add to init
f09a7eb
add to init
20b4195
add first impl
630532c
add pattern
9403d53
add license
e382794
add changes
768e647
add saga init dsc
564cf12
add init saga dsc
7ac47b2
add changes to dsc
c4c37d7
add
3a4677c
add orchestrator
1b32e23
add ch
ffeee2f
separate pkgs
fbcfeb0
add info
09b4663
add choreogr
8f85353
merge changes
74c7273
rem space
de56cbb
change according to cgeckstyle
87af122
add changes according to google style
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add choreogr
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
saga/src/main/java/com/iluwatar/saga/choreography/FlyBookingService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * The MIT License | ||
| * Copyright © 2014-2019 Ilkka Seppälä | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.iluwatar.saga.choreography; | ||
|
|
||
|
|
||
| /** | ||
| * Class representing a service to book a fly | ||
| */ | ||
| public class FlyBookingService extends Service { | ||
| public FlyBookingService(ServiceDiscoveryService service) { | ||
| super(service); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "booking a Fly"; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
saga/src/main/java/com/iluwatar/saga/choreography/HotelBookingService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * The MIT License | ||
| * Copyright © 2014-2019 Ilkka Seppälä | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.iluwatar.saga.choreography; | ||
|
|
||
|
|
||
| /** | ||
| * Class representing a service to book a hotel | ||
| */ | ||
| public class HotelBookingService extends Service { | ||
| public HotelBookingService(ServiceDiscoveryService service) { | ||
| super(service); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "booking a Hotel"; | ||
| } | ||
|
|
||
|
|
||
| } |
39 changes: 39 additions & 0 deletions
39
saga/src/main/java/com/iluwatar/saga/choreography/OrderService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * The MIT License | ||
| * Copyright © 2014-2019 Ilkka Seppälä | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.iluwatar.saga.choreography; | ||
|
|
||
|
|
||
| /** | ||
| * Class representing a service to init a new order. | ||
| */ | ||
| public class OrderService extends Service{ | ||
|
|
||
| public OrderService(ServiceDiscoveryService service) { | ||
| super(service); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "init an order"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
saga/src/main/java/com/iluwatar/saga/choreography/SagaApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * The MIT License | ||
| * Copyright © 2014-2019 Ilkka Seppälä | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.iluwatar.saga.choreography; | ||
|
|
||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * This pattern is used in distributed services to perform a group of operations atomically. | ||
| * This is an analog of transaction in a database but in terms of microservices architecture this is executed | ||
| * in a distributed environment | ||
| * <p> | ||
| * A saga is a sequence of local transactions in a certain context. If one transaction fails for some reason, | ||
| * the saga executes compensating transactions(rollbacks) to undo the impact of the preceding transactions. | ||
| * <p> | ||
| * In this approach, there are no mediators or orchestrators services. | ||
| * All chapters are handled and moved by services manually. | ||
| * <p> | ||
| * The major difference with choreography saga is an ability to handle crashed services | ||
| * (otherwise in choreography services very hard to prevent a saga if one of them has been crashed) | ||
| * | ||
| * @see com.iluwatar.saga.choreography.Saga | ||
| * @see Service | ||
| */ | ||
| public class SagaApplication { | ||
| private static final Logger logger = LoggerFactory.getLogger(SagaApplication.class); | ||
|
|
||
| public static void main(String[] args) { | ||
| ServiceDiscoveryService sd = serviceDiscovery(); | ||
iluwatar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Chapter service = sd.findAny(); | ||
| Saga goodOrderSaga = service.execute(newSaga("good_order")); | ||
| Saga badOrderSaga = service.execute(newSaga("bad_order")); | ||
| logger.info("orders: goodOrder is {}, badOrder is {}", | ||
| goodOrderSaga.getResult(), badOrderSaga.getResult()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| private static Saga newSaga(Object value) { | ||
| return Saga | ||
| .create() | ||
| .chapter("init an order").setInValue(value) | ||
| .chapter("booking a Fly") | ||
| .chapter("booking a Hotel") | ||
| .chapter("withdrawing Money"); | ||
| } | ||
|
|
||
| private static ServiceDiscoveryService serviceDiscovery() { | ||
| ServiceDiscoveryService sd = new ServiceDiscoveryService(); | ||
| return sd | ||
| .discover(new OrderService(sd)) | ||
| .discover(new FlyBookingService(sd)) | ||
| .discover(new HotelBookingService(sd)) | ||
| .discover(new WithdrawMoneyService(sd)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.