Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental saga participant proxy concept, misc refactoring
- Loading branch information
Showing
18 changed files
with
312 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ build/ | |
*.idea/ | ||
*.iml | ||
*.log | ||
out |
32 changes: 32 additions & 0 deletions
32
...as-framework/src/main/java/io/eventuate/tram/sagas/orchestration/SagaCommandProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.eventuate.tram.sagas.orchestration; | ||
|
||
import io.eventuate.tram.commands.common.Command; | ||
import io.eventuate.tram.commands.producer.CommandProducer; | ||
import io.eventuate.tram.sagas.common.SagaCommandHeaders; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SagaCommandProducer { | ||
|
||
@Autowired | ||
private CommandProducer commandProducer; | ||
|
||
public SagaCommandProducer() { | ||
} | ||
|
||
public SagaCommandProducer(CommandProducer commandProducer) { | ||
this.commandProducer = commandProducer; | ||
} | ||
|
||
|
||
|
||
public void sendCommand(String sagaType, String sagaId, String destinationChannel, String resource, String requestId, Command command, String replyTo) { | ||
Map<String, String> headers = new HashMap<>(); | ||
headers.put(SagaCommandHeaders.SAGA_TYPE, sagaType); | ||
headers.put(SagaCommandHeaders.SAGA_ID, sagaId); | ||
headers.put(SagaCommandHeaders.SAGA_REQUEST_ID, requestId); | ||
commandProducer.send(destinationChannel, resource, command, replyTo, headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ram-sagas-simple-dsl/src/main/java/io/eventuate/tram/sagas/simpledsl/CommandEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.eventuate.tram.sagas.simpledsl; | ||
|
||
import io.eventuate.tram.commands.common.Command; | ||
|
||
import java.util.Set; | ||
|
||
public class CommandEndpoint<C extends Command> { | ||
|
||
private String commandChannel; | ||
private Class<C> commandClass; | ||
private Set<Class> replyClasses; | ||
|
||
public CommandEndpoint(String commandChannel, Class<C> commandClass, Set<Class> replyClasses) { | ||
this.commandChannel = commandChannel; | ||
this.commandClass = commandClass; | ||
this.replyClasses = replyClasses; | ||
} | ||
|
||
public String getCommandChannel() { | ||
return commandChannel; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...as-simple-dsl/src/main/java/io/eventuate/tram/sagas/simpledsl/CommandEndpointBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.eventuate.tram.sagas.simpledsl; | ||
|
||
import io.eventuate.tram.commands.common.Command; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class CommandEndpointBuilder<C extends Command> { | ||
|
||
private String channel; | ||
private Class<C> commandClass; | ||
private Set<Class> replyClasses = new HashSet<>(); | ||
|
||
public CommandEndpointBuilder(Class<C> commandClass) { | ||
this.commandClass = commandClass; | ||
} | ||
|
||
public static <C extends Command> CommandEndpointBuilder<C> forCommand(Class<C> commandClass) { | ||
return new CommandEndpointBuilder<>(commandClass); | ||
} | ||
|
||
public CommandEndpointBuilder<C> withChannel(String channel) { | ||
this.channel = channel; | ||
return this; | ||
} | ||
|
||
|
||
public <T> CommandEndpointBuilder<C> withReply(Class<T> replyClass) { | ||
this.replyClasses.add(replyClass); | ||
return this; | ||
} | ||
|
||
public CommandEndpoint<C> build() { | ||
return new CommandEndpoint<>(channel, commandClass, replyClasses); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...sl/src/main/java/io/eventuate/tram/sagas/simpledsl/ParticipantEndpointInvocationImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.eventuate.tram.sagas.simpledsl; | ||
|
||
import io.eventuate.tram.commands.common.Command; | ||
import io.eventuate.tram.commands.common.CommandReplyOutcome; | ||
import io.eventuate.tram.commands.common.ReplyMessageHeaders; | ||
import io.eventuate.tram.commands.consumer.CommandWithDestination; | ||
import io.eventuate.tram.messaging.common.Message; | ||
|
||
import java.util.function.Function; | ||
|
||
public class ParticipantEndpointInvocationImpl<Data, C extends Command> implements ParticipantInvocation<Data> { | ||
|
||
|
||
private final CommandEndpoint<C> commandEndpoint; | ||
private final Function<Data, C> commandProvider; | ||
|
||
public ParticipantEndpointInvocationImpl(CommandEndpoint<C> commandEndpoint, Function<Data, C> commandProvider) { | ||
this.commandEndpoint = commandEndpoint; | ||
this.commandProvider = commandProvider; | ||
} | ||
|
||
@Override | ||
public boolean isSuccessfulReply(Message message) { | ||
return CommandReplyOutcome.SUCCESS.name().equals(message.getRequiredHeader(ReplyMessageHeaders.REPLY_OUTCOME)); | ||
} | ||
|
||
@Override | ||
public CommandWithDestination makeCommandToSend(Data data) { | ||
return new CommandWithDestination(commandEndpoint.getCommandChannel(), null, commandProvider.apply(data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
FROM eventuateio/eventuate-tram-mysql:0.2.0 | ||
FROM eventuateio/eventuate-tram-mysql:0.5.0.RELEASE | ||
COPY tram-saga-schema.sql /docker-entrypoint-initdb.d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...te/examples/tram/sagas/ordersandcustomers/orders/sagas/createorder/CreateOrderSagaV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.eventuate.examples.tram.sagas.ordersandcustomers.orders.sagas.createorder; | ||
|
||
import io.eventuate.examples.tram.sagas.ordersandcustomers.commondomain.Money; | ||
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.sagas.participants.ApproveOrderCommand; | ||
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.sagas.participants.ReserveCreditCommand; | ||
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.sagas.participants.proxy.CustomerServiceProxy; | ||
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.sagas.participants.proxy.OrderServiceProxy; | ||
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.service.RejectOrderCommand; | ||
import io.eventuate.tram.sagas.orchestration.SagaDefinition; | ||
import io.eventuate.tram.sagas.simpledsl.SimpleSaga; | ||
|
||
public class CreateOrderSagaV2 implements SimpleSaga<CreateOrderSagaData> { | ||
|
||
private CustomerServiceProxy customerService; | ||
private OrderServiceProxy orderService; | ||
|
||
public CreateOrderSagaV2(CustomerServiceProxy customerService, OrderServiceProxy orderService) { | ||
this.customerService = customerService; | ||
this.orderService = orderService; | ||
} | ||
|
||
private SagaDefinition<CreateOrderSagaData> sagaDefinition = | ||
step() | ||
.withCompensation(orderService.reject, this::makeRejectCommand) | ||
.step() | ||
.invokeParticipant(customerService.reserveCredit, this::makeReserveCreditCommand) | ||
.step() | ||
.invokeParticipant(orderService.approve, this::makeApproveCommand) | ||
.build(); | ||
|
||
|
||
@Override | ||
public SagaDefinition<CreateOrderSagaData> getSagaDefinition() { | ||
return this.sagaDefinition; | ||
} | ||
|
||
|
||
private ReserveCreditCommand makeReserveCreditCommand(CreateOrderSagaData data) { | ||
long orderId = data.getOrderId(); | ||
Long customerId = data.getOrderDetails().getCustomerId(); | ||
Money orderTotal = data.getOrderDetails().getOrderTotal(); | ||
return new ReserveCreditCommand(customerId, orderId, orderTotal); | ||
} | ||
|
||
private RejectOrderCommand makeRejectCommand(CreateOrderSagaData data) { | ||
return new RejectOrderCommand(data.getOrderId()); | ||
} | ||
|
||
private ApproveOrderCommand makeApproveCommand(CreateOrderSagaData data) { | ||
return new ApproveOrderCommand(data.getOrderId()); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.