diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java b/saga/src/main/java/com/iluwatar/saga/ServiceDiscoveryService.java similarity index 95% rename from saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java rename to saga/src/main/java/com/iluwatar/saga/ServiceDiscoveryService.java index 652740051..f9d91b53e 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java +++ b/saga/src/main/java/com/iluwatar/saga/ServiceDiscoveryService.java @@ -20,7 +20,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package com.iluwatar.saga.orchestration; +package com.iluwatar.saga; + +import com.iluwatar.saga.orchestration.Chapter; import java.util.HashMap; import java.util.Map; diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/Chapter.java b/saga/src/main/java/com/iluwatar/saga/choreography/Chapter.java new file mode 100644 index 000000000..a76e2c4df --- /dev/null +++ b/saga/src/main/java/com/iluwatar/saga/choreography/Chapter.java @@ -0,0 +1,59 @@ +/* + * 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 com.iluwatar.saga.orchestration.ChapterResult; + +/** + * Chapter is an interface representing a contract for an external service. + * @param is type for passing params + */ +public interface Chapter { + + /** + * @return service name. + */ + String getName(); + + /** + * every chapter is responsible for a part of saga. + * @param value incoming value + * @return saga result @see {@link RichSaga} + */ + RichSaga execute(K value); + + /** + * The operation executed in general case. + * @param value incoming value + * @return result {@link ChapterResult} + */ + ChapterResult process(K value); + + /** + * The operation executed in rollback case. + * @param value incoming value + * @return result {@link ChapterResult} + */ + ChapterResult rollback(K value); + +} diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyService.java b/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyService.java new file mode 100644 index 000000000..e88a131b2 --- /dev/null +++ b/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyService.java @@ -0,0 +1,33 @@ +package com.iluwatar.saga.choreography; + +import com.iluwatar.saga.ServiceDiscoveryService; +import com.iluwatar.saga.orchestration.Chapter; +import com.iluwatar.saga.orchestration.ChapterResult; +import com.iluwatar.saga.orchestration.OrchestrationService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class ChoreographyService implements Chapter { + + protected static final Logger logger = LoggerFactory.getLogger(OrchestrationService.class); + + private final ServiceDiscoveryService service; + protected ChoreographyService(ServiceDiscoveryService service) { + this.service = service; + } + + @Override + public ChapterResult process(K value) { + logger.info("The chapter '{}' has been started. The data {} has been stored or calculated successfully", + getName(),value); + return ChapterResult.success(value); + } + + @Override + public ChapterResult rollback(K value) { + logger.info("The Rollback for a chapter '{}' has been started. The data {} has been rollbacked successfully", + getName(),value); + return ChapterResult.success(value); + } + +} diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/RichSaga.java b/saga/src/main/java/com/iluwatar/saga/choreography/RichSaga.java new file mode 100644 index 000000000..27e099fc7 --- /dev/null +++ b/saga/src/main/java/com/iluwatar/saga/choreography/RichSaga.java @@ -0,0 +1,4 @@ +package com.iluwatar.saga.choreography; + +public class RichSaga { +} diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java b/saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java index 6cb8479c0..34f3f4f18 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java @@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration; /** * Class representing a service to book a fly */ -public class FlyBookingService extends Service { +public class FlyBookingService extends OrchestrationService { @Override public String getName() { return "booking a Fly"; diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java b/saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java index e21046328..a4da64d36 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java @@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration; /** * Class representing a service to book a hotel */ -public class HotelBookingService extends Service { +public class HotelBookingService extends OrchestrationService { @Override public String getName() { return "booking a Hotel"; diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/Service.java b/saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationService.java similarity index 95% rename from saga/src/main/java/com/iluwatar/saga/orchestration/Service.java rename to saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationService.java index 20b34f55a..7f0e9a03c 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/Service.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationService.java @@ -30,8 +30,8 @@ import org.slf4j.LoggerFactory; * implementing a general contract @see {@link Chapter} * @param type of incoming param */ -public abstract class Service implements Chapter { - protected static final Logger logger = LoggerFactory.getLogger(Service.class); +public abstract class OrchestrationService implements Chapter { + protected static final Logger logger = LoggerFactory.getLogger(OrchestrationService.class); @Override public abstract String getName(); diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java b/saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java index 6edd94ace..8fb7b118d 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java @@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration; /** * Class representing a service to init a new order. */ -public class OrderService extends Service { +public class OrderService extends OrchestrationService { @Override public String getName() { return "init an order"; diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java b/saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java index c21435f20..9f8312d9f 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java @@ -70,5 +70,8 @@ public class Saga { this.name = name; } + public String getName() { + return name; + } } } diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java b/saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java index 71db1a7bf..b69d04013 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java @@ -24,6 +24,7 @@ package com.iluwatar.saga.orchestration; +import com.iluwatar.saga.ServiceDiscoveryService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +41,7 @@ import org.slf4j.LoggerFactory; * * @see Saga * @see SagaOrchestrator - * @see Service + * @see OrchestrationService */ public class SagaApplication { private static final Logger logger = LoggerFactory.getLogger(SagaApplication.class); diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java b/saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java index 34ad29b26..588a860aa 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java @@ -22,6 +22,7 @@ */ package com.iluwatar.saga.orchestration; +import com.iluwatar.saga.ServiceDiscoveryService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java b/saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java index dad15cec3..d53f106cb 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java @@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration; /** * Class representing a service to withdraw a money */ -public class WithdrawMoneyService extends Service { +public class WithdrawMoneyService extends OrchestrationService { @Override public String getName() { return "withdrawing Money"; diff --git a/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorInternallyTest.java b/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorInternallyTest.java index b8377cebf..a40a3ebec 100644 --- a/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorInternallyTest.java +++ b/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorInternallyTest.java @@ -1,5 +1,6 @@ package com.iluwatar.saga.orchestration; +import com.iluwatar.saga.ServiceDiscoveryService; import org.junit.Assert; import org.junit.Test; @@ -38,7 +39,7 @@ public class SagaOrchestratorInternallyTest { .discover(new Service4()); } - class Service1 extends Service { + class Service1 extends OrchestrationService { @Override public String getName() { @@ -58,7 +59,7 @@ public class SagaOrchestratorInternallyTest { } } - class Service2 extends Service { + class Service2 extends OrchestrationService { @Override public String getName() { @@ -77,7 +78,7 @@ public class SagaOrchestratorInternallyTest { } } - class Service3 extends Service { + class Service3 extends OrchestrationService { @Override public String getName() { @@ -96,7 +97,7 @@ public class SagaOrchestratorInternallyTest { } } - class Service4 extends Service { + class Service4 extends OrchestrationService { @Override public String getName() { diff --git a/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorTest.java b/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorTest.java index 8558b1f5a..cd659601e 100644 --- a/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorTest.java +++ b/saga/src/test/java/com/iluwatar/saga/orchestration/SagaOrchestratorTest.java @@ -1,10 +1,9 @@ package com.iluwatar.saga.orchestration; +import com.iluwatar.saga.ServiceDiscoveryService; import org.junit.Assert; import org.junit.Test; -import static org.junit.Assert.*; - public class SagaOrchestratorTest { @Test