Skip to content

Commit cd2a2e7

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Java 11 migrate all remaining s (iluwatar#1120)
* Moves saga to Java 11 * Moves semaphore to Java 11 * Moves servant to Java 11 * Moves serverless to Java 11 * Moves service-layer to Java 11 * Moves service-locator to Java 11 * Moves sharding to Java 11 * Moves singleton to Java 11 * Moves spatial-partition to Java 11 * Moves specification to Java 11 * Moves state to Java 11 * Moves step-builder to Java 11 * Moves strategy to Java 11 * Moves subclass-sandbox to Java 11 * Fixes checkstyle issues
1 parent 310ae50 commit cd2a2e7

File tree

98 files changed

+717
-854
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+717
-854
lines changed

saga/src/main/java/com/iluwatar/saga/choreography/ChoreographyChapter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626

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

@@ -41,6 +41,7 @@ public interface ChoreographyChapter {
4141

4242
/**
4343
* get name method.
44+
*
4445
* @return service name.
4546
*/
4647
String getName();

saga/src/main/java/com/iluwatar/saga/choreography/Saga.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
import java.util.List;
2929

3030
/**
31-
* Saga representation.
32-
* Saga consists of chapters.
33-
* Every ChoreographyChapter is executed a certain service.
31+
* Saga representation. Saga consists of chapters. Every ChoreographyChapter is executed a certain
32+
* service.
3433
*/
3534
public class Saga {
3635

@@ -61,6 +60,7 @@ public SagaResult getResult() {
6160

6261
/**
6362
* add chapter to saga.
63+
*
6464
* @param name chapter name
6565
* @return this
6666
*/
@@ -71,6 +71,7 @@ public Saga chapter(String name) {
7171

7272
/**
7373
* set value to last chapter.
74+
*
7475
* @param value invalue
7576
* @return this
7677
*/
@@ -84,6 +85,7 @@ public Saga setInValue(Object value) {
8485

8586
/**
8687
* get value from current chapter.
88+
*
8789
* @return value
8890
*/
8991
public Object getCurrentValue() {
@@ -92,6 +94,7 @@ public Object getCurrentValue() {
9294

9395
/**
9496
* set value to current chapter.
97+
*
9598
* @param value to set
9699
*/
97100
public void setCurrentValue(Object value) {
@@ -100,6 +103,7 @@ public void setCurrentValue(Object value) {
100103

101104
/**
102105
* set status for current chapter.
106+
*
103107
* @param result to set
104108
*/
105109
public void setCurrentStatus(ChapterResult result) {
@@ -145,8 +149,8 @@ boolean isCurrentSuccess() {
145149
}
146150

147151
/**
148-
* Class presents a chapter status and incoming
149-
* parameters(incoming parameter transforms to outcoming parameter).
152+
* Class presents a chapter status and incoming parameters(incoming parameter transforms to
153+
* outcoming parameter).
150154
*/
151155
public static class Chapter {
152156
private String name;
@@ -173,6 +177,7 @@ public String getName() {
173177

174178
/**
175179
* set result.
180+
*
176181
* @param result {@link ChapterResult}
177182
*/
178183
public void setResult(ChapterResult result) {
@@ -181,6 +186,7 @@ public void setResult(ChapterResult result) {
181186

182187
/**
183188
* the result for chapter is good.
189+
*
184190
* @return true if is good otherwise bad
185191
*/
186192
public boolean isSuccess() {

saga/src/main/java/com/iluwatar/saga/choreography/SagaApplication.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,19 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
/**
30-
* This pattern is used in distributed services to perform a group of operations atomically.
31-
* This is an analog of transaction in a database but in terms
32-
* of microservices architecture this is executed
33-
* in a distributed environment
30+
* This pattern is used in distributed services to perform a group of operations atomically. This is
31+
* an analog of transaction in a database but in terms of microservices architecture this is
32+
* executed in a distributed environment
3433
*
3534
* <p>A saga is a sequence of local transactions in a certain context.
36-
* If one transaction fails for some reason,
37-
* the saga executes compensating transactions(rollbacks)
35+
* If one transaction fails for some reason, the saga executes compensating transactions(rollbacks)
3836
* to undo the impact of the preceding transactions.
3937
*
4038
* <p>In this approach, there are no mediators or orchestrators services.
4139
* All chapters are handled and moved by services manually.
4240
*
4341
* <p>The major difference with choreography saga is an ability to handle crashed services
44-
* (otherwise in choreography services very hard to prevent a saga
45-
* if one of them has been crashed)
42+
* (otherwise in choreography services very hard to prevent a saga if one of them has been crashed)
4643
*
4744
* @see com.iluwatar.saga.choreography.Saga
4845
* @see Service
@@ -54,10 +51,10 @@ public class SagaApplication {
5451
* main method.
5552
*/
5653
public static void main(String[] args) {
57-
ServiceDiscoveryService sd = serviceDiscovery();
58-
ChoreographyChapter service = sd.findAny();
59-
Saga goodOrderSaga = service.execute(newSaga("good_order"));
60-
Saga badOrderSaga = service.execute(newSaga("bad_order"));
54+
var sd = serviceDiscovery();
55+
var service = sd.findAny();
56+
var goodOrderSaga = service.execute(newSaga("good_order"));
57+
var badOrderSaga = service.execute(newSaga("bad_order"));
6158
LOGGER.info("orders: goodOrder is {}, badOrder is {}",
6259
goodOrderSaga.getResult(), badOrderSaga.getResult());
6360

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

7673
private static ServiceDiscoveryService serviceDiscovery() {
77-
ServiceDiscoveryService sd = new ServiceDiscoveryService();
74+
var sd = new ServiceDiscoveryService();
7875
return sd
7976
.discover(new OrderService(sd))
8077
.discover(new FlyBookingService(sd))

saga/src/main/java/com/iluwatar/saga/choreography/Service.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030

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

4444
@Override
4545
public Saga execute(Saga saga) {
46-
Saga nextSaga = saga;
46+
var nextSaga = saga;
4747
Object nextVal;
48-
String chapterName = saga.getCurrent().getName();
48+
var chapterName = saga.getCurrent().getName();
4949
if (chapterName.equals(getName())) {
5050
if (saga.isForward()) {
5151
nextSaga = process(saga);
@@ -67,7 +67,7 @@ public Saga execute(Saga saga) {
6767

6868
nextSaga.setCurrentValue(nextVal);
6969
}
70-
Saga finalNextSaga = nextSaga;
70+
var finalNextSaga = nextSaga;
7171

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

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

9292
@Override
9393
public Saga rollback(Saga saga) {
94-
Object inValue = saga.getCurrentValue();
94+
var inValue = saga.getCurrentValue();
9595
LOGGER.info("The Rollback for a chapter '{}' has been started. "
9696
+ "The data {} has been rollbacked successfully",
9797
getName(), inValue);

saga/src/main/java/com/iluwatar/saga/choreography/WithdrawMoneyService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public String getName() {
3939

4040
@Override
4141
public Saga process(Saga saga) {
42-
Object inValue = saga.getCurrentValue();
42+
var inValue = saga.getCurrentValue();
4343

4444
if (inValue.equals("bad_order")) {
4545
LOGGER.info("The chapter '{}' has been started. But the exception has been raised."

saga/src/main/java/com/iluwatar/saga/orchestration/OrchestrationChapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public interface OrchestrationChapter<K> {
3232

3333
/**
3434
* method get name.
35+
*
3536
* @return service name.
3637
*/
3738
String getName();

saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
import java.util.List;
2828

2929
/**
30-
* Saga representation.
31-
* Saga consists of chapters.
32-
* Every ChoreographyChapter is executed by a certain service.
30+
* Saga representation. Saga consists of chapters. Every ChoreographyChapter is executed by a
31+
* certain service.
3332
*/
3433
public class Saga {
3534

saga/src/main/java/com/iluwatar/saga/orchestration/SagaApplication.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,19 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
/**
30-
* This pattern is used in distributed services to perform
31-
* a group of operations atomically.
32-
* This is an analog of transaction in a database but in terms
33-
* of microservices architecture this is executed
34-
* in a distributed environment
30+
* This pattern is used in distributed services to perform a group of operations atomically. This is
31+
* an analog of transaction in a database but in terms of microservices architecture this is
32+
* executed in a distributed environment
3533
*
3634
* <p>A saga is a sequence of local transactions in a certain context.
37-
* If one transaction fails for some reason,
38-
* the saga executes compensating transactions(rollbacks)
35+
* If one transaction fails for some reason, the saga executes compensating transactions(rollbacks)
3936
* to undo the impact of the preceding transactions.
4037
*
4138
* <p>In this approach, there is an orchestrator @see {@link SagaOrchestrator}
42-
* that manages all the transactions and directs
43-
* the participant services to execute local transactions based on events.
44-
* The major difference with choreography saga is an ability to handle crashed services
45-
* (otherwise in choreography services very hard to prevent a saga
46-
* if one of them has been crashed)
39+
* that manages all the transactions and directs the participant services to execute local
40+
* transactions based on events. The major difference with choreography saga is an ability to handle
41+
* crashed services (otherwise in choreography services very hard to prevent a saga if one of them
42+
* has been crashed)
4743
*
4844
* @see Saga
4945
* @see SagaOrchestrator
@@ -56,7 +52,7 @@ public class SagaApplication {
5652
* method to show common saga logic.
5753
*/
5854
public static void main(String[] args) {
59-
SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
55+
var sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
6056

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

7975
private static ServiceDiscoveryService serviceDiscovery() {
80-
return
81-
new ServiceDiscoveryService()
82-
.discover(new OrderService())
83-
.discover(new FlyBookingService())
84-
.discover(new HotelBookingService())
85-
.discover(new WithdrawMoneyService());
76+
return new ServiceDiscoveryService()
77+
.discover(new OrderService())
78+
.discover(new FlyBookingService())
79+
.discover(new HotelBookingService())
80+
.discover(new WithdrawMoneyService());
8681
}
8782
}

saga/src/main/java/com/iluwatar/saga/orchestration/SagaOrchestrator.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323

2424
package com.iluwatar.saga.orchestration;
2525

26+
import static com.iluwatar.saga.orchestration.Saga.Result;
2627
import static com.iluwatar.saga.orchestration.Saga.Result.CRASHED;
2728
import static com.iluwatar.saga.orchestration.Saga.Result.FINISHED;
2829
import static com.iluwatar.saga.orchestration.Saga.Result.ROLLBACK;
2930

30-
import java.util.Optional;
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333

3434

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

4646
/**
4747
* Create a new service to orchetrate sagas.
48+
*
4849
* @param saga saga to process
49-
* @param sd service discovery @see {@link ServiceDiscoveryService}
50+
* @param sd service discovery @see {@link ServiceDiscoveryService}
5051
*/
5152
public SagaOrchestrator(Saga saga, ServiceDiscoveryService sd) {
5253
this.saga = saga;
@@ -59,38 +60,38 @@ public SagaOrchestrator(Saga saga, ServiceDiscoveryService sd) {
5960
*
6061
* @param value incoming value
6162
* @param <K> type for incoming value
62-
* @return result @see {@link Saga.Result}
63+
* @return result @see {@link Result}
6364
*/
6465
@SuppressWarnings("unchecked")
65-
public <K> Saga.Result execute(K value) {
66+
public <K> Result execute(K value) {
6667
state.cleanUp();
6768
LOGGER.info(" The new saga is about to start");
68-
Saga.Result result = FINISHED;
69+
var result = FINISHED;
6970
K tempVal = value;
7071

7172
while (true) {
72-
int next = state.current();
73-
Saga.Chapter ch = saga.get(next);
74-
Optional<OrchestrationChapter> srvOpt = sd.find(ch.name);
73+
var next = state.current();
74+
var ch = saga.get(next);
75+
var srvOpt = sd.find(ch.name);
7576

76-
if (!srvOpt.isPresent()) {
77+
if (srvOpt.isEmpty()) {
7778
state.directionToBack();
7879
state.back();
7980
continue;
8081
}
8182

82-
OrchestrationChapter srv = srvOpt.get();
83+
var srv = srvOpt.get();
8384

8485
if (state.isForward()) {
85-
ChapterResult processRes = srv.process(tempVal);
86+
var processRes = srv.process(tempVal);
8687
if (processRes.isSuccess()) {
8788
next = state.forward();
8889
tempVal = (K) processRes.getValue();
8990
} else {
9091
state.directionToBack();
9192
}
9293
} else {
93-
ChapterResult rlRes = srv.rollback(tempVal);
94+
var rlRes = srv.rollback(tempVal);
9495
if (rlRes.isSuccess()) {
9596
next = state.back();
9697
tempVal = (K) rlRes.getValue();

saga/src/main/java/com/iluwatar/saga/orchestration/Service.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
/**
30-
* Common abstraction class representing services.
31-
* implementing a general contract @see {@link OrchestrationChapter}
30+
* Common abstraction class representing services. implementing a general contract @see {@link
31+
* OrchestrationChapter}
3232
*
3333
* @param <K> type of incoming param
3434
*/

0 commit comments

Comments
 (0)