add ch
This commit is contained in:
parent
3a4677c56d
commit
1b32e23493
@ -20,7 +20,9 @@
|
|||||||
* 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;
|
||||||
|
|
||||||
|
import com.iluwatar.saga.orchestration.Chapter;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
@ -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);
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.iluwatar.saga.choreography;
|
||||||
|
|
||||||
|
public class RichSaga<K> {
|
||||||
|
}
|
@ -25,7 +25,7 @@ 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 OrchestrationService<String> {
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "booking a Fly";
|
return "booking a Fly";
|
||||||
|
@ -25,7 +25,7 @@ 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 OrchestrationService<String> {
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "booking a Hotel";
|
return "booking a Hotel";
|
||||||
|
@ -30,8 +30,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
* implementing a general contract @see {@link Chapter}
|
* implementing a general contract @see {@link Chapter}
|
||||||
* @param <K> type of incoming param
|
* @param <K> type of incoming param
|
||||||
*/
|
*/
|
||||||
public abstract class Service<K> implements Chapter<K> {
|
public abstract class OrchestrationService<K> implements Chapter<K> {
|
||||||
protected static final Logger logger = LoggerFactory.getLogger(Service.class);
|
protected static final Logger logger = LoggerFactory.getLogger(OrchestrationService.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String getName();
|
public abstract String getName();
|
@ -25,7 +25,7 @@ package com.iluwatar.saga.orchestration;
|
|||||||
/**
|
/**
|
||||||
* Class representing a service to init a new order.
|
* Class representing a service to init a new order.
|
||||||
*/
|
*/
|
||||||
public class OrderService extends Service<String> {
|
public class OrderService extends OrchestrationService<String> {
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "init an order";
|
return "init an order";
|
||||||
|
@ -70,5 +70,8 @@ public class Saga {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
package com.iluwatar.saga.orchestration;
|
package com.iluwatar.saga.orchestration;
|
||||||
|
|
||||||
|
|
||||||
|
import com.iluwatar.saga.ServiceDiscoveryService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
*
|
*
|
||||||
* @see Saga
|
* @see Saga
|
||||||
* @see SagaOrchestrator
|
* @see SagaOrchestrator
|
||||||
* @see Service
|
* @see OrchestrationService
|
||||||
*/
|
*/
|
||||||
public class SagaApplication {
|
public class SagaApplication {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SagaApplication.class);
|
private static final Logger logger = LoggerFactory.getLogger(SagaApplication.class);
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.saga.orchestration;
|
package com.iluwatar.saga.orchestration;
|
||||||
|
|
||||||
|
import com.iluwatar.saga.ServiceDiscoveryService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ 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 OrchestrationService<String> {
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "withdrawing Money";
|
return "withdrawing Money";
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.iluwatar.saga.orchestration;
|
package com.iluwatar.saga.orchestration;
|
||||||
|
|
||||||
|
import com.iluwatar.saga.ServiceDiscoveryService;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ public class SagaOrchestratorInternallyTest {
|
|||||||
.discover(new Service4());
|
.discover(new Service4());
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service1 extends Service<Integer> {
|
class Service1 extends OrchestrationService<Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -58,7 +59,7 @@ public class SagaOrchestratorInternallyTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service2 extends Service<Integer> {
|
class Service2 extends OrchestrationService<Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -77,7 +78,7 @@ public class SagaOrchestratorInternallyTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service3 extends Service<Integer> {
|
class Service3 extends OrchestrationService<Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -96,7 +97,7 @@ public class SagaOrchestratorInternallyTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service4 extends Service<Integer> {
|
class Service4 extends OrchestrationService<Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
package com.iluwatar.saga.orchestration;
|
package com.iluwatar.saga.orchestration;
|
||||||
|
|
||||||
|
import com.iluwatar.saga.ServiceDiscoveryService;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class SagaOrchestratorTest {
|
public class SagaOrchestratorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user