JBPM6 Service task to execute java code
Asked Answered
H

4

7

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.

Hindrance answered 3/3, 2015 at 6:22 Comment(0)
U
8

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.

Unhouse answered 3/3, 2015 at 13:21 Comment(7)
Hello mike. I am still not able to generate custom work item. I followed same procedure as u mentioned. Change in my code is only that in main class i am using 'KieSession ksession = engine.getKieSession();' and in CustomWorkItemHandlers.conf. I changed it as per my package 'Awesome": new com.sample.AwesomeHandler()' Do we need any drools specific setting for it because currently i am doing it in normal mavenJBPM project. Do we need any specific Palette? I have JBPM palette only for now. can you help me with this?Hindrance
Are you saying that your error is that you are not seeing the custom task in your palette? It is hard to troubleshoot without seeing what you did. Are you sure that all of the files are in the correct location? Does the icon filename specified in WorkItemDefinitions.wid actually refer to an actual image file in src/main/resources? Did you remember the drools.session.conf file? Is it possible for you to post your code?Unhouse
I just double checked my project and I had a wrong file location. It should be src/main/resources/META-INF/drools.session.conf . I will update the filepath in my original post.Unhouse
Thank you mike.Its done.But now i am facing different issue.I have jar file of my project which i included in JBPM's kjar. But i am facing classnotfoundException.Someway I am able to remove that but still interfaces not detected.So I am not able to perform operation from .jar file through process. Can you help me with that?Hindrance
I would post this as a separate question. I don't know too much about the deployment structure. I just happened to have implemented custom service tasks a week before asking. If you post this a separate question, you will likely get either Kris Verlaenen or salaboy to help you out.Unhouse
Then it encounters error- Unexpected error encountered: [com.sample.bpmn.hello:30 - Awesome:2] -- Could not find work item handler for AwesomeIllness
How to execute multiple workitemhandler sequentially?Illness
A
1

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.

Appolonia answered 8/5, 2016 at 21:37 Comment(0)
S
1

@mike i really hope you see my comment and willing to help me. so i followed everysteps that you mentioned but i still never see my customtask.

enter image description here

this is my project directory and i think everything is right, but to make sure ill just posts my code here

WorkItemDefinitions.wid:

import org.drools.core.process.core.datatype.impl.type.StringDataType;

    [
      [
        "name" : "Awesome",
        "parameters" : [
          "Message1" : new StringDataType(),
           "Message2" : new StringDataType()
         ],
        "displayName" : "Awesome",
        "icon" : "ezgif.com-apng-to-gif.gif"
      ]
    ]

drools.session.conf:

drools.workItemHandlers = CustomWorkItemHandlers.conf

CustomWorkItemHandlers.conf:

[
  "Awesome": new com.sample.AwesomeHandler()
]

AwsomeHandler.java:

package com.sample;

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.runtime.process.WorkItemManager;

public class AwesomeHandler implements WorkItemHandler{
    
    public AwesomeHandler() {
        super();
    }


    @Override
    public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
        // TODO Auto-generated method stub
        System.out.println("Executing Awesome handler");
        manager.completeWorkItem(workItem.getId(), null);
    }

    @Override
    public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        // TODO Auto-generated method stub
        System.out.println("Aborting");
    }

}

in main:

TaskService taskService = engine.getTaskService();
ksession.getWorkItemManager().registerWorkItemHandler("Awesome", new AwesomeHandler());

I really don't know what i did wrong and i need this for my unis. i really hope i will get a reply and wish you a very good day ;)

Soemba answered 27/4, 2021 at 15:56 Comment(0)
L
0

Apart from the (excellent) example provided by Mike, if your only goal is to execute some Java code, you can consider using a Script task instead (and just embed the Java code in your process) or reuse the already existing Service Task that can invoke an operation on a Java class.

Loppy answered 3/3, 2015 at 20:0 Comment(1)
yes Kris.I am doing with same currently.But the case when i have to work with asynchronous tasks that time multiple objects will get created which i think is not beneficial. So i am considering about this Service thask. Thank you very much for responding.Hindrance

© 2022 - 2024 — McMap. All rights reserved.