"Code piece" JBPM4.4 Development process node (dynamic agent implementation process management business)

Source: Internet
Author: User
Tags jbpm

following the previous blog, "thought article" Workflow Technology JBPM4.4 Development Introduction (iv) , "thought article" Workflow Technology JBPM4.4 Development Introduction (v) This blog to combine the code to simply say, how to let the process to manage the business:

Let's take a look at the proxy class we pumped out:

STARTABSTRACTJBPM: Process Start node

Package Com.hjy.ssh.action;import Com.hjy.ssh.beans.abstractapply;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Javax.annotation.resource;import Org.jbpm.api.processinstance;import Org.springframework.context.annotation.scope;import Org.springframework.stereotype.controller;import Java.lang.reflect.invocationhandler;import Java.util.map;import Com.hjy.ssh.service.jbpmservice;import Com.opensymphony.xwork2.ActionSupport, @Controller @scope ("prototype") public class STARTABSTRACTJBPM extends Actionsupport {private static final long Serialversionuid = 1l;//defines an attribute variable private map<string, object> Variablesmap; private string pdkey;protected jbpmservice jbpmservice;public void Common (String pdkey,map<string, object> Variablesmap,jbpmservice jbpmservice) {this.variablesmap=variablesmap;this.pdkey=pdkey;this.jbpmservice= Jbpmservice;} If you want to try to pass the argument in other ways, the new word is too resource-intensive/*public startabstractjbpm (String pdkey,map<string, object> Variablesmap, Jbpmservice jbpmservice) {this.Variablesmap=variablesmap;this.pdkey=pdkey;this.jbpmservice=jbpmservice;} *///Dynamic proxy classes can only proxy interfaces (not supported by abstract classes), proxy classes need to implement the Invocationhandler class to implement the Invoke method.        The Invoke method is called when calling all methods of the Proxied interface, and the value returned by the Invoke method is an implementation class public class LogHandler1 implements of the proxy interface invocationhandler{      Target object Private Object TargetObject;                  The Invoke method is executed when a binding relationship, that is, which method is associated to which interface (binding to the concrete implementation class) is called.          public Object Newproxyinstancestart (object targetObject) {this.targetobject=targetobject; This method is used for specifying the class loader, a set of interfaces, and invoking the processor to generate a dynamic proxy class instance//The first parameter specifies the class loader that produces the proxy object, which needs to be specified as the same class loader as the target object//The second parameter is to implement the same interface as the target object, so it only needs Get the implementation interface of the target object//The third parameter indicates which Invocationhandler invoke method is required to execute the intercepted method when it is intercepted//returns a proxy object based on the incoming target return Pro Xy.newproxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). GetInterfaces (), Thi      s); } @Override//The method of the associated implementation class will be executed when the method is invoked//Invocationhandler interface, proxy represents the method that represents the original object being called, and args represents the method's parameter P Ublic Object Invoke (OBJECT Proxy, method, object[] args) throws Throwable {System.out.println ("start-->>");          for (int i=0;i<args.length;i++) {System.out.println (args[i]);          } Object Ret=null;                                 try{//System.out.println ("satrt-->>") to process log information before the original object method call; Start Process ProcessInstance pi= (processinstance) Jbpmservice.startprocessinstancebykey (pdkey,variablesmap                        );            Call target method abstractapply abstractapply= (abstractapply) args[0];            Abstractapply.setexecuteid (Pi.getid ());            args[0]=abstractapply;                          Ret=method.invoke (TargetObject, args);                  Call completion current node//>> Finish 1th Task "Submit Application" Jbpmservice.completefirsttask (PI);          The original object method is called after processing log information System.out.println ("success-->>");        }catch (Exception e) {e.printstacktrace ();      System.out.println ("error-->>");          Throw e;      } return ret; }}}

Handleabstractjbpmaction: Task Management node


Package Com.hjy.ssh.action;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Javax.annotation.resource;import Org.jbpm.api.processinstance;import Java.lang.reflect.invocationhandler;import Com.hjy.ssh.service.jbpmservice;public Abstract class Handleabstractjbpmaction<t> extends modeldrivenbaseaction<t> {protected String outcome;//Branch protected string taskid;//task idprotected Boolean approval ;//whether agree @resourceprotected jbpmservice jbpmservice;//Dynamic proxy class can only proxy interface (does not support abstract classes), proxy classes need to implement the Invocationhandler class, implement the Invoke method. The Invoke method is called when calling all methods of the Proxied interface, and the value returned by the Invoke method is an implementation class of the proxy interface, public class Loghandler implements invocationhandler{/      /target object private object TargetObject;                  The Invoke method is executed when a binding relationship, that is, which method is associated to which interface (binding to the concrete implementation class) is called.          public Object Newproxyinstance (object targetObject) {this.targetobject=targetobject; This method is used for specifying the class loader, a set of interfaces, and invoking the processor to generate a dynamic proxy class instance//The first parameter specifies the class loader that produces the proxy object, which needs to be specified as the same class loader as the target object//The second parameter is to implement the same interface as the target object, so it only needs Get the target object.Implementing the interface///The third parameter indicates which Invocationhandler invoke method is required to execute the intercepted method when it is intercepted//returns a proxy object based on the incoming target return PROXY.NEWP      Roxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). Getinterfaces (), this);      } @Override//The method of the associated implementation class will be executed when the method is called//Invocationhandler interface, proxy represents the agent, method represents the methods that the original object was called, and args represents the parameters of the method public object invoke (object proxy, Method method, object[] args) throws Throwable {System.out.print          ln ("start-->>");          for (int i=0;i<args.length;i++) {System.out.println (args[i]);          } Object Ret=null;                         try{//System.out.println ("satrt-->>") to process log information before the original object method call;                 Save processing information//abstractapprove ();    2, handle the current task, call complete the current node ProcessInstance pi=jbpmservice.completetask (taskId, outcome); Invokes the action of the workflow if (approval==false) {if (pi! = null) {//If the process has not ended//ends the current process Jbpmservice.endproCessinstance (PI);                          }}//Call the target method Ret=method.invoke (TargetObject, args);                        Call the workflow, each implementation of such an interface can be, to determine whether to modify the Isend (PI);          The original object method is called after processing log information System.out.println ("success-->>");              }catch (Exception e) {e.printstacktrace ();              System.out.println ("error-->>");          Throw e;      } return ret; }}//abstract method, implement save processing information, and set consent to disagree, but do not need to update protected abstract void Abstractapprove () throws exception;//abstract method, if it is the last node and agreed, Then the data table that needs to be updated protected abstract void Isend (ProcessInstance pi);//-----------Public String getoutcome () {return outcome;} public void Setoutcome (String outcome) {this.outcome = outcome;} Public String GetTaskID () {return taskId;} public void Settaskid (String taskId) {this.taskid = taskId;} public Boolean isapproval () {return approval;} public void Setapproval (Boolean approval) {this.approval = approval;}}


Note: The above agent uses two methods to pass the value, because Java multi-inheritance is not supported, so the first way is better, but the second way to pass the value is more simple, we can choose according to the need!

These two are abstract out of the proxy class, then what role does it play?

Let's take a look at their applications:

corresponding boot node:

/** submit the application, start the workflow-think of it as the host program */public String edit () throws Exception {Long stucourseid=model.getid ();//Submit application//package application information, Student's application information stucourseapply stucourseapply = new stucourseapply (); Stucourseapply.setstucourseid (Stucourseid); newCourse= New String (Newcourse.getbytes ("iso-8859-1"), "Utf-8"), Newteacher=new string (newteacher.getbytes ("iso-8859-1"), " Utf-8 "); Stucourseapply.setapplicant (Getcurrentuser ()); Applicant, Current User Stucourseapply.setoldcourse (Model.getcourse ()); Stucourseapply.setnewcourse (Newcourse); Stucourseapply.setnewteacher (Newteacher); Stucourseapply.setoldteacher (Model.getteacher ()); Stucourseapply.settitle ("Modify course information"); String Processdefinitionkeystr=new string (processdefinitionkey.getbytes ("iso-8859-1"), "Utf-8"); Stucourseapply.setprocessdefinitionkey (PROCESSDEFINITIONKEYSTR);//Call business method (save request information)//1, set properties and save Stucourseapply Stucourseapply.setapplytime (Sdf.format (New Date ())); Application time, Current time stucourseapply.setstatus (stucourseapply.status_running); Two times save?   Stucourseapplyservice.save (stucourseapply); 2,Prepare the process variable map<string, object> variablesmap = new hashmap<string, object> (); Variablesmap.put ("StuCourseApply" , stucourseapply);//Get the process definition keystring Pdkey = Stucourseapply.getprocessdefinitionkey ();//3, start the process instance start flow, and take the process variable (current request information), call the host program//Call the business, save the Application information Startabstractjbpm.common (Pdkey, Variablesmap, Jbpmservice); Startabstractjbpm.loghandler1 Loghandler = Startabstractjbpm.new LogHandler1 ();//Set the value in the proxy// Stucourseapply.setexecuteid (Pi.getid ()); Stucourseapplyservice stucourseapplyservice1= (stucourseapplyservice) Loghandler.newproxyinstancestart (  Stucourseapplyservice); Stucourseapplyservice1.save (stucourseapply); return "Tostuapplicationlist"; After success, go to my Application enquiry}


Corresponding processing node:

/** Approval Processing */public String approve () throws Exception {Abstractapprove ();//Application Agent Loghandler loghandler=new Loghandler ();//Tune The completion of the current task method Stucourseapplyservice stucourseapplyservice1= (Stucourseapplyservice) is called before the workflow is updated with the business, update state, and update status. Loghandler.newproxyinstance (stucourseapplyservice);  Stucourseapplyservice1.update (stucourseapply); return "Tostutasklist"; After success go to my Approval page}

Through these two pieces of code, I believe you can see that there is no workflow in these two pieces of the content, and our process is still managed by the workflow, that is, we will all about the workflow of the methods are abstracted out, let our class simply deal with the business, in the invocation of the business method, we call the agent, At this time, the agent has been the start of the workflow process, such as a series of operations encapsulated in, when we call the Proxy method has started the workflow, and then also operate the workflow processing, so the entire business process in the case we do not need to understand the workflow has been implemented by the process management.


we've been talking. AOP , then what is AOP ?

AOP (aspectorientedprogramming): Divides code from business logic code, such as logging, performance statistics, security control, transaction processing, exception handling, and so on, by separating these behaviors, we want to be able to separate them into non-instructional methods of business logic , the code that changes these behaviors without affecting the business logic --- two words summed up: decoupling.

Summarize:


most want to say a word: The meeting is not difficult, the difficult person will not. During this period of learning workflow, various kinds of don't understand, all kinds of questioned themselves, this is AOP ? Is this what we want? Sometimes you still ask yourself what a workflow is, and in the end, what does it do for us?

Work Flow (Workflow) is "the automation of part or whole of a business process in a computer application environment ", it primarily addresses the" process of automating the delivery of documents, information, or tasks between multiple participants in accordance with a predefined rule, to achieve an expected business goal, or to promote the realization of this goal "it's really great, but I think we're going to do more than that, One thing to add is the AOPthat implements the workflow!





"Code piece" JBPM4.4 Development process node (dynamic agent implementation process management business)

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.