Use a custom task parser

Source: Internet
Author: User
Sometimes we want to bind a global listener to every task in activiti, but we cannot manually add listeners to the task every time (too tedious ), therefore, a custom task parser is used.
First look at the activiti configuration, so that you can use a custom task parser to replace activiti's default task Parser (the principle can be self-query source code)
<Bean id = "processengineconfiguration" class = "org. activiti. spring. springprocessengineconfiguration "> <property name =" datasource "ref =" datasource "/> <property name =" transactionmanager "ref =" transactionmanager "/> <property name =" databaseschemaupdate "value = "True"/> <property name = "jobexecutoractivate" value = "true"/> <! -- Added custom parser, used to bind the listener to the task --> <property name = "customdefaultbpmnparsehandlers"> <list> <Bean class = "me. lb. activiti. parser. customusertaskparsehandler "/> </List> </property>... </bean>

Next is the implementation of the task parser.
Package me. lb. activiti. parser; import Java. util. arraylist; import Java. util. collection; import Java. util. hashset; import Java. util. list; import Java. util. set; import Org. activiti. BPMN. model. activitilistener; import Org. activiti. BPMN. model. baseelement; import Org. activiti. BPMN. model. fieldextension; import Org. activiti. BPMN. model. implementationtype; import Org. activiti. BPMN. model. usertask; import Org. activiti. E Ngine. delegate. tasklistener; import Org. activiti. engine. impl. BPMN. parser. bpmnparse; import Org. activiti. engine. impl. BPMN. parser. handler. usertaskparsehandler; import Org. activiti. engine. impl. task. taskdefinition; import Org. activiti. engine. parse. bpmnparsehandler; public class customusertaskparsehandler implements bpmnparsehandler {public collection <class <? Extends baseelement> gethandledtypes () {// this method is called by activiti settings and compared with the built-in task parser. If the type is the same, will replace the default task parser set <class <? Extends baseelement> types = new hashset <class <? Extends baseelement >>> (); types. add (usertask. class); return types;} public void parse (bpmnparse, baseelement) {// call the default task parser new usertaskparsehandler (). parse (bpmnparse, baseelement); taskdefinition = (taskdefinition) bpmnparse. getcurrentactivity (). getproperty (usertaskparsehandler. property_task_definition); // custom task parsing (add listener) activitilistener custom = new activitilistener (); // bind listener event custom. setevent (tasklistener. eventname_create); // sets the listener implementation class custom. setimplementationtype (implementationtype. implementation_type_class); custom. setimplementation ("me. lb. activiti. listener. customtasklistener "); // set the attribute list to be injected <fieldextension> FeS = new arraylist <fieldextension> (); fieldextension field_test = new fieldextension (); field_test.setfieldname (" test "); field_test.setstringvalue ("test: 1: 100"); FES. add (field_test); custom. setfieldextensions (FES); // binds the listener to the task taskdefinition. addtasklistener (tasklistener. eventname_create, bpmnparse. getlistenerfactory (). createclassdelegatetasklistener (custom ));}}

The last is a global listener for testing.
Package me. lb. activiti. parser; import Java. util. arraylist; import Java. util. collection; import Java. util. hashset; import Java. util. list; import Java. util. set; import Org. activiti. BPMN. model. activitilistener; import Org. activiti. BPMN. model. baseelement; import Org. activiti. BPMN. model. fieldextension; import Org. activiti. BPMN. model. implementationtype; import Org. activiti. BPMN. model. usertask; import Org. activiti. E Ngine. delegate. tasklistener; import Org. activiti. engine. impl. BPMN. parser. bpmnparse; import Org. activiti. engine. impl. BPMN. parser. handler. usertaskparsehandler; import Org. activiti. engine. impl. task. taskdefinition; import Org. activiti. engine. parse. bpmnparsehandler; public class customusertaskparsehandler implements bpmnparsehandler {public collection <class <? Extends baseelement> gethandledtypes () {// this method is called by activiti settings and compared with the built-in task parser. If the type is the same, will replace the default task parser set <class <? Extends baseelement> types = new hashset <class <? Extends baseelement >>> (); types. add (usertask. class); return types;} public void parse (bpmnparse, baseelement) {// call the default task parser new usertaskparsehandler (). parse (bpmnparse, baseelement); taskdefinition = (taskdefinition) bpmnparse. getcurrentactivity (). getproperty (usertaskparsehandler. property_task_definition); // custom task parsing (add listener) activitilistener custom = new activitilistener (); // bind listener event custom. setevent (tasklistener. eventname_create); // sets the listener implementation class custom. setimplementationtype (implementationtype. implementation_type_class); custom. setimplementation ("me. lb. activiti. listener. customtasklistener "); // set the attribute list to be injected <fieldextension> FeS = new arraylist <fieldextension> (); fieldextension field_test = new fieldextension (); field_test.setfieldname (" test "); field_test.setstringvalue ("test: 1: 100"); FES. add (field_test); custom. setfieldextensions (FES); // binds the listener to the task taskdefinition. addtasklistener (tasklistener. eventname_create, bpmnparse. getlistenerfactory (). createclassdelegatetasklistener (custom ));}}

At this point, we can add custom global listeners to any task. Of course, there are many ways to use the task parser, not limited to global listeners, this depends on the actual situation.

Use a custom task parser

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.