-
-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Besok
committed
Nov 2, 2019
1 parent
7ac47b2
commit c4c37d7
Showing
13 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
saga/src/main/java/com/iluwatar/saga/orchestration/Chapter.java
This file contains 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,35 @@ | ||
/* | ||
* 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.orchestration; | ||
|
||
/** | ||
* Chapter including into Saga | ||
*/ | ||
public interface Chapter<K> { | ||
|
||
String getName(); | ||
|
||
ChapterResult<K> process(K value); | ||
ChapterResult<K> rollback(K value); | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
saga/src/main/java/com/iluwatar/saga/orchestration/ChapterResult.java
This file contains 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,22 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
public class ChapterResult<K> { | ||
K value; | ||
State state; | ||
|
||
public ChapterResult(K value, State state) { | ||
this.value = value; | ||
this.state = state; | ||
} | ||
|
||
public static <K> ChapterResult<K> success(K val) { | ||
return new ChapterResult<>(val, State.SUCCESS); | ||
} | ||
public static <K> ChapterResult<K> failure(K val) { | ||
return new ChapterResult<>(val, State.FAILURE); | ||
} | ||
|
||
public enum State { | ||
SUCCESS, FAILURE | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java
This file contains 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,8 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
public class FlyBookingService extends Service<String> { | ||
@Override | ||
public String getName() { | ||
return "booking a Fly"; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java
This file contains 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,8 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
public class HotelBookingService extends Service<String> { | ||
@Override | ||
public String getName() { | ||
return "booking a Hotel"; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java
This file contains 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,8 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
public class OrderService extends Service<String> { | ||
@Override | ||
public String getName() { | ||
return "init an order"; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java
This file contains 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,80 @@ | ||
/* | ||
* 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.orchestration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Saga { | ||
|
||
private List<Chapter> chapters; | ||
private Result result; | ||
|
||
public Saga() { | ||
this.chapters = new ArrayList<>(); | ||
this.result = Result.INPROCESS; | ||
} | ||
|
||
public void setResult(Result result) { | ||
this.result = result; | ||
} | ||
|
||
public boolean isSuccess(){ | ||
return result == Result.FINISHED; | ||
} | ||
|
||
public boolean isFinished(){ | ||
return result != Result.INPROCESS; | ||
} | ||
|
||
public Saga chapter(String name, int timeoutInSec) { | ||
this.chapters.add(new Chapter(name, timeoutInSec)); | ||
return this; | ||
} | ||
|
||
public Chapter get(int idx) { | ||
if (chapters.size() < idx || idx < 0) { | ||
throw new SagaException("idx shoud be less then ch size or more then 0"); | ||
} | ||
return chapters.get(idx); | ||
} | ||
|
||
|
||
|
||
public static Saga create() { | ||
return new Saga(); | ||
} | ||
|
||
public enum Result{ | ||
FINISHED,CANCELED, INPROCESS; | ||
} | ||
private static class Chapter { | ||
String name; | ||
int timeoutInSec; | ||
|
||
public Chapter(String name, int timeoutInSec) { | ||
this.name = name; | ||
this.timeoutInSec = timeoutInSec; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java
This file contains 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,50 @@ | ||
/* | ||
* 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.orchestration; | ||
|
||
|
||
public class SagaApplication { | ||
public static void main(String[] args) { | ||
|
||
} | ||
|
||
|
||
|
||
private Saga createSaga() { | ||
return Saga.create() | ||
.chapter("init an order",10) | ||
.chapter("booking a Fly",10) | ||
.chapter("booking a Hotel",10) | ||
.chapter("withdrawing Money",10); | ||
} | ||
|
||
private static ServiceDiscoveryService sd() { | ||
return | ||
new ServiceDiscoveryService() | ||
.discover(new OrderService()) | ||
.discover(new FlyBookingService()) | ||
.discover(new HotelBookingService()) | ||
.discover(new WithdrawMoneyService()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
saga/src/main/java/com/iluwatar/saga/orchestration/SagaException.java
This file contains 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,29 @@ | ||
/* | ||
* 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.orchestration; | ||
|
||
public class SagaException extends RuntimeException { | ||
public SagaException(String message) { | ||
super(message); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java
This file contains 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,30 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class SagaOrchestrator { | ||
|
||
private ExecutorService executor; | ||
private AtomicBoolean isNormalFlow; | ||
private AtomicBoolean isFinished; | ||
private AtomicInteger currentState; | ||
private final Saga saga; | ||
|
||
public SagaOrchestrator(Saga saga) { | ||
this.saga = saga; | ||
this.isNormalFlow = new AtomicBoolean(true); | ||
this.isFinished = new AtomicBoolean(false); | ||
this.currentState = new AtomicInteger(0); | ||
this.executor = Executors.newFixedThreadPool(20); | ||
} | ||
|
||
public <K> Saga.Result kickOff(K value) { | ||
|
||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
saga/src/main/java/com/iluwatar/saga/orchestration/Service.java
This file contains 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,33 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.iluwatar.saga.orchestration.Utility.sleepInSec; | ||
|
||
public abstract class Service<K> implements Chapter<K> { | ||
private static final Logger logger = LoggerFactory.getLogger(Service.class); | ||
|
||
@Override | ||
public abstract String getName(); | ||
|
||
|
||
@Override | ||
public ChapterResult<K> process(K value) { | ||
logger.info("The chapter about {} is starting", getName()); | ||
logger.info("storing or calculating a data {} to db", value); | ||
sleepInSec(1); | ||
logger.info("the data {} has been stored or calculated successfully", value); | ||
return ChapterResult.success(value); | ||
} | ||
|
||
@Override | ||
public ChapterResult<K> rollback(K value) { | ||
logger.info("Something is going wrong hence The chapter about {} is starting the rollback procedure", getName()); | ||
sleepInSec(1); | ||
logger.info("the data {} has been rollbacked successfully", value); | ||
return ChapterResult.success(value); | ||
} | ||
|
||
|
||
} |
46 changes: 46 additions & 0 deletions
46
saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java
This file contains 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,46 @@ | ||
/* | ||
* 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.orchestration; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class ServiceDiscoveryService { | ||
private Map<String, Chapter<?>> services; | ||
|
||
public Optional<Chapter<?>> find(String service) { | ||
return Optional.ofNullable(services.getOrDefault(service, null)); | ||
} | ||
|
||
public ServiceDiscoveryService discover(Chapter<?> chapterService) { | ||
services.put(chapterService.getName(), chapterService); | ||
return this; | ||
} | ||
|
||
public ServiceDiscoveryService() { | ||
this.services = new HashMap<>(); | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
saga/src/main/java/com/iluwatar/saga/orchestration/Utility.java
This file contains 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,16 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
public class Utility { | ||
private Utility() { | ||
} | ||
|
||
public static void sleepInSec(int sec){ | ||
try { | ||
Thread.sleep(sec*1000); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java
This file contains 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,16 @@ | ||
package com.iluwatar.saga.orchestration; | ||
|
||
public class WithdrawMoneyService extends Service<String> { | ||
@Override | ||
public String getName() { | ||
return "withdrawing Money"; | ||
} | ||
|
||
@Override | ||
public ChapterResult<String> process(String value) { | ||
if(value.equals("no-money")){ | ||
return ChapterResult.failure(value); | ||
} | ||
return super.process(value); | ||
} | ||
} |