Java Workflow Engine JBoss jbpm 3.1.2 demo

Source: Internet
Author: User

Jbpm is a workflow and Business Process Management engine developed by JBoss. It can be used to create business processes that interact with different people, different applications, and different services. Jbpm also provides a visual business flow designer. Jbpm includes the following components: runtime engine (A pojo Library), graphical designer (an Eclipse plug-in), Hibernate-based persistence, JSF-based Web console, and BPEL extension.

Hello world example

A process definition is a manageable flowchart that contains deliverables between nodes. The Hello world sample process contains three nodes. To see how nodes work together, no design tool is used to define a simple process. The flowchart definition of the Hello World sample process is displayed:

Hello world example Flowchart

Public void testhelloworldprocess (){
Processdefinition = processdefinition. parsexmlstring (
"<Process-definition>" +
"<Start-State>" +
"<Transition to ='s '/>" +
"</Start-State>" +
"<State name ='s '>" +
"<Transition to = 'end'/>" +
"</State>" +
"<End-state name = 'end'/>" +
"</Process-definition>"
);
Processinstance = new processinstance (processdefinition );
Token token = processinstance. getroottoken ();
Assertsame (processdefinition. getstartstate (), Token. getnode ());
Token. Signal ();
Assertsame (processdefinition. getnode ("S"), Token. getnode ());
Token. Signal ();
Assertsame (processdefinition. getnode ("end"), Token. getnode ());
}

Database example

One of the basic features of jbpm is the process that is in the waiting state during persistent execution in the jbpm database. The database example shows how to store a process instance in the jbpm database. This example may involve the jbpm context. Create a separate method for different user codes. For example, a piece of user code in webapplication starts a process and persists in the database. Then, a message-driven Bean (MDB) loads the process instance to continue the process.

Public class helloworlddbtest extends testcase {
Static jbpmconfiguration = NULL;
Static {
Jbpmconfiguration = jbpmconfiguration. parsexmlstring (
"<Jbpm-configuration>" +
"<Jbpm-context>" +
"<Service name = 'persistence '" +
"Factory = 'org. jbpm. Persistence. DB. dbpersistenceservicefactory '/>" +
"</Jbpm-context>" +
"<String name = 'Resource. hibernate. cfg. xml'" +
"Value = 'hibernate. cfg. xml'/>" +
"<String name = 'Resource. Business. Calendar '" +
"Value = 'org/jbpm/calendar/jbpm. Business. Calendar. properties'/>" +
"<String name = 'Resource. Default. Les le'" +
"Value = 'org/jbpm/graph/DEF/jbpm. Default. modules. properties'/>" +
"<String name = 'Resource. converter '" +
"Value = 'org/jbpm/DB/hibernate/jbpm. converter. properties'/>" +
"<String name = 'Resource. Action. types '" +
"Value = 'org/jbpm/graph/Action/action. types. xml'/>" +
"<String name = 'Resource. node. types '" +
"Value = 'org/jbpm/graph/node. types. xml'/>" +
"<String name = 'Resource. varmapping'" +
"Value = 'org/jbpm/Context/EXE/jbpm. varmapping. xml'/>" +
"</Jbpm-configuration>"
);
}
Public void setup (){
Jbpmconfiguration. createschema ();
}
Public void teardown (){
Jbpmconfiguration. dropschema ();
}
Public void testsimplepersistence (){
Deployprocessdefinition ();
Processinstanceiscreatedwhenusersubmitswebappform ();
Theprocessinstancecontinueswhenanasyncmessageisreceived ();
}
Public void deployprocessdefinition (){
Processdefinition = processdefinition. parsexmlstring (
"<Process-definition name = 'Hello world'>" +
"<Start-state name = 'start'>" +
"<Transition to ='s '/>" +
"</Start-State>" +
"<State name ='s '>" +
"<Transition to = 'end'/>" +
"</State>" +
"<End-state name = 'end'/>" +
"</Process-definition>"
);
Jbpmcontext = jbpmconfiguration. createjbpmcontext ();
Try {
Jbpmcontext. deployprocessdefinition (processdefinition );

} Finally {
Jbpmcontext. Close ();
}
}
Public void processinstanceiscreatedwhenusersubmitswebappform (){
Jbpmcontext = jbpmconfiguration. createjbpmcontext ();
Try {
Graphsession = jbpmcontext. getgraphsession ();
Processdefinition = graphsession. findlatestprocessdefinition ("Hello World ");
Processinstance = new processinstance (processdefinition );
Token token = processinstance. getroottoken ();
Assertequals ("START", Token. getnode (). getname ());
Token. Signal ();
Assertequals ("S", Token. getnode (). getname ());
Jbpmcontext. Save (processinstance );
} Finally {
Jbpmcontext. Close ();
}
}
Public void theprocessinstancecontinueswhenanasyncmessageisinclued (){
Jbpmcontext = jbpmconfiguration. createjbpmcontext ();
Try {
Graphsession = jbpmcontext. getgraphsession ();
Processdefinition = graphsession. findlatestprocessdefinition ("Hello World ");
List processinstances = graphsession. findprocessinstances (processdefinition. GETID ());
Processinstance = (processinstance) processinstances. Get (0 );
Processinstance. Signal ();
Asserttrue (processinstance. hasended ());
Jbpmcontext. Save (processinstance );
} Finally {
Jbpmcontext. Close ();
}
}
}

