JBPM6 Utility to execute Java code

I am new to JBPM6. My scenario looks like this: I want to execute some Java code using a JBPM service. From the documentation, I cannot figure out how to use the domain-specific process and the work item handler in this type of code. If anyone has a sample example, please share. It will be very helpful.

Thanks in advance.

+6
source share
3 answers

Here's how to add a handler inside an eclipse maven project. I call it the Awesome handler, but you have to choose a more specific name.

1) First create a work item definition file in the src / main / resources / WorkItemDefinitions.wid file. My icon file is located in src / main / resources.

import org.drools.core.process.core.datatype.impl.type.StringDataType; [ [ "name" : "Awesome", "parameters" : [ "Message1" : new StringDataType(), "Message2" : new StringDataType() ], "displayName" : "Awesome", "icon" : "icon-info.gif" ] ] 

2) Create a work item handler configuration file in the src / main / resources / META-INF / CustomWorkItemHandlers.conf file

 [ "Awesome": new org.jbpm.examples.util.handler.AwesomeHandler() ] 

3) Create a drools session configuration file: src / main / resources / META-INF / drools.session.conf

 drools.workItemHandlers = CustomWorkItemHandlers.conf 

4) Create a handler so that it matches the class that you defined in step 2

 public class AwesomeHandler implements WorkItemHandler { public AwesomeHandler() { super(); } public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { System.out.println("Executing Awesome handler"); manager.completeWorkItem(workItem.getId(), null); } public void abortWorkItem(WorkItem workItem, WorkItemManager manager) { System.out.println("Aborting"); } } 

5) After installing the handler, you must register it in your session.

 //Get session KieSession ksession = runtime.getKieSession(); //Register handlers ksession.getWorkItemManager().registerWorkItemHandler("Awesome", new AwesomeHandler()); 

At this point, you should restart eclipse. When an eclipse opens, the palette should have a tab "Custom Tasks". It must contain an Awesome entry with the indicated icon.

+7
source

I know that the question has already been answered, but I wanted to do the same thing (execute Java code in a service task) without creating a work item definition (I did not want to use a custom task, but as a service task, as it is), here’s how I decided this:

here I read about ServiceTaskHandler, but I could not find very good usage information.

I read the ServiceTaskHandler code, it uses reflection to run your java code.

I found this (it says jbpm5-samples, but I tested with jbpm 6.3), it uses a service task, the service task executes the "hello" method from the class (HelloService) that you create:

 package com.test; import java.util.HashMap; import java.util.Map; public class HelloService { public DataOutput hello(com.test.DataInput name) { Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("s", "Hello " + name.getDataMap().get("s") + "!"); DataOutput output = new DataOutput(dataMap); return output; } } 

ServiceTaskHandler is logged in the same way as step (5) in the answer marked correctly:

 //Get session KieSession ksession = runtime.getKieSession(); //Register handlers ksession.getWorkItemManager().registerWorkItemHandler("Service Task", new ServiceTaskHandler()); 

After that, I associated the service task with the java class (HelloService - hello method). For this, I used the eclipse bpmn fashion designer, but I did not find it very intuitive, so I opened the bpmn sample file (BPMN2-ServiceProcess.bpmn2) with the help of a fashion designer and filled my office task with the same material as there.

+1
source

Besides the (excellent) example provided by Mike, if your only goal is to execute some Java code, you can use the Script task instead (and just embed the Java code in your process) or reuse an existing utility task that can trigger an operation on Java class.

0
source

Source: https://habr.com/ru/post/983250/


All Articles