(4) activiti workflow engine uel expression, activitiuel

Source: Internet
Author: User

(4) activiti workflow engine uel expression, activitiuel

With the previous chapters, we are certainly confused. How can activiti be integrated with actual business, such as a purchase order, and how can it be associated with a process instance?

Here we need to use activiti to set the businessKey of a process instance when starting the process instance (usually store the id of one of our purchase orders)

1. Start the process instance and set its businessKey

/*** Start a process instance and set its Business id */@ Test public void startProInsWithKey () {RuntimeService runtimeService = engine. getRuntimeService (); String processDefinitionKey = "purchasingflow"; // set a businessKey, which may be a purchase order or an id String businessKey such as an order in my actual business = "111 "; // start a process instance ProcessInstance processInstance = runtimeService according to the key defined in the process. startProcessInstanceByKey (processDefinitionKey, businessKey); System. out. println ("process instance id:" + processInstance. getId (); System. out. println ("process definition id:" + processInstance. getProcessDefinitionId ());}

Generally, when a user saves a purchase order, we start the instance and dynamically obtain the id of the purchase order (that is, the businessKey used as the process instance ), in addition, we will save the id of this process instance in the purchase order table and bind it one-to-one in two directions to facilitate business query.

2. dynamically query the corresponding process instance based on the purchase order id (that is, the businessKey used as the process instance ).

/*** Query the process instance through businessKey */@ Test public void queryProInsWithKey () {RuntimeService runtimeService = engine. getRuntimeService (); String businessKey = "111"; ProcessInstanceQuery instanceQuery = runtimeService. createProcessInstanceQuery (); // define the key and Business id businessKey according to the process to query the corresponding process instance. Generally, there is only one instanceQuery. processInstanceBusinessKey (businessKey); // query the unique 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 have learned how to bind a process instance with actual business data.

 

We have started a lot of process instances here. We found that all our task handlers are written to zhangsan, lisi, etc. Can we dynamically specify them, this requires the use of our uel expression.

First, what is the uel expression?

UEL is part of the java EE6 specification (Uniied Expression Language) Is a unified expression language. activiti supports two UEL expressions: UEL-value and UEL-method. We will introduce them separately.

(1) Let's first demonstrate uel-value

First, let's get started with a simple application.

Usage steps: (run the following code without understanding it and go back again)

1. Set the handler expression $ {assignee} without specifying the handler id at the task node, and redeploy the process definition.

2. To start a process instance, set the startup variable to dynamically set the value of assignee.

3. check and verify whether the handler of the next task is set dynamically for us.

 

1. First, set the node

2. Set the value of assignee dynamically when starting the code.

/*** Set the global variable when starting the process instance */@ Test public void startProInsWithArgs () {RuntimeService runtimeService = engine. getRuntimeService (); String processDefinitionKey = "purchasingflow"; // you can set the global variable parameter to be started. The value can be a javabean, a common String, or a Map. <String, object> variables = new HashMap <String, Object> (); variables. put ("assignee", "feige"); // set the parameter to start ProcessInstance processInstance = runtimeService when the process starts. startProcessInstanceByKey (processDefinitionKey, variables); System. out. println ("process instance id:" + processInstance. getId (); System. out. println ("process definition id:" + processInstance. getProcessDefinitionId ());}

3. In this case, you can view the database, act_ru_task, and

From the above, we can see that the process has arrived. Create a purchase order and the assignee processor is feige.

This is because when we create a purchase order, we will find the value named assignee from the global variable of the current process instance. If so, we will assign the value of assignee to this node, if we set it dynamically, but go to the node (Why do we say it before the node, because we can set variables at other times, which will be discussed later)

If the value of this assignee variable is not found, an error is returned. You can try it yourself.

 

 

Of course, apart from the above setting method, do we have other setting methods to dynamically set the value of its node? The answer is yes. The value of the parameter during the startup process can be not only the String type, but also the Object (serialized), Map, List, and Array.

Here we use objects for demonstration. The procedure is as follows:

1. Set the dynamic value of the first node of the process to $ {user. userId}. By default, the user will find the getUserId value of the corresponding value of the variable name as user, and redeploy the process definition.

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

3. Check whether the handler of the current task on this node is the value of the userId variable of our user.

 

1. Set nodes

 

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

Public class User implements Serializable {/*** for serialization */private static final long serialVersionUID = 771710974223077256l; 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 process: Set a user to the global variable */@ Test public void startProInsWithObj () {RuntimeService runtimeService = engine. getRuntimeService (); String processDefinitionKey = "purchasingflow"; // set the global variable parameter it starts, and set its value to the user object, here, the dead User user = new User ("101", "male", "zhangsan"); Map <String, Object> variables = new HashMap <String, Object> (); variables. put ("user", user); // set the parameter to start ProcessInstance processInstance = runtimeService when the process starts. startProcessInstanceByKey (processDefinitionKey, variables); System. out. println ("process instance id:" + processInstance. getId (); System. out. println ("process definition id:" + processInstance. getProcessDefinitionId ());}

3. The process instance is started successfully. Observe the current task table of the database and check whether the handler of the first node is the userId of 101 we set.

 

Set successfully!

 

(2) demonstration of uel-method

Procedure

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

2. Inject the method into the bean of activiti's processEngineConfiguration (in our activiti. cfg. xml)

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

 

Okay, go directly to the code

1. Set nodes and redeploy the Process Definition

 

2. java method of method and activiti. cfg. xml injection Configuration

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 policy --> <property name = "databaseSchemaUpdate" value = "true"/> <! -- Multiple classes can be injected into activiti beans, key corresponds to our class name --> <property name = "beans"> <map> <entry key = "commonmethod" value-ref = "commonmethod"/> </ map> </property> </bean>

3. Start a process instance

/*** Obtain the value based on the method */@ Test public void startProInsWithMethod () {RuntimeService runtimeService = engine. getRuntimeService (); String processDefinitionKey = "purchasingflow"; // If userId is set to 8, the first node searches for the getUserNameByUserId method of the commonmethod we injected and passes the userId Map <String, object> variables = new HashMap <String, Object> (); variables. put ("userId", 8); // set the parameter to start ProcessInstance processInstance = runtimeService when the process starts. 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 is successfully started, and the current task handler of the process is acitiviti8. In actual applications, because they are all spring-managed classes, we can assign values to various queries, very convenient!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.