Skip to content

Commit

Permalink
add changes according to google style
Browse files Browse the repository at this point in the history
  • Loading branch information
Besok committed Nov 13, 2019
1 parent de56cbb commit 87af122
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.choreography;


Expand All @@ -31,14 +32,15 @@
public interface ChoreographyChapter {

/**
* In that case, every method is responsible to make a decision on what to do then
* In that case, every method is responsible to make a decision on what to do then.
*
* @param saga incoming saga
* @return saga result
*/
Saga execute(Saga saga);

/**
* get name method.
* @return service name.
*/
String getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.choreography;


/**
* Class representing a service to book a fly
* Class representing a service to book a fly.
*/
public class FlyBookingService extends Service {
public FlyBookingService(ServiceDiscoveryService service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.choreography;


/**
* Class representing a service to book a hotel
* Class representing a service to book a hotel.
*/
public class HotelBookingService extends Service {
public HotelBookingService(ServiceDiscoveryService service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.choreography;


Expand Down
26 changes: 14 additions & 12 deletions saga/src/main/java/com/iluwatar/saga/choreography/Saga.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.choreography;

import java.util.ArrayList;
Expand All @@ -44,7 +45,7 @@ public static Saga create() {
}

/**
* get resuzlt of saga
* get resuzlt of saga.
*
* @return result of saga @see {@link SagaResult}
*/
Expand All @@ -59,7 +60,7 @@ public SagaResult getResult() {
}

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

/**
* set value to last chapter
* set value to last chapter.
* @param value invalue
* @return this
*/
Expand All @@ -82,23 +83,23 @@ public Saga setInValue(Object value) {
}

/**
* get value from current chapter
* get value from current chapter.
* @return value
*/
public Object getCurrentValue() {
return chapters.get(pos).getInValue();
}

/**
* set value to current chapter
* set value to current chapter.
* @param value to set
*/
public void setCurrentValue(Object value) {
chapters.get(pos).setInValue(value);
}

/**
* set status for current chapter
* set status for current chapter.
* @param result to set
*/
public void setCurrentStatus(ChapterResult result) {
Expand Down Expand Up @@ -143,8 +144,9 @@ boolean isCurrentSuccess() {
return chapters.get(pos).isSuccess();
}

/***
* 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 @@ -170,15 +172,15 @@ public String getName() {
}

/**
* set result
* set result.
* @param result {@link ChapterResult}
*/
public void setResult(ChapterResult result) {
this.result = result;
}

/**
* the result for chapter is good
* the result for chapter is good.
* @return true if is good otherwise bad
*/
public boolean isSuccess() {
Expand All @@ -188,14 +190,14 @@ public boolean isSuccess() {


/**
* result for chapter
* result for chapter.
*/
public enum ChapterResult {
INIT, SUCCESS, ROLLBACK
}

/**
* result for saga
* result for saga.
*/
public enum SagaResult {
PROGRESS, FINISHED, ROLLBACKED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.choreography;

package com.iluwatar.saga.choreography;

import org.slf4j.Logger;
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
* 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) to undo the impact of the preceding transactions.
* <p>
* In this approach, there are no mediators or orchestrators services.
*
* <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)
* 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)
*
* <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)
*
* @see com.iluwatar.saga.choreography.Saga
* @see Service
Expand All @@ -47,7 +51,7 @@ public class SagaApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(SagaApplication.class);

/**
* main method
* main method.
*/
public static void main(String[] args) {
ServiceDiscoveryService sd = serviceDiscovery();
Expand Down
14 changes: 9 additions & 5 deletions saga/src/main/java/com/iluwatar/saga/choreography/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.choreography;

import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Supplier;

/**
* Common abstraction class representing services
* Common abstraction class representing services.
* implementing a general contract @see {@link ChoreographyChapter}
*/
public abstract class Service implements ChoreographyChapter {
Expand Down Expand Up @@ -73,13 +74,15 @@ public Saga execute(Saga saga) {
}

private Supplier<RuntimeException> serviceNotFoundException(String chServiceName) {
return () -> new RuntimeException(String.format("the service %s has not been found", chServiceName));
return () -> new RuntimeException(
String.format("the service %s has not been found", chServiceName));
}

@Override
public Saga process(Saga saga) {
Object inValue = saga.getCurrentValue();
LOGGER.info("The chapter '{}' has been started. The data {} has been stored or calculated successfully",
LOGGER.info("The chapter '{}' has been started. "
+ "The data {} has been stored or calculated successfully",
getName(), inValue);
saga.setCurrentStatus(Saga.ChapterResult.SUCCESS);
saga.setCurrentValue(inValue);
Expand All @@ -89,7 +92,8 @@ public Saga process(Saga saga) {
@Override
public Saga rollback(Saga saga) {
Object inValue = saga.getCurrentValue();
LOGGER.info("The Rollback for a chapter '{}' has been started. The data {} has been rollbacked successfully",
LOGGER.info("The Rollback for a chapter '{}' has been started. "
+ "The data {} has been rollbacked successfully",
getName(), inValue);

saga.setCurrentStatus(Saga.ChapterResult.ROLLBACK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.choreography;

package com.iluwatar.saga.choreography;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,7 +35,7 @@ public class ServiceDiscoveryService {
private Map<String, ChoreographyChapter> services;

/**
* find any service
* find any service.
*
* @return any service
* @throws NoSuchElementException if no elements further
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.choreography;

package com.iluwatar.saga.choreography;

/**
* Class representing a service to withdraw a money
* Class representing a service to withdraw a money.
*/
public class WithdrawMoneyService extends Service {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.orchestration;

/**
* Executing result for chapter
* Executing result for chapter.
*
* @param <K> incoming value
*/
Expand Down Expand Up @@ -53,7 +54,7 @@ public static <K> ChapterResult<K> failure(K val) {
}

/**
* state for chapter
* state for chapter.
*/
public enum State {
SUCCESS, FAILURE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

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> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

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> {
@Override
Expand All @@ -42,7 +43,8 @@ public ChapterResult<String> rollback(String value) {
return ChapterResult.failure(value);
}

LOGGER.info("The Rollback for a chapter '{}' has been started. The data {} has been rollbacked successfully",
LOGGER.info("The Rollback for a chapter '{}' has been started. "
+ "The data {} has been rollbacked successfully",
getName(), value);

return super.rollback(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.orchestration;

/**
Expand All @@ -30,6 +31,7 @@
public interface OrchestrationChapter<K> {

/**
* method get name.
* @return service name.
*/
String getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.saga.orchestration;

/**
Expand Down
Loading

0 comments on commit 87af122

Please sign in to comment.