Skip to content
Prev Previous commit
Next Next commit
add ch
  • Loading branch information
Besok committed Nov 3, 2019
commit 1b32e23493d1383033603162741c42264c8c965d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions saga/src/main/java/com/iluwatar/saga/choreography/Chapter.java
Original file line number Diff line number Diff line change
@@ -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 <K> is type for passing params
*/
public interface Chapter<K> {

/**
* @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<K> execute(K value);

/**
* The operation executed in general case.
* @param value incoming value
* @return result {@link ChapterResult}
*/
ChapterResult<K> process(K value);

/**
* The operation executed in rollback case.
* @param value incoming value
* @return result {@link ChapterResult}
*/
ChapterResult<K> rollback(K value);

}
Original file line number Diff line number Diff line change
@@ -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<K> implements Chapter<K> {

protected static final Logger logger = LoggerFactory.getLogger(OrchestrationService.class);

private final ServiceDiscoveryService service;
protected ChoreographyService(ServiceDiscoveryService service) {
this.service = service;
}

@Override
public ChapterResult<K> 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<K> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.iluwatar.saga.choreography;

public class RichSaga<K> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Class representing a service to book a fly
*/
public class FlyBookingService extends Service<String> {
public class FlyBookingService extends OrchestrationService<String> {
@Override
public String getName() {
return "booking a Fly";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Class representing a service to book a hotel
*/
public class HotelBookingService extends Service<String> {
public class HotelBookingService extends OrchestrationService<String> {
@Override
public String getName() {
return "booking a Hotel";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* implementing a general contract @see {@link Chapter}
* @param <K> type of incoming param
*/
public abstract class Service<K> implements Chapter<K> {
protected static final Logger logger = LoggerFactory.getLogger(Service.class);
public abstract class OrchestrationService<K> implements Chapter<K> {
protected static final Logger logger = LoggerFactory.getLogger(OrchestrationService.class);

@Override
public abstract String getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Class representing a service to init a new order.
*/
public class OrderService extends Service<String> {
public class OrderService extends OrchestrationService<String> {
@Override
public String getName() {
return "init an order";
Expand Down
3 changes: 3 additions & 0 deletions saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,8 @@ public Chapter(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.iluwatar.saga.orchestration;


import com.iluwatar.saga.ServiceDiscoveryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,7 +41,7 @@
*
* @see Saga
* @see SagaOrchestrator
* @see Service
* @see OrchestrationService
*/
public class SagaApplication {
private static final Logger logger = LoggerFactory.getLogger(SagaApplication.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.iluwatar.saga.orchestration;

import com.iluwatar.saga.ServiceDiscoveryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Class representing a service to withdraw a money
*/
public class WithdrawMoneyService extends Service<String> {
public class WithdrawMoneyService extends OrchestrationService<String> {
@Override
public String getName() {
return "withdrawing Money";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iluwatar.saga.orchestration;

import com.iluwatar.saga.ServiceDiscoveryService;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -38,7 +39,7 @@ private ServiceDiscoveryService serviceDiscovery() {
.discover(new Service4());
}

class Service1 extends Service<Integer> {
class Service1 extends OrchestrationService<Integer> {

@Override
public String getName() {
Expand All @@ -58,7 +59,7 @@ public ChapterResult<Integer> rollback(Integer value) {
}
}

class Service2 extends Service<Integer> {
class Service2 extends OrchestrationService<Integer> {

@Override
public String getName() {
Expand All @@ -77,7 +78,7 @@ public ChapterResult<Integer> rollback(Integer value) {
}
}

class Service3 extends Service<Integer> {
class Service3 extends OrchestrationService<Integer> {

@Override
public String getName() {
Expand All @@ -96,7 +97,7 @@ public ChapterResult<Integer> rollback(Integer value) {
}
}

class Service4 extends Service<Integer> {
class Service4 extends OrchestrationService<Integer> {

@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down