This commit is contained in:
Besok 2019-11-02 14:45:05 +00:00
parent 7ac47b291d
commit c4c37d77e9
13 changed files with 381 additions and 0 deletions

View File

@ -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);
}

View File

@ -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
}
}

View File

@ -0,0 +1,8 @@
package com.iluwatar.saga.orchestration;
public class FlyBookingService extends Service<String> {
@Override
public String getName() {
return "booking a Fly";
}
}

View File

@ -0,0 +1,8 @@
package com.iluwatar.saga.orchestration;
public class HotelBookingService extends Service<String> {
@Override
public String getName() {
return "booking a Hotel";
}
}

View File

@ -0,0 +1,8 @@
package com.iluwatar.saga.orchestration;
public class OrderService extends Service<String> {
@Override
public String getName() {
return "init an order";
}
}

View File

@ -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;
}
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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) {
}
}

View File

@ -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);
}
}

View File

@ -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<>();
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}