(4) uel expression of activiti

Keywords: Java Database Spring xml

With the previous chapters, we must have some confusion about how activiti integrates with the actual business, such as a purchase order, and how it relates to a process instance.

Here you need to set up a Business Key for a process instance when activiti starts the process instance (which typically stores the ID of a purchase order)

1. Start the process instance to set its businessKey

/**
     * Start a process instance and set its business id
     */
    @Test
    public void startProInsWithKey() {
        RuntimeService runtimeService = engine.getRuntimeService();

        String processDefinitionKey = "purchasingflow";
        //Set up a businessKey,In my actual business, it might be a purchase order, or an order, or something like that. id
        String businessKey ="111";
        // Process-defined key Start a process instance
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,businessKey);
        System.out.println("Process instance id:" + processInstance.getId());
        System.out.println("Process definition id:" + processInstance.getProcessDefinitionId());

    }

Normally, when the user saves a purchase order, we start the instance and dynamically acquire the id of the purchase order (i.e. the business key used as the process instance), and we also save the id of the process instance in the purchase order form, bind one-to-one in two directions, so as to facilitate business query.

2. According to the id of the purchase order (i.e. the business key used as a process instance), the corresponding process instance is dynamically queried.

/**
     * Query process instances through businessKey
     */
    @Test
    public void queryProInsWithKey(){
        RuntimeService runtimeService = engine.getRuntimeService();
        String businessKey ="111";
        
        ProcessInstanceQuery instanceQuery = runtimeService.createProcessInstanceQuery();
        //According to its process definition key And business id businessKey Query out the corresponding process instance, usually only one
        instanceQuery.processInstanceBusinessKey(businessKey);
        //Query out the only process instance
        ProcessInstance processInstance = instanceQuery.singleResult();
        
        System.out.println("Process instance id:"+processInstance.getId());
        System.out.println("Process definition id:"+processInstance.getProcessDefinitionId());
        
    }

At this point, we are clear about the binding of a process instance to the actual business data.

 

We have also started a lot of process examples here, and found that our task managers are all written to death as zhangsan, lisi and so on, so can we dynamically specify it, which requires the use of our uel expression?

What is the uel expression in the first place?

UEL is part of the java EE6 specification. UEL (Unified Expression Language) is the Unified Expression Language. Activity supports two UEL expressions: UEL-value and UEL-method. We will introduce them separately.

(1) Let's demonstrate uel-value first.

Let's start with a simple usage to get you started.

Use steps: (Understand running the following code to go over and look back)

1. At the node of the task, the id of the handler is not specified directly, the handler expression ${assignee} is set, and the process definition is redeployed.

2. Start a process instance by setting the startup variable to dynamically set the assignee value

3. Check and verify whether the next task handler dynamically sets up for us

 

1. First, we set up the nodes.

2. Set assignee value dynamically when starting code

/**
     * Setting global variables when starting process instances
     */
    @Test
    public void startProInsWithArgs(){
        
        RuntimeService runtimeService = engine.getRuntimeService();

        String processDefinitionKey = "purchasingflow";
        //Set the global variable parameter for its startup, and value Could be javabean,Or it could be a normal string, a number.
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("assignee", "feige");
        //Set parameters to start when setting process startup
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,variables);
        System.out.println("Process instance id:" + processInstance.getId());
        System.out.println("Process definition id:" + processInstance.getProcessDefinitionId());
        
    }

3. At this point, we look at the database to see the tasks currently running by act_ru_task.

From the above we can see that the process has come to create purchase orders, and the assignee for purchasing handles artificial feige.

Because when we go to create the purchase order, we will find the value name d assignee from the global variable of the current process instance. If we query, we assign the value of the assignee to the node. If we set it dynamically, we will go to the node (why do we say before the node, because we can set variables at other times, as will be discussed in the next chapter).

If you don't find the value of this assignee variable, you will get an error. You can try it for yourself.

 

 

Of course, in addition to the above settings, do we have other settings to dynamically set the values of its nodes? The answer is yes, the value of the parameter at the start of the process can be not only String type, but also Object object (serialized), Map,List,Array.

Here we use the object to do the demonstration. The steps are as follows.

1. Setting the dynamic value of the first node of the process to be ${user.userId}, he defaults to find the getUserId value of the corresponding value of the variable name as user, and redeploys the process definition.

2. When starting the process, set the javabean of the user to the global variable of the process

3. See if the handler of the current task that goes to this node is the value of the userId variable of our user?

 

1. Setting up nodes

 

2. Set up the javabean User object and set it in when the process instance starts

public class User implements Serializable{
    /**
     * For serialization
     */
    private static final long serialVersionUID = 7717000074223077256L;
    private String userId;
    private String sex;
    private String name;
    
    public User(String userId, String sex, String name) {
        super();
        this.userId = userId;
        this.sex = sex;
        this.name = name;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
/**
     * Start the process to set a user to the global variable
     */
    @Test
    public void startProInsWithObj(){
        
        RuntimeService runtimeService = engine.getRuntimeService();

        String processDefinitionKey = "purchasingflow";
        
        //Set the global variable parameter for its startup, and value Set to user Object, write dead here
        User user = new User("101","male","Zhang San");
        
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("user", user);
        //Set parameters to start when setting process startup
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,variables);
        System.out.println("Process instance id:" + processInstance.getId());
        System.out.println("Process definition id:" + processInstance.getProcessDefinitionId());
        
    }

3. The process instance was successfully started here. We observed the current task table of the database and whether the handler of the first node set 101 userId for us.

 

Successful setup!

 

(2) Demonstrating uel-method

Execution steps

1. Set the executor of the node to ${method.getUserNameByUserId(userId)}, where the method method method is a class we inject into spring, and userId is the global variable we set.

2. Inject the method method method into the process Engine Configuration bean of activiti (in our activiti.cfg.xml)

3. Start a process to set the global variable userId as the startup parameter to see if the handler who reaches this node is the name returned by our method getUserNameByUserId

 

Okay, go straight to the code.

1. Setting up nodes and redeploying process definitions

 

2. Method's java method and the injection configuration of activiti.cfg.xml

public class CommonMethod{

    public String getUserNameByUserId(int userId){
        
        return "activiti"+userId;
        
    }

}
<bean class="cn.nfcm.po.CommonMethod" id="commonmethod"></bean>
    
    <bean id="processEngineConfiguration"
        class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- data source -->
        <property name="dataSource" ref="dataSource" />
        <!-- activiti Database Table Processing Strategy -->
        <property name="databaseSchemaUpdate" value="true" />
        <!-- Multiple classes can be injected to activiti Of beans Among them key That corresponds to our class name. -->
        <property name="beans">
            <map>
                <entry key="commonmethod" value-ref="commonmethod" />
            </map>
        </property>
    </bean>

3. Start a process instance

/**
     * Get the value according to the method
     */
    @Test
    public void startProInsWithMethod(){

        RuntimeService runtimeService = engine.getRuntimeService();

        String processDefinitionKey = "purchasingflow";
        //Set up here userId For 8, walking to the first node will find what we injected into it. commonmethod Of getUserNameByUserId Method and delivery userId
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("userId", 8);
        
        //Set parameters to start when setting process startup
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,variables);
        System.out.println("Process instance id:" + processInstance.getId());
        System.out.println("Process definition id:" + processInstance.getProcessDefinitionId());
        
        
    }

Here we will find that the process started successfully, and the current task of the process is handled by human acitiviti8. In practical application, because they are all spring management classes, we can carry out a variety of query assignment, which is very convenient! ________

Posted by rs_nayr on Sat, 30 Mar 2019 00:45:29 -0700