Activiti Series (i)--The simplest Activiti program

Source: Internet
Author: User

Introduction


Activiti the working engine, in my words: the simple framework of separating business processes from system processes.


Someone asked, we actually use a few tables of the database, we can implement, submit our information to the designated next person. Why do we have to use Activiti? This problem, in fact, is very simple, activiti our business process and system process separation, simply speaking, we just need to submit our information on the line, do not need to know who is the approver. Such decoupling and.


not much to say, on the code:


First look at our file directory:




One, the reference jar package:


Among the more important packages:




second, to draw a BPMN file:



three, fill in the parameters of each node:


We write "Zhang San" in the assignee in Properties--main config that submits the request, for example:





In the same way, we write "John Doe" in Properties--main Config--assiginee in the Approval Department manager node.


In the same way, we write "Harry" in Properties--main Config--assiginee in the "Approval" manager "node.


Iv. Set the ID of the process:


After writing it, we click on the blank BPMN file in the blank place,



v. Writing XML and Classes


Write Activiti.cfg.xml file:


<span style= "FONT-SIZE:18PX;" ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/ Schema/context "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPRING-CONTEXT-2.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.0.xsd "><bean id=" processengineconfiguration "class=" Org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration "><!--connection Data configuration--><property name=" Jdbcdriver "value=" Com.mysql.jdbc.Driver "></property><property name=" Jdbcurl "value=" jdbc:mysql:// Localhost:3306/itcast0711activiti?useunicode=true&characterencoding=utf8 "></property><property Name= "JdbcusernAme "value=" root "></property><property name=" Jdbcpassword "value=" root "></property><!-- No table created table--><property name= "Databaseschemaupdate" value= "true" ></property></bean></beans ></span>

To add a log4j.properties file:


<span style= "FONT-SIZE:18PX;" >log4j.rootlogger=info, ca# consoleappenderlog4j.appender.ca= org.apache.log4j.consoleappenderlog4j.appender.ca.layout= Org.apache.log4j.patternlayoutlog4j.appender.ca.layout.conversionpattern=%d{hh:mm:ss,sss} [%t]%-5p%c%x-%m%n< /SPAN>


Write HelloWorld class:


<span style= "FONT-SIZE:18PX;" >package Cn.itcast.a_helloworld;import Java.util.list;import Org.activiti.engine.processengine;import Org.activiti.engine.processengines;import Org.activiti.engine.repository.deployment;import Org.activiti.engine.runtime.processinstance;import Org.activiti.engine.task.task;import Org.junit.Test;public Class HelloWorld {Processengine Processengine = Processengines.getdefaultprocessengine ();/** deployment Process definition */@Testpublic void Deploymentprocessdefinition () {Deployment Deployment = Processengine.getrepositoryservice ()// Service.createdeployment ()//Create a Deployment object associated with the process definition and Deployment object. Name ("HelloWorld starter Program")//Add the name of the deployment. Addclasspathresource (" DIAGRAMS/HELLOWORLD.BPMN ")//loaded from CLASSPATH resources, only one file can be loaded at a time. Addclasspathresource (" diagrams/helloworld.png ")// Load from Classpath resources, only one file can be loaded at a time. deploy ();//Complete Deployment SYSTEM.OUT.PRINTLN ("Deployment ID:" +deployment.getid ());// 1SYSTEM.OUT.PRINTLN ("Deployment Name:" +deployment.getname ()),//helloworld Starter program}/** START Process instance */@Testpublic void Startprocessinstance () {//keystring Processdefiniti for process definitionOnKey = "HelloWorld"; ProcessInstance pi = Processengine.getruntimeservice ()//Service.startprocessinstancebykey associated with the executing process instance and the execution object ( Processdefinitionkey);//Use process-defined key to start the process instance,    Key corresponds to the attribute value of the ID in the HELLOWORLD.BPMN file, starts with the key value, and by default launches System.out.println ("Process instance ID:" +pi.getid ()) According to the latest version of the process definition;//Process Instance ID 101SYSTEM.OUT.PRINTLN ("Process definition ID:" +pi.getprocessdefinitionid ());//process definition ID helloworld:1:4}/** query The current person's personal task */@Testpublic void Findmypersonaltask () {String assignee = "Harry"; list<task> list = Processengine.gettaskservice ()//Service.createtaskquery related to the task management being performed ()// Creates a task query object. Taskassignee (Assignee)//Specify Personal task query, specify the person who handles. List (); if (List!=null && list.size () >0) {for (Task task: List) {System.out.println ("task ID:" +task.getid ()); SYSTEM.OUT.PRINTLN ("Task Name:" +task.getname ()); SYSTEM.OUT.PRINTLN ("Task Creation Time:" +task.getcreatetime ()); SYSTEM.OUT.PRINTLN ("The task of the person:" +task.getassignee ()); SYSTEM.OUT.PRINTLN ("Process instance ID:" +task.getprocessinstanceid ()); System.out.println ("Execution object ID:" +task.getexecutionid ()); SYSTEM.OUT.PRINTLN ("Process definition ID:" +task.getprocessdefiniTionid ()); System.out.println ("########################################################");}}} /** Complete My task */@Testpublic void Completemypersonaltask () {//task idstring taskId = "302";p rocessengine.gettaskservice ()// Service.complete (TASKID) related to the task management being performed; SYSTEM.OUT.PRINTLN ("Complete task: Task ID:" +taskid);}} </span>

Note:


The first method Deploymentprocessdefinition method is used to deploy the process, the deployment process means that we have newly written an instance of a workflow, run the Deploymentprocessdefinition method, and the workflow instance will be deployed. Deployment takes only one time.


The second method, Startprocessinstance (), is the way to start the process instance, where the processdefinitionkey must be consistent with our previous Properties--process--id, Because this processdefinitionkey is based on this value to find the instance of the deployment process.


The third method Findmypersonaltask () looks for personal tasks, the assignee we need here is when we are drawing, the names in Properties--main Config--assignee are consistent, and the personal tasks of "Harry" are viewed here , that is, when the Zhang San submitted the application, assignee for "John Doe" time, will appear John Doe task, John Doe complete the task, put to Harry, this time, Harry will appear to have a task.


The fourth Method Completemypersonaltask () Complete the task, this parameter is TaskID, that is, the third method is now queried whether there is a task, when there is a task, we will be the task ID parameters, run this method task will be completed.











Activiti Series (i)--The simplest Activiti program

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.