add changes according to google style
This commit is contained in:
		@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					package com.iluwatar.saga.choreography;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -31,7 +32,7 @@ package com.iluwatar.saga.choreography;
 | 
				
			|||||||
public interface ChoreographyChapter {
 | 
					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
 | 
					   * @param saga incoming saga
 | 
				
			||||||
   * @return saga result
 | 
					   * @return saga result
 | 
				
			||||||
@@ -39,6 +40,7 @@ public interface ChoreographyChapter {
 | 
				
			|||||||
  Saga execute(Saga saga);
 | 
					  Saga execute(Saga saga);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * get name method.
 | 
				
			||||||
   * @return service name.
 | 
					   * @return service name.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  String getName();
 | 
					  String getName();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,11 +20,12 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					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 class FlyBookingService extends Service {
 | 
				
			||||||
  public FlyBookingService(ServiceDiscoveryService service) {
 | 
					  public FlyBookingService(ServiceDiscoveryService service) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,11 +20,12 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					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 class HotelBookingService extends Service {
 | 
				
			||||||
  public HotelBookingService(ServiceDiscoveryService service) {
 | 
					  public HotelBookingService(ServiceDiscoveryService service) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					package com.iluwatar.saga.choreography;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					package com.iluwatar.saga.choreography;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					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}
 | 
					   * @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
 | 
					   * @param name chapter name
 | 
				
			||||||
   * @return this
 | 
					   * @return this
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
@@ -69,7 +70,7 @@ public class Saga {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * set value to last chapter
 | 
					   * set value to last chapter.
 | 
				
			||||||
   * @param value invalue
 | 
					   * @param value invalue
 | 
				
			||||||
   * @return this
 | 
					   * @return this
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
@@ -82,7 +83,7 @@ public class Saga {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * get value from current chapter
 | 
					   * get value from current chapter.
 | 
				
			||||||
   * @return value
 | 
					   * @return value
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public Object getCurrentValue() {
 | 
					  public Object getCurrentValue() {
 | 
				
			||||||
@@ -90,7 +91,7 @@ public class Saga {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * set value to current chapter
 | 
					   * set value to current chapter.
 | 
				
			||||||
   * @param value to set
 | 
					   * @param value to set
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public void setCurrentValue(Object value) {
 | 
					  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
 | 
					   * @param result to set
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public void setCurrentStatus(ChapterResult result) {
 | 
					  public void setCurrentStatus(ChapterResult result) {
 | 
				
			||||||
@@ -143,8 +144,9 @@ public class Saga {
 | 
				
			|||||||
    return chapters.get(pos).isSuccess();
 | 
					    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 {
 | 
					  public static class Chapter {
 | 
				
			||||||
    private String name;
 | 
					    private String name;
 | 
				
			||||||
@@ -170,7 +172,7 @@ public class Saga {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * set result
 | 
					     * set result.
 | 
				
			||||||
     * @param result {@link ChapterResult}
 | 
					     * @param result {@link ChapterResult}
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public void setResult(ChapterResult result) {
 | 
					    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
 | 
					     * @return true if is good otherwise bad
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public boolean isSuccess() {
 | 
					    public boolean isSuccess() {
 | 
				
			||||||
@@ -188,14 +190,14 @@ public class Saga {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * result for chapter
 | 
					   * result for chapter.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public enum ChapterResult {
 | 
					  public enum ChapterResult {
 | 
				
			||||||
    INIT, SUCCESS, ROLLBACK
 | 
					    INIT, SUCCESS, ROLLBACK
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * result for saga
 | 
					   * result for saga.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public enum SagaResult {
 | 
					  public enum SagaResult {
 | 
				
			||||||
    PROGRESS, FINISHED, ROLLBACKED
 | 
					    PROGRESS, FINISHED, ROLLBACKED
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,25 +20,29 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package com.iluwatar.saga.choreography;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This pattern is used in distributed services to perform a group of operations atomically.
 | 
					 * 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
 | 
					 * in a distributed environment
 | 
				
			||||||
 * <p>
 | 
					 *
 | 
				
			||||||
 * A saga is a sequence of local transactions in a certain context. If one transaction fails for some reason,
 | 
					 * <p>A saga is a sequence of local transactions in a certain context.
 | 
				
			||||||
 * the saga executes compensating transactions(rollbacks) to undo the impact of the preceding transactions.
 | 
					 * If one transaction fails for some reason,
 | 
				
			||||||
 * <p>
 | 
					 * the saga executes compensating transactions(rollbacks)
 | 
				
			||||||
 * In this approach, there are no mediators or orchestrators services.
 | 
					 * 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.
 | 
					 * All chapters are handled and moved by services manually.
 | 
				
			||||||
 * <p>
 | 
					 *
 | 
				
			||||||
 * The major difference with choreography saga is an ability to handle crashed services
 | 
					 * <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)
 | 
					 * (otherwise in choreography services very hard to prevent a saga
 | 
				
			||||||
 | 
					 * if one of them has been crashed)
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @see com.iluwatar.saga.choreography.Saga
 | 
					 * @see com.iluwatar.saga.choreography.Saga
 | 
				
			||||||
 * @see Service
 | 
					 * @see Service
 | 
				
			||||||
@@ -47,7 +51,7 @@ public class SagaApplication {
 | 
				
			|||||||
  private static final Logger LOGGER = LoggerFactory.getLogger(SagaApplication.class);
 | 
					  private static final Logger LOGGER = LoggerFactory.getLogger(SagaApplication.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * main method
 | 
					   * main method.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public static void main(String[] args) {
 | 
					  public static void main(String[] args) {
 | 
				
			||||||
    ServiceDiscoveryService sd = serviceDiscovery();
 | 
					    ServiceDiscoveryService sd = serviceDiscovery();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,15 +20,16 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					package com.iluwatar.saga.choreography;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.function.Supplier;
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					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}
 | 
					 * implementing a general contract @see {@link ChoreographyChapter}
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public abstract class Service implements ChoreographyChapter {
 | 
					public abstract class Service implements ChoreographyChapter {
 | 
				
			||||||
@@ -73,13 +74,15 @@ public abstract class Service implements ChoreographyChapter {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private Supplier<RuntimeException> serviceNotFoundException(String chServiceName) {
 | 
					  private Supplier<RuntimeException> 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
 | 
					  @Override
 | 
				
			||||||
  public Saga process(Saga saga) {
 | 
					  public Saga process(Saga saga) {
 | 
				
			||||||
    Object inValue = saga.getCurrentValue();
 | 
					    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);
 | 
					        getName(), inValue);
 | 
				
			||||||
    saga.setCurrentStatus(Saga.ChapterResult.SUCCESS);
 | 
					    saga.setCurrentStatus(Saga.ChapterResult.SUCCESS);
 | 
				
			||||||
    saga.setCurrentValue(inValue);
 | 
					    saga.setCurrentValue(inValue);
 | 
				
			||||||
@@ -89,7 +92,8 @@ public abstract class Service implements ChoreographyChapter {
 | 
				
			|||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  public Saga rollback(Saga saga) {
 | 
					  public Saga rollback(Saga saga) {
 | 
				
			||||||
    Object inValue = saga.getCurrentValue();
 | 
					    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);
 | 
					        getName(), inValue);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    saga.setCurrentStatus(Saga.ChapterResult.ROLLBACK);
 | 
					    saga.setCurrentStatus(Saga.ChapterResult.ROLLBACK);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,8 +20,8 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package com.iluwatar.saga.choreography;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
@@ -35,7 +35,7 @@ public class ServiceDiscoveryService {
 | 
				
			|||||||
  private Map<String, ChoreographyChapter> services;
 | 
					  private Map<String, ChoreographyChapter> services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * find any service
 | 
					   * find any service.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @return any service
 | 
					   * @return any service
 | 
				
			||||||
   * @throws NoSuchElementException if no elements further
 | 
					   * @throws NoSuchElementException if no elements further
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,11 +20,11 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.choreography;
 | 
					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 {
 | 
					public class WithdrawMoneyService extends Service {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,10 +20,11 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Executing result for chapter
 | 
					 * Executing result for chapter.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param <K> incoming value
 | 
					 * @param <K> incoming value
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@@ -53,7 +54,7 @@ public class ChapterResult<K> {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * state for chapter
 | 
					   * state for chapter.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public enum State {
 | 
					  public enum State {
 | 
				
			||||||
    SUCCESS, FAILURE
 | 
					    SUCCESS, FAILURE
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,10 +20,11 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					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<String> {
 | 
					public class FlyBookingService extends Service<String> {
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,10 +20,11 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					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<String> {
 | 
					public class HotelBookingService extends Service<String> {
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
@@ -42,7 +43,8 @@ public class HotelBookingService extends Service<String> {
 | 
				
			|||||||
      return ChapterResult.failure(value);
 | 
					      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);
 | 
					        getName(), value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return super.rollback(value);
 | 
					    return super.rollback(value);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -30,6 +31,7 @@ package com.iluwatar.saga.orchestration;
 | 
				
			|||||||
public interface OrchestrationChapter<K> {
 | 
					public interface OrchestrationChapter<K> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * method get name.
 | 
				
			||||||
   * @return service name.
 | 
					   * @return service name.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  String getName();
 | 
					  String getName();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
@@ -60,14 +61,14 @@ public class Saga {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * result for saga
 | 
					   * result for saga.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public enum Result {
 | 
					  public enum Result {
 | 
				
			||||||
    FINISHED, ROLLBACK, CRASHED
 | 
					    FINISHED, ROLLBACK, CRASHED
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * class represents chapter name
 | 
					   * class represents chapter name.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public static class Chapter {
 | 
					  public static class Chapter {
 | 
				
			||||||
    String name;
 | 
					    String name;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,23 +23,27 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This pattern is used in distributed services to perform a group of operations atomically.
 | 
					 * This pattern is used in distributed services to perform
 | 
				
			||||||
 * This is an analog of transaction in a database but in terms of microservices architecture this is executed
 | 
					 * 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
 | 
					 * in a distributed environment
 | 
				
			||||||
 * <p>
 | 
					 *
 | 
				
			||||||
 * A saga is a sequence of local transactions in a certain context. If one transaction fails for some reason,
 | 
					 * <p>A saga is a sequence of local transactions in a certain context.
 | 
				
			||||||
 * the saga executes compensating transactions(rollbacks) to undo the impact of the preceding transactions.
 | 
					 * If one transaction fails for some reason,
 | 
				
			||||||
 * <p>
 | 
					 * the saga executes compensating transactions(rollbacks)
 | 
				
			||||||
 * In this approach, there is an orchestrator @see {@link SagaOrchestrator}
 | 
					 * to undo the impact of the preceding transactions.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * <p>In this approach, there is an orchestrator @see {@link SagaOrchestrator}
 | 
				
			||||||
 * that manages all the transactions and directs
 | 
					 * that manages all the transactions and directs
 | 
				
			||||||
 * the participant services to execute local transactions based on events.
 | 
					 * the participant services to execute local transactions based on events.
 | 
				
			||||||
 * The major difference with choreography saga is an ability to handle crashed services
 | 
					 * 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 Saga
 | 
				
			||||||
 * @see SagaOrchestrator
 | 
					 * @see SagaOrchestrator
 | 
				
			||||||
@@ -49,7 +53,7 @@ public class SagaApplication {
 | 
				
			|||||||
  private static final Logger LOGGER = LoggerFactory.getLogger(SagaApplication.class);
 | 
					  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) {
 | 
					  public static void main(String[] args) {
 | 
				
			||||||
    SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
 | 
					    SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
 | 
				
			||||||
@@ -58,7 +62,8 @@ public class SagaApplication {
 | 
				
			|||||||
    Saga.Result badOrder = sagaOrchestrator.execute("bad_order");
 | 
					    Saga.Result badOrder = sagaOrchestrator.execute("bad_order");
 | 
				
			||||||
    Saga.Result crashedOrder = sagaOrchestrator.execute("crashed_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);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,14 +20,17 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					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.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					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
 | 
					 * 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 saga saga to process
 | 
				
			||||||
   * @param sd service discovery @see {@link ServiceDiscoveryService}
 | 
					   * @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 value incoming value
 | 
				
			||||||
   * @param <K>   type for incoming value
 | 
					   * @param <K>   type for incoming value
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,13 +20,14 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Common abstraction class representing services
 | 
					 * Common abstraction class representing services.
 | 
				
			||||||
 * implementing a general contract @see {@link OrchestrationChapter}
 | 
					 * implementing a general contract @see {@link OrchestrationChapter}
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param <K> type of incoming param
 | 
					 * @param <K> type of incoming param
 | 
				
			||||||
@@ -40,14 +41,16 @@ public abstract class Service<K> implements OrchestrationChapter<K> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  public ChapterResult<K> process(K value) {
 | 
					  public ChapterResult<K> 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);
 | 
					        getName(), value);
 | 
				
			||||||
    return ChapterResult.success(value);
 | 
					    return ChapterResult.success(value);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  public ChapterResult<K> rollback(K value) {
 | 
					  public ChapterResult<K> 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);
 | 
					        getName(), value);
 | 
				
			||||||
    return ChapterResult.success(value);
 | 
					    return ChapterResult.success(value);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					package com.iluwatar.saga.orchestration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,10 +20,11 @@
 | 
				
			|||||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
				
			||||||
 * THE SOFTWARE.
 | 
					 * THE SOFTWARE.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.saga.orchestration;
 | 
					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<String> {
 | 
					public class WithdrawMoneyService extends Service<String> {
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user