@Component("orderStateListener") @WithStateMachine(name = "orderStateMachine") public class OrderStateListenerImpl{ @OnTransition(source = "WAIT_PAYMENT", target = "WAIT_DELIVER") public boolean payTransition(Message<OrderStatusChangeEvent> message) { Order order = (Order) message.getHeaders().get("order"); order.setStatus(OrderStatus.WAIT_DELIVER); System.out.println("支付,状态机反馈信息:" + message.getHeaders().toString()); return true; } @OnTransition(source = "WAIT_DELIVER", target = "WAIT_RECEIVE") public boolean deliverTransition(Message<OrderStatusChangeEvent> message) { Order order = (Order) message.getHeaders().get("order"); order.setStatus(OrderStatus.WAIT_RECEIVE); System.out.println("发货,状态机反馈信息:" + message.getHeaders().toString()); return true; } @OnTransition(source = "WAIT_RECEIVE", target = "FINISH") public boolean receiveTransition(Message<OrderStatusChangeEvent> message){ Order order = (Order) message.getHeaders().get("order"); order.setStatus(OrderStatus.FINISH); System.out.println("收货,状态机反馈信息:" + message.getHeaders().toString()); return true; } }
6、创建IOrderService接口
1 2 3 4 5 6 7 8 9 10 11 12
public interface IOrderService { //创建新订单 Order create(); //发起支付 Order pay(int id); //订单发货 Order deliver(int id); //订单收货 Order receive(int id); //获取所有订单信息 Map<Integer, Order> getOrders(); }
@Service("orderService") public class OrderServiceImpl implements IOrderService {
@Autowired private StateMachine<OrderStatus, OrderStatusChangeEvent> orderStateMachine; @Autowired private StateMachinePersister<OrderStatus, OrderStatusChangeEvent, Order> persister; private int id = 1; private Map<Integer, Order> orders = new HashMap<>();
public Order create() { Order order = new Order(); order.setStatus(OrderStatus.WAIT_PAYMENT); order.setId(id++); orders.put(order.getId(), order); return order; }
public Order pay(int id) { Order order = orders.get(id); System.out.println("线程名称:" + Thread.currentThread().getName() + " 尝试支付,订单号:" + id); Message message = MessageBuilder.withPayload(OrderStatusChangeEvent.PAYED).setHeader("order", order).build(); if (!sendEvent(message, order)) { System.out.println("线程名称:" + Thread.currentThread().getName() + " 支付失败, 状态异常,订单号:" + id); } return orders.get(id); }
public Order deliver(int id) { Order order = orders.get(id); System.out.println("线程名称:" + Thread.currentThread().getName() + " 尝试发货,订单号:" + id); if (!sendEvent(MessageBuilder.withPayload(OrderStatusChangeEvent.DELIVERY).setHeader("order", order).build(), orders.get(id))) { System.out.println("线程名称:" + Thread.currentThread().getName() + " 发货失败,状态异常,订单号:" + id); } return orders.get(id); }
public Order receive(int id) { Order order = orders.get(id); System.out.println("线程名称:" + Thread.currentThread().getName() + " 尝试收货,订单号:" + id); if (!sendEvent(MessageBuilder.withPayload(OrderStatusChangeEvent.RECEIVED).setHeader("order", order).build(), orders.get(id))) { System.out.println("线程名称:" + Thread.currentThread().getName() + " 收货失败,状态异常,订单号:" + id); } return orders.get(id); }
public class PhaseId implements Comparable { private final int ordinal; private String phaseName; private static int nextOrdinal = 0; private static final String ANY_PHASE_NAME = "ANY"; public static final PhaseId ANY_PHASE = new PhaseId("ANY"); private static final String RESTORE_VIEW_NAME = "RESTORE_VIEW"; public static final PhaseId RESTORE_VIEW = new PhaseId("RESTORE_VIEW"); private static final String APPLY_REQUEST_VALUES_NAME = "APPLY_REQUEST_VALUES"; public static final PhaseId APPLY_REQUEST_VALUES = new PhaseId("APPLY_REQUEST_VALUES"); private static final String PROCESS_VALIDATIONS_NAME = "PROCESS_VALIDATIONS"; public static final PhaseId PROCESS_VALIDATIONS = new PhaseId("PROCESS_VALIDATIONS"); private static final String UPDATE_MODEL_VALUES_NAME = "UPDATE_MODEL_VALUES"; public static final PhaseId UPDATE_MODEL_VALUES = new PhaseId("UPDATE_MODEL_VALUES"); private static final String INVOKE_APPLICATION_NAME = "INVOKE_APPLICATION"; public static final PhaseId INVOKE_APPLICATION = new PhaseId("INVOKE_APPLICATION"); private static final String RENDER_RESPONSE_NAME = "RENDER_RESPONSE"; public static final PhaseId RENDER_RESPONSE = new PhaseId("RENDER_RESPONSE"); private static final PhaseId[] values; public static final List VALUES;