Context example: Process Variables

The process variable contains the context information during the process execution. The process variable is similar to a java. util. Map type name/value pair and is a Java object. A process variable is an integral part of a process instance. To avoid event complexity, this example only demonstrates the API for processing process variables, not including persistent processing.

Processdefinition = processdefinition. parsexmlstring (
"<Process-definition>" +
"<Start-State>" +
"<Transition to ='s '/>" +
"</Start-State>" +
"<State name ='s '>" +
"<Transition to = 'end'/>" +
"</State>" +
"<End-state name = 'end'/>" +
"</Process-definition>"
);

Processinstance = new processinstance (processdefinition );
Contextinstance = processinstance. getcontextinstance ();
Contextinstance. setvariable ("amount", new INTEGER (500 ));
Contextinstance. setvariable ("reason", "I met my deadline ");
Processinstance. Signal ();
Assertequals (New INTEGER (500), contextinstance. getvariable ("amount "));
Assertequals ("I met my deadline", contextinstance. getvariable ("reason "));

Task Allocation example

The Task Assignment example shows how to assign a task to a user. Due to the separation of the jbpm workflow engine and organizational model, there are always many restrictions on the language used to process participants. Therefore, you must implement a task distributor that contains handling task participants.

Public void testtaskassignment (){
Processdefinition = processdefinition. parsexmlstring (
"<Process-definition name = 'The baby process'>" +
"<Start-State>" +
"<Transition name = 'Baby cries' to = 'T'/>" +
"</Start-State>" +
"<Task-node name = 'T'>" +
"<Task Name = 'change nappy'>" +
"<Assignment class = 'org. jbpm. Tutorial. taskmgmt. nappyassignmenthandler'/>" +
"</Task>" +
"<Transition to = 'end'/>" +
"</Task-node>" +
"<End-state name = 'end'/>" +
"</Process-definition>"
);
Processinstance =
New processinstance (processdefinition );
Token token = processinstance. getroottoken ();
Token. Signal ();
Assertsame (processdefinition. getnode ("T"), Token. getnode ());
Taskinstance = (taskinstance) processinstance
. Gettaskmgmtinstance ()
. Gettaskinstances ()
. Iterator (). Next ();
Assertequals ("papa", taskinstance. getactorid ());
Taskinstance. End ();
Assertsame (processdefinition. getnode ("end"), Token. getnode ());
}

JBoss jbpm 3.1 workflow and BPM made practical

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.