diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyChapter.java b/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyChapter.java index caa377a1e..c79a77a9a 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyChapter.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyChapter.java @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.choreography; @@ -31,7 +32,7 @@ package com.iluwatar.saga.choreography; public interface ChoreographyChapter { /** - * In that case, every method is responsible to make a decision on what to do then + * In that case, every method is responsible to make a decision on what to do then. * * @param saga incoming saga * @return saga result @@ -39,6 +40,7 @@ public interface ChoreographyChapter { Saga execute(Saga saga); /** + * get name method. * @return service name. */ String getName(); diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/FlyBookingService.java b/saga/src/main/java/com/iluwatar/saga/choreography/FlyBookingService.java index 60b183be3..4a9b1e804 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/FlyBookingService.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/FlyBookingService.java @@ -20,11 +20,12 @@ * 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 + * Class representing a service to book a fly. */ public class FlyBookingService extends Service { public FlyBookingService(ServiceDiscoveryService service) { diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/HotelBookingService.java b/saga/src/main/java/com/iluwatar/saga/choreography/HotelBookingService.java index 0bbf1d3e3..ee623482e 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/HotelBookingService.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/HotelBookingService.java @@ -20,11 +20,12 @@ * 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 + * Class representing a service to book a hotel. */ public class HotelBookingService extends Service { public HotelBookingService(ServiceDiscoveryService service) { diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/OrderService.java b/saga/src/main/java/com/iluwatar/saga/choreography/OrderService.java index 3a2c002b1..19586a5a3 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/OrderService.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/OrderService.java @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.choreography; diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/Saga.java b/saga/src/main/java/com/iluwatar/saga/choreography/Saga.java index 35069b80f..b591adaf2 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/Saga.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/Saga.java @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.choreography; import java.util.ArrayList; @@ -44,7 +45,7 @@ public class Saga { } /** - * get resuzlt of saga + * get resuzlt of saga. * * @return result of saga @see {@link SagaResult} */ @@ -59,7 +60,7 @@ public class Saga { } /** - * add chapter to saga + * add chapter to saga. * @param name chapter name * @return this */ @@ -69,7 +70,7 @@ public class Saga { } /** - * set value to last chapter + * set value to last chapter. * @param value invalue * @return this */ @@ -82,7 +83,7 @@ public class Saga { } /** - * get value from current chapter + * get value from current chapter. * @return value */ public Object getCurrentValue() { @@ -90,7 +91,7 @@ public class Saga { } /** - * set value to current chapter + * set value to current chapter. * @param value to set */ public void setCurrentValue(Object value) { @@ -98,7 +99,7 @@ public class Saga { } /** - * set status for current chapter + * set status for current chapter. * @param result to set */ public void setCurrentStatus(ChapterResult result) { @@ -143,8 +144,9 @@ public class Saga { return chapters.get(pos).isSuccess(); } - /*** - * Class presents a chapter status and incoming parameters(incoming parameter transforms to outcoming parameter) + /** + * Class presents a chapter status and incoming + * parameters(incoming parameter transforms to outcoming parameter). */ public static class Chapter { private String name; @@ -170,7 +172,7 @@ public class Saga { } /** - * set result + * set result. * @param result {@link ChapterResult} */ public void setResult(ChapterResult result) { @@ -178,7 +180,7 @@ public class Saga { } /** - * the result for chapter is good + * the result for chapter is good. * @return true if is good otherwise bad */ public boolean isSuccess() { @@ -188,14 +190,14 @@ public class Saga { /** - * result for chapter + * result for chapter. */ public enum ChapterResult { INIT, SUCCESS, ROLLBACK } /** - * result for saga + * result for saga. */ public enum SagaResult { PROGRESS, FINISHED, ROLLBACKED diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/SagaApplication.java b/saga/src/main/java/com/iluwatar/saga/choreography/SagaApplication.java index 3aeb8672c..d8844c864 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/SagaApplication.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/SagaApplication.java @@ -20,25 +20,29 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package com.iluwatar.saga.choreography; +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 + * This is an analog of transaction in a database but in terms + * of microservices architecture this is executed * in a distributed environment - *

- * 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. - *

- * In this approach, there are no mediators or orchestrators services. + * + *

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. + * + *

In this approach, there are no mediators or orchestrators services. * All chapters are handled and moved by services manually. - *

- * 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) + * + *

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 @@ -47,7 +51,7 @@ public class SagaApplication { private static final Logger LOGGER = LoggerFactory.getLogger(SagaApplication.class); /** - * main method + * main method. */ public static void main(String[] args) { ServiceDiscoveryService sd = serviceDiscovery(); diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/Service.java b/saga/src/main/java/com/iluwatar/saga/choreography/Service.java index a602b1147..2e932b528 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/Service.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/Service.java @@ -20,15 +20,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.choreography; +import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.function.Supplier; /** - * Common abstraction class representing services + * Common abstraction class representing services. * implementing a general contract @see {@link ChoreographyChapter} */ public abstract class Service implements ChoreographyChapter { @@ -73,13 +74,15 @@ public abstract class Service implements ChoreographyChapter { } private Supplier serviceNotFoundException(String chServiceName) { - return () -> new RuntimeException(String.format("the service %s has not been found", chServiceName)); + return () -> new RuntimeException( + String.format("the service %s has not been found", chServiceName)); } @Override public Saga process(Saga saga) { Object inValue = saga.getCurrentValue(); - LOGGER.info("The chapter '{}' has been started. The data {} has been stored or calculated successfully", + LOGGER.info("The chapter '{}' has been started. " + + "The data {} has been stored or calculated successfully", getName(), inValue); saga.setCurrentStatus(Saga.ChapterResult.SUCCESS); saga.setCurrentValue(inValue); @@ -89,7 +92,8 @@ public abstract class Service implements ChoreographyChapter { @Override public Saga rollback(Saga saga) { Object inValue = saga.getCurrentValue(); - LOGGER.info("The Rollback for a chapter '{}' has been started. The data {} has been rollbacked successfully", + LOGGER.info("The Rollback for a chapter '{}' has been started. " + + "The data {} has been rollbacked successfully", getName(), inValue); saga.setCurrentStatus(Saga.ChapterResult.ROLLBACK); diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/ServiceDiscoveryService.java b/saga/src/main/java/com/iluwatar/saga/choreography/ServiceDiscoveryService.java index 919c99f02..a616ff4a5 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/ServiceDiscoveryService.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/ServiceDiscoveryService.java @@ -20,8 +20,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package com.iluwatar.saga.choreography; +package com.iluwatar.saga.choreography; import java.util.HashMap; import java.util.Map; @@ -35,7 +35,7 @@ public class ServiceDiscoveryService { private Map services; /** - * find any service + * find any service. * * @return any service * @throws NoSuchElementException if no elements further diff --git a/saga/src/main/java/com/iluwatar/saga/choreography/WithdrawMoneyService.java b/saga/src/main/java/com/iluwatar/saga/choreography/WithdrawMoneyService.java index b30a05e95..637e8ac1b 100644 --- a/saga/src/main/java/com/iluwatar/saga/choreography/WithdrawMoneyService.java +++ b/saga/src/main/java/com/iluwatar/saga/choreography/WithdrawMoneyService.java @@ -20,11 +20,11 @@ * 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 withdraw a money + * Class representing a service to withdraw a money. */ public class WithdrawMoneyService extends Service { diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/ChapterResult.java b/saga/src/main/java/com/iluwatar/saga/orchestration/ChapterResult.java index a8a1e7b5e..ef34ddb98 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/ChapterResult.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/ChapterResult.java @@ -20,10 +20,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; /** - * Executing result for chapter + * Executing result for chapter. * * @param incoming value */ @@ -53,7 +54,7 @@ public class ChapterResult { } /** - * state for chapter + * state for chapter. */ public enum State { SUCCESS, FAILURE 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 5dc0577bc..23b612957 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/FlyBookingService.java @@ -20,10 +20,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; /** - * Class representing a service to book a fly + * Class representing a service to book a fly. */ public class FlyBookingService extends Service { @Override 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 51d8d5aad..2d6ba1389 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/HotelBookingService.java @@ -20,10 +20,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; /** - * Class representing a service to book a hotel + * Class representing a service to book a hotel. */ public class HotelBookingService extends Service { @Override @@ -42,7 +43,8 @@ public class HotelBookingService extends Service { return ChapterResult.failure(value); } - LOGGER.info("The Rollback for a chapter '{}' has been started. The data {} has been rollbacked successfully", + LOGGER.info("The Rollback for a chapter '{}' has been started. " + + "The data {} has been rollbacked successfully", getName(), value); return super.rollback(value); diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationChapter.java b/saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationChapter.java index 861382a00..7e9e3581f 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationChapter.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationChapter.java @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; /** @@ -30,6 +31,7 @@ package com.iluwatar.saga.orchestration; public interface OrchestrationChapter { /** + * method get name. * @return service name. */ 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 32b9ce90b..c42a9d7a1 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/OrderService.java @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; /** 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 e42b06f9a..0d53362aa 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java @@ -20,6 +20,7 @@ * 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; @@ -60,14 +61,14 @@ public class Saga { } /** - * result for saga + * result for saga. */ public enum Result { FINISHED, ROLLBACK, CRASHED } /** - * class represents chapter name + * class represents chapter name. */ public static class Chapter { String 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 6d7e9fd8d..830f5e653 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java @@ -23,23 +23,27 @@ package com.iluwatar.saga.orchestration; - 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 + * 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 - *

- * 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. - *

- * In this approach, there is an orchestrator @see {@link SagaOrchestrator} + * + *

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. + * + *

In this approach, there is an orchestrator @see {@link SagaOrchestrator} * that manages all the transactions and directs * the participant services to execute local transactions based on events. * 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) + * (otherwise in choreography services very hard to prevent a saga + * if one of them has been crashed) * * @see Saga * @see SagaOrchestrator @@ -49,7 +53,7 @@ public class SagaApplication { private static final Logger LOGGER = LoggerFactory.getLogger(SagaApplication.class); /** - * method to show common saga logic + * method to show common saga logic. */ public static void main(String[] args) { SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery()); @@ -58,7 +62,8 @@ public class SagaApplication { Saga.Result badOrder = sagaOrchestrator.execute("bad_order"); Saga.Result crashedOrder = sagaOrchestrator.execute("crashed_order"); - LOGGER.info("orders: goodOrder is {}, badOrder is {},crashedOrder is {}", goodOrder, badOrder, crashedOrder); + LOGGER.info("orders: goodOrder is {}, badOrder is {},crashedOrder is {}", + goodOrder, badOrder, crashedOrder); } 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 2f25ebd46..beec37655 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java @@ -20,14 +20,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; +import static com.iluwatar.saga.orchestration.Saga.Result.CRASHED; +import static com.iluwatar.saga.orchestration.Saga.Result.FINISHED; +import static com.iluwatar.saga.orchestration.Saga.Result.ROLLBACK; + +import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Optional; - -import static com.iluwatar.saga.orchestration.Saga.Result.*; /** * The orchestrator that manages all the transactions and directs @@ -41,7 +44,7 @@ public class SagaOrchestrator { /** - * Create a new service to orchetrate sagas + * Create a new service to orchetrate sagas. * @param saga saga to process * @param sd service discovery @see {@link ServiceDiscoveryService} */ @@ -52,7 +55,7 @@ public class SagaOrchestrator { } /** - * pipeline to execute saga process/story + * pipeline to execute saga process/story. * * @param value incoming value * @param type for incoming value diff --git a/saga/src/main/java/com/iluwatar/saga/orchestration/Service.java b/saga/src/main/java/com/iluwatar/saga/orchestration/Service.java index c4ba2a9e1..d2b065201 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/Service.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/Service.java @@ -20,13 +20,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Common abstraction class representing services + * Common abstraction class representing services. * implementing a general contract @see {@link OrchestrationChapter} * * @param type of incoming param @@ -40,14 +41,16 @@ public abstract class Service implements OrchestrationChapter { @Override public ChapterResult process(K value) { - LOGGER.info("The chapter '{}' has been started. The data {} has been stored or calculated successfully", + 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", + 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/orchestration/ServiceDiscoveryService.java b/saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java index 4831730a7..dbc6e7eb5 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/ServiceDiscoveryService.java @@ -20,6 +20,7 @@ * 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; 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 f5a0c90f3..7eb5634ef 100644 --- a/saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java +++ b/saga/src/main/java/com/iluwatar/saga/orchestration/WithdrawMoneyService.java @@ -20,10 +20,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + package com.iluwatar.saga.orchestration; /** - * Class representing a service to withdraw a money + * Class representing a service to withdraw a money. */ public class WithdrawMoneyService extends Service { @Override