[State Machine] Enterprise Available Level Development Guide for spring statemachine 5 - message to pass parameters

Keywords: Programming Spring Java Database

Category Column: spring statemachine
Article Tag: spring boot spring Statemachine Java state machine
Copyright Statement: This is an original blogger article, which follows the CC 4.0 BY-SA copyright agreement. Please attach a link to the original source and this statement for reproduction.
Link to this article: https://blog.csdn.net/firebat/article/details/89886975
Receive
Data transfer between different businesses is the most common task in enterprise development, so although our main architecture uses a state machine, that is, to view the project from the perspective of process state, in specific businesses, each state change involves a variety of businesses, some of which need to be notified of state machine changes and need to pass status values to the industryBusiness Classes and Business Methods, similarly, in dealing with state changes, also need to obtain business data to facilitate different businesses to do their own business in the same state change link. Let's talk about this data transfer in spring statemachine.

This time our order changes, starting with an external incoming order number to the controller:

@RequestMapping("/testOrderState")
    public void testOrderState(String orderId) throws Exception {
 
        StateMachine<OrderStates, OrderEvents> stateMachine = orderStateMachineBuilder.build(beanFactory);
        System.out.println(stateMachine.getId());
 
        // Create process
        stateMachine.start();
 
        // Trigger PAY Event
        stateMachine.sendEvent(OrderEvents.PAY);
 
        // Trigger RECEIVE Event
        Order order = new Order(orderId, "547568678", "Shenzhen, Guangdong Province", "13435465465", "RECEIVE");
        Message<OrderEvents> message = MessageBuilder.withPayload(OrderEvents.RECEIVE).setHeader("order", order).build();
        stateMachine.sendEvent(message);
 
        // Get Final State
        System.out.println("Final State:" + stateMachine.getState().getId());
    }


controller receives the parameter of the request, orderId, and then the state machine triggers the event in turn. By the time the RECEIVE event is triggered, we create a new Order and insert the orderId. In fact, more often than not, we get the orderId, then query the database to get the orderData Object. Here, to simplify the code, we create a new Order.

Then the real hero comes on, Message.It is not really exclusive to spirng statemachine. It is a common messaging tool in spring. Look at its source code:

package org.springframework.messaging;
 
public interface Message<T> {
 
    /**
     * Return the message payload.
     */
    T getPayload();
 
    /**
     * Return message headers for the message (never {@code null} but may be empty).
     */
    MessageHeaders getHeaders();
 
}


It consists of two parts, you can see from the picture, and it's consistent with the code

Inside the spring statemachine, we plug the status into the payload of the message, and then the business data that needs to be passed (in the example, the order object) into the header.The message builder was used to create the message. Looking at its name, you know it was created specifically for the message.

Message<OrderEvents> message = MessageBuilder.withPayload(OrderEvents.RECEIVE).setHeader("order", order).build();
        stateMachine.sendEvent(message);

When a message is created, the state machine sendEvent can send more than just one event, and it can be combined with event (OrderEvents.RECEIVE) and data content (order) to the state machine change processing class eventconfig.Let's look at the processing of eventConfig:

/**
     * WAITING_FOR_RECEIVE->DONE Actions performed
     */
    @OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
    public void receive(Message<OrderEvents> message) {
        System.out.println("Parameters passed:" + message.getHeaders().get("order"));
        logger.info("---User receives, order complete---");
    }

First, the parameter of the receive method is null from the previous one:

public void receive() {
        logger.info("---User receives, order complete---");
}

Change to Message <OrderEvents> message so that you can retrieve the passed data object from the message's getHeaders.

What if we need to transfer more than one data object? For example, in our actual business, in addition to order data, we may also need to transfer commodity data, or payment results data, so it's easy. We start from the controller:

Message<OrderEvents> message = MessageBuilder.withPayload(OrderEvents.RECEIVE).setHeader("order", order).setHeader("otherobj", "otherobjvalue").build();

Just continue setHeader later, and go inside eventConfig:

System.out.println("Parameters passed:" + message.getHeaders().get("order"));
System.out.println("Parameters passed:" + message.getHeaders().get("otherObj"));

Look at the log after running:

Parameters passed: Order [id=null, userId=547568678, address=Shenzhen, Guangdong Province, phoneNum=13435465465, state=RECEIVE]
Parameter passed: otherObjValue

Knowing that both data are passed into eventConfig, this allows multiple data objects to be passed at the same time.

So far, the state machine has made data connections to other business code through the message object.In fact, this is very important, only to achieve data transfer with other businesses, can it be truly available.

In the next chapter we will continue with the persistence problem of state machines and how to start creating state machines in a non-starting state

Code Cloud Companion Code Address
--------
Copyright Notice: This is an original article by CSDN blogger "wphmoon123", which follows the CC 4.0 BY-SA copyright agreement. Please attach a link to the original source and this statement.
Original link: https://blog.csdn.net/firebat/java/article/details/89886975

Posted by a2bardeals on Sun, 19 Apr 2020 19:08:25 -0700