Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 11 migrate all remaining s #1120

Merged
merged 16 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@


/**
* ChoreographyChapter is an interface representing a contract for an external service.
* In that case, a service needs to make a decision what to do further
* hence the server needs to get all context representing {@link Saga}
* ChoreographyChapter is an interface representing a contract for an external service. In that
* case, a service needs to make a decision what to do further hence the server needs to get all
* context representing {@link Saga}
*/
public interface ChoreographyChapter {

Expand All @@ -41,6 +41,7 @@ public interface ChoreographyChapter {

/**
* get name method.
*
* @return service name.
*/
String getName();
Expand Down
16 changes: 11 additions & 5 deletions saga/src/main/java/com/iluwatar/saga/choreography/Saga.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import java.util.List;

/**
* Saga representation.
* Saga consists of chapters.
* Every ChoreographyChapter is executed a certain service.
* Saga representation. Saga consists of chapters. Every ChoreographyChapter is executed a certain
* service.
*/
public class Saga {

Expand Down Expand Up @@ -61,6 +60,7 @@ public SagaResult getResult() {

/**
* add chapter to saga.
*
* @param name chapter name
* @return this
*/
Expand All @@ -71,6 +71,7 @@ public Saga chapter(String name) {

/**
* set value to last chapter.
*
* @param value invalue
* @return this
*/
Expand All @@ -84,6 +85,7 @@ public Saga setInValue(Object value) {

/**
* get value from current chapter.
*
* @return value
*/
public Object getCurrentValue() {
Expand All @@ -92,6 +94,7 @@ public Object getCurrentValue() {

/**
* set value to current chapter.
*
* @param value to set
*/
public void setCurrentValue(Object value) {
Expand All @@ -100,6 +103,7 @@ public void setCurrentValue(Object value) {

/**
* set status for current chapter.
*
* @param result to set
*/
public void setCurrentStatus(ChapterResult result) {
Expand Down Expand Up @@ -145,8 +149,8 @@ boolean isCurrentSuccess() {
}

/**
* 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;
Expand All @@ -173,6 +177,7 @@ public String getName() {

/**
* set result.
*
* @param result {@link ChapterResult}
*/
public void setResult(ChapterResult result) {
Expand All @@ -181,6 +186,7 @@ public void setResult(ChapterResult result) {

/**
* the result for chapter is good.
*
* @return true if is good otherwise bad
*/
public boolean isSuccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@
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
* in a distributed environment
* 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
*
* <p>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)
* If one transaction fails for some reason, the saga executes compensating transactions(rollbacks)
* 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.
*
* <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 Service
Expand All @@ -54,10 +51,10 @@ public class SagaApplication {
* main method.
*/
public static void main(String[] args) {
ServiceDiscoveryService sd = serviceDiscovery();
ChoreographyChapter service = sd.findAny();
Saga goodOrderSaga = service.execute(newSaga("good_order"));
Saga badOrderSaga = service.execute(newSaga("bad_order"));
var sd = serviceDiscovery();
var service = sd.findAny();
var goodOrderSaga = service.execute(newSaga("good_order"));
var badOrderSaga = service.execute(newSaga("bad_order"));
LOGGER.info("orders: goodOrder is {}, badOrder is {}",
goodOrderSaga.getResult(), badOrderSaga.getResult());

Expand All @@ -74,7 +71,7 @@ private static Saga newSaga(Object value) {
}

private static ServiceDiscoveryService serviceDiscovery() {
ServiceDiscoveryService sd = new ServiceDiscoveryService();
var sd = new ServiceDiscoveryService();
return sd
.discover(new OrderService(sd))
.discover(new FlyBookingService(sd))
Expand Down
14 changes: 7 additions & 7 deletions saga/src/main/java/com/iluwatar/saga/choreography/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@


/**
* Common abstraction class representing services.
* implementing a general contract @see {@link ChoreographyChapter}
* Common abstraction class representing services. implementing a general contract @see {@link
* ChoreographyChapter}
*/
public abstract class Service implements ChoreographyChapter {
protected static final Logger LOGGER = LoggerFactory.getLogger(Service.class);
Expand All @@ -43,9 +43,9 @@ public Service(ServiceDiscoveryService service) {

@Override
public Saga execute(Saga saga) {
Saga nextSaga = saga;
var nextSaga = saga;
Object nextVal;
String chapterName = saga.getCurrent().getName();
var chapterName = saga.getCurrent().getName();
if (chapterName.equals(getName())) {
if (saga.isForward()) {
nextSaga = process(saga);
Expand All @@ -67,7 +67,7 @@ public Saga execute(Saga saga) {

nextSaga.setCurrentValue(nextVal);
}
Saga finalNextSaga = nextSaga;
var finalNextSaga = nextSaga;

return sd.find(chapterName).map(ch -> ch.execute(finalNextSaga))
.orElseThrow(serviceNotFoundException(chapterName));
Expand All @@ -80,7 +80,7 @@ private Supplier<RuntimeException> serviceNotFoundException(String chServiceName

@Override
public Saga process(Saga saga) {
Object inValue = saga.getCurrentValue();
var inValue = saga.getCurrentValue();
LOGGER.info("The chapter '{}' has been started. "
+ "The data {} has been stored or calculated successfully",
getName(), inValue);
Expand All @@ -91,7 +91,7 @@ public Saga process(Saga saga) {

@Override
public Saga rollback(Saga saga) {
Object inValue = saga.getCurrentValue();
var inValue = saga.getCurrentValue();
LOGGER.info("The Rollback for a chapter '{}' has been started. "
+ "The data {} has been rollbacked successfully",
getName(), inValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getName() {

@Override
public Saga process(Saga saga) {
Object inValue = saga.getCurrentValue();
var inValue = saga.getCurrentValue();

if (inValue.equals("bad_order")) {
LOGGER.info("The chapter '{}' has been started. But the exception has been raised."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface OrchestrationChapter<K> {

/**
* method get name.
*
* @return service name.
*/
String getName();
Expand Down
5 changes: 2 additions & 3 deletions saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import java.util.List;

/**
* Saga representation.
* Saga consists of chapters.
* Every ChoreographyChapter is executed by a certain service.
* Saga representation. Saga consists of chapters. Every ChoreographyChapter is executed by a
* certain service.
*/
public class Saga {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@
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
* in a distributed environment
* 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
*
* <p>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)
* If one transaction fails for some reason, the saga executes compensating transactions(rollbacks)
* 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
* 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)
* 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)
*
* @see Saga
* @see SagaOrchestrator
Expand All @@ -56,7 +52,7 @@ public class SagaApplication {
* method to show common saga logic.
*/
public static void main(String[] args) {
SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
var sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());

Saga.Result goodOrder = sagaOrchestrator.execute("good_order");
Saga.Result badOrder = sagaOrchestrator.execute("bad_order");
Expand All @@ -77,11 +73,10 @@ private static Saga newSaga() {
}

private static ServiceDiscoveryService serviceDiscovery() {
return
new ServiceDiscoveryService()
.discover(new OrderService())
.discover(new FlyBookingService())
.discover(new HotelBookingService())
.discover(new WithdrawMoneyService());
return new ServiceDiscoveryService()
.discover(new OrderService())
.discover(new FlyBookingService())
.discover(new HotelBookingService())
.discover(new WithdrawMoneyService());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

package com.iluwatar.saga.orchestration;

import static com.iluwatar.saga.orchestration.Saga.Result;
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;


/**
* The orchestrator that manages all the transactions and directs
* the participant services to execute local transactions based on events.
* The orchestrator that manages all the transactions and directs the participant services to
* execute local transactions based on events.
*/
public class SagaOrchestrator {
private static final Logger LOGGER = LoggerFactory.getLogger(SagaOrchestrator.class);
Expand All @@ -45,8 +45,9 @@ public class SagaOrchestrator {

/**
* Create a new service to orchetrate sagas.
*
* @param saga saga to process
* @param sd service discovery @see {@link ServiceDiscoveryService}
* @param sd service discovery @see {@link ServiceDiscoveryService}
*/
public SagaOrchestrator(Saga saga, ServiceDiscoveryService sd) {
this.saga = saga;
Expand All @@ -59,38 +60,38 @@ public SagaOrchestrator(Saga saga, ServiceDiscoveryService sd) {
*
* @param value incoming value
* @param <K> type for incoming value
* @return result @see {@link Saga.Result}
* @return result @see {@link Result}
*/
@SuppressWarnings("unchecked")
public <K> Saga.Result execute(K value) {
public <K> Result execute(K value) {
state.cleanUp();
LOGGER.info(" The new saga is about to start");
Saga.Result result = FINISHED;
var result = FINISHED;
K tempVal = value;

while (true) {
int next = state.current();
Saga.Chapter ch = saga.get(next);
Optional<OrchestrationChapter> srvOpt = sd.find(ch.name);
var next = state.current();
var ch = saga.get(next);
var srvOpt = sd.find(ch.name);

if (!srvOpt.isPresent()) {
if (srvOpt.isEmpty()) {
state.directionToBack();
state.back();
continue;
}

OrchestrationChapter srv = srvOpt.get();
var srv = srvOpt.get();

if (state.isForward()) {
ChapterResult processRes = srv.process(tempVal);
var processRes = srv.process(tempVal);
if (processRes.isSuccess()) {
next = state.forward();
tempVal = (K) processRes.getValue();
} else {
state.directionToBack();
}
} else {
ChapterResult rlRes = srv.rollback(tempVal);
var rlRes = srv.rollback(tempVal);
if (rlRes.isSuccess()) {
next = state.back();
tempVal = (K) rlRes.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.slf4j.LoggerFactory;

/**
* Common abstraction class representing services.
* implementing a general contract @see {@link OrchestrationChapter}
* Common abstraction class representing services. implementing a general contract @see {@link
* OrchestrationChapter}
*
* @param <K> type of incoming param
*/
Expand Down
Loading