Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JBPM6 Service task to execute java code

Tags:

java

jbpm

bpmn

I am new in JBPM6. My scenario is like this that i want to execute some java code using JBPM service task.From documentation i am not able to understand how to use domain specific process and Work Item Handler in this type of code. If someone have sample example of it please share.That will be very much helpful.

Thank you in advance.

like image 849
user3118158 Avatar asked Mar 03 '15 06:03

user3118158


People also ask

What is service task in jBPM?

Service tasks (aka work items) are of tremendous use in business processes. Users can build their custom logic into well defined tasks that can be reused across processes or even projects. jBPM comes with rather large set of service tasks out of the box, you can explore them in jbpm-work-items repository in GitHub.

What is script task in jBPM?

The script task defines a script that needs to be executed when the task is reached. In this case, the script invokes an existing class org.jbpm.examples.quickstarts.HelloService : HelloService. getInstance().

What is jBPM console?

jBPM is a flexible Business Process Management (BPM) Suite. It is light-weight, fully open-source (distributed under Apache License 2.0) and written in Java. It allows you to model, execute, and monitor business processes and cases throughout their life cycle.


2 Answers

Here is how to add a handler inside a Eclipse maven project. I call it the Awesome handler, but your should pick a more specific name.

1) First create a work item definition file in src/main/resources/WorkItemDefinitions.wid. 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 Config file in src/main/resources/META-INF/CustomWorkItemHandlers.conf

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

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

drools.workItemHandlers = CustomWorkItemHandlers.conf

4) Create your Handler so that it matches the class 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 you establish the handler, you must register it with your session.

//Get session
KieSession ksession = runtime.getKieSession();

//Register handlers
ksession.getWorkItemManager().registerWorkItemHandler("Awesome", new AwesomeHandler());

At this point you should restart eclipse. When eclipse opens, there should be a 'Custom Tasks' tab in the palette. It should contain an entry labeled 'Awesome' with the specified icon.

like image 200
Mike Avatar answered Sep 24 '22 01:09

Mike


I know the question is already answered, but I wanted to do the same (execute java code in service task) without creating work item definition (I did't want to use a custom task but a service task as it is). This is how I solved it:

here I read about the ServiceTaskHandler but I couldn't find very good info about the usage.

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 method "hello" from a Class (HelloService) 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;
    }

}

The ServiceTaskHandler is registered the same way as the step (5) in the answer marked correct:

//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 - method hello). To do that I used the eclipse bpmn modeler but I didn't find it very intuitive, so I opened the sample's bpmn file (BPMN2-ServiceProcess.bpmn2) with the modeler and filled my service task with the same stuff I read there.

like image 32
venito Avatar answered Sep 22 '22 01:09

venito