@Component
public class InTransitStatusChangeHandler extends AbstractFulfillmentStatusChangeHandler {
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
@Getter(AccessLevel.PROTECTED)
private final FulfillmentInTransitProducer messageProducer;
public InTransitStatusChangeHandler(
FulfillmentSplittingService<OrderFulfillment> splittingService,
FulfillmentProvider<OrderFulfillment> fulfillmentProvider,
OrderProvider<Order> orderProvider,
FulfillmentInTransitProducer messageProducer,
TypeFactory typeFactory) {
super(splittingService, fulfillmentProvider, orderProvider, typeFactory);
this.messageProducer = messageProducer;
}
// (1)
@Override
protected String getValidTargetStatus() {
return IN_TRANSIT.name();
}
// (2)
@Override
protected Set<String> getValidOriginatingStatuses() {
return new HashSet<>(Arrays.asList(FULFILLED.name()));
}
// Send message
@Override
protected OrderFulfillment postProcessStatusChange(
FulfillmentStatusChangeRequest request,
String originalStatus,
List<OrderFulfillment> splitFulfillments,
Order order,
@Nullable ContextInfo contextInfo) {
OrderFulfillment inTransit = getFulfillmentWithChangedStatus(request, splitFulfillments);
// (3)
sendFulfillmentStatusChangeEvent(messageProducer.fulfillmentInTransitOutput(), request,
originalStatus, inTransit, order, contextInfo);
return inTransit;
}
@Override
protected void sendFulfillmentStatusChangeEvent(MessageChannel channel,
FulfillmentStatusChangeRequest request,
String originalStatus,
OrderFulfillment changed,
Order order,
@Nullable ContextInfo contextInfo) {
FulfillmentStatusChangeEvent event =
getTypeFactory().get(FulfillmentStatusChangeEvent.class);
event.setRequest(request);
event.setOriginalStatus(originalStatus);
event.setFulfillment(changed);
event.setOrder(order);
event.setContextInfo(contextInfo);
// (4)
if (getProperties() != null && !"none".equals(getProperties().getProvider())
&& getDetachedDurableMessageSender() != null) {
getDetachedDurableMessageSender().send(event, FulfillmentInTransitProducer.TYPE,
changed.getId(),
RouteConstants.Persistence.ORDER_OP_ROUTE_KEY);
} else {
channel.send(MessageBuilder.withPayload(event)
.setHeaderIfAbsent(MESSAGE_IDEMPOTENCY_KEY, ULID.random(SECURE_RANDOM))
.build());
}
}
}