This commit is contained in:
Besok 2019-11-03 12:03:33 +00:00
parent 3a4677c56d
commit 1b32e23493
14 changed files with 117 additions and 14 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,4 @@
package com.iluwatar.saga.choreography;
public class RichSaga<K> {
}

View File

@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration;
/**
* 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";

View File

@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration;
/**
* 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";

View File

@ -30,8 +30,8 @@ import org.slf4j.LoggerFactory;
* 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();

View File

@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration;
/**
* 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";

View File

@ -70,5 +70,8 @@ public class Saga {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

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

View File

@ -22,6 +22,7 @@
*/
package com.iluwatar.saga.orchestration;
import com.iluwatar.saga.ServiceDiscoveryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration;
/**
* 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";

View File

@ -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<Integer> {
class Service1 extends OrchestrationService<Integer> {
@Override
public String getName() {
@ -58,7 +59,7 @@ public class SagaOrchestratorInternallyTest {
}
}
class Service2 extends Service<Integer> {
class Service2 extends OrchestrationService<Integer> {
@Override
public String getName() {
@ -77,7 +78,7 @@ public class SagaOrchestratorInternallyTest {
}
}
class Service3 extends Service<Integer> {
class Service3 extends OrchestrationService<Integer> {
@Override
public String getName() {
@ -96,7 +97,7 @@ public class SagaOrchestratorInternallyTest {
}
}
class Service4 extends Service<Integer> {
class Service4 extends OrchestrationService<Integer> {
@Override
public String getName() {

View File

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