Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing domain-specific objects to a domain-specific task in jBPM 6 workbench

I've been struggling with this for a few days and haven't had much luck. In jBPM 6 workbench running on jboss wildfly, I'm trying to invoke a custom work item handler and pass in an instance of a domain-specific object.

Here's my process so far:

  1. Define a domain-specific class acme.datamodel.UserBio in an external jar: datamodel.jar.

  2. Upload "datamodel.jar" to jbpm-console via the "Artifact repository"->Upload feature.

  3. Create a new repository in jbpm-console and create a new jbpm project.

  4. Add datamodel.jar as a dependency to my jbpm project via the Project editor.

  5. Create a new process in the jbpm project.

  6. Create a new process variable of type acme.datamodel.UserBio.

  7. Create a new work item definition for my domain-specific task (Greeting.wid) like so:

    import org.drools.core.process.core.datatype.impl.type.StringDataType;
    import org.drools.core.process.core.datatype.impl.type.ObjectDataType;
    [
      [
        "name" : "Greeting", 
        "parameters" : [ 
            "UserBio" : new ObjectDataType("acme.datamodel.UserBio") 
        ], 
        "results" : [ 
            "Result" : new ObjectDataType("java.util.Map") 
        ], 
        "displayName" : "Greeting", 
        "icon" : "../../../global/defaultservicenodeicon.png" 
      ]
    ]
    
  8. Add the domain-specific task (Greeting) to my process workflow.

  9. Map the process variable from step 5 as the input parameter for the Greeting task.

  10. Define my custom work item handler in an external jar (workitems.jar) like so:

    package acme.workitem.greeting;
    
    import java.util.Map;
    
    import org.kie.api.runtime.process.WorkItem;
    import org.kie.api.runtime.process.WorkItemManager;
    import org.kie.internal.runtime.StatefulKnowledgeSession;
    
    import acme.datamodel.UserBio;
    
    public class GreetingWorkItemHandler implements WorkItemHandler {
    
            private StatefulKnowledgeSession ksession;
    
        public GreetingWorkItemHandler(StatefulKnowledgeSession ksession) {
                this.ksession = ksession;
            }
    
        public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    
            // Extract required parameters
            UserBio bio = (UserBio) workItem.getParameter("UserBio");
    
            // Do something
    
            // Notify manager that work item has been completed and return results
            Map<String, Object> results = new HashMap<String, Object>();
            workItemManager.completeWorkItem(workItem.getId(), results);
        }
    
        public void abortWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
            // Can't abort a greeting work item handler
        }
    }
    
  11. Copy workitems.jar to the jbpm-installer/dependencies directory.

  12. Edit jbpm-installer/conf/META-INF/CustomWorkItemHandlers.conf and add the following line:

    "Greeting" : new acme.workitem.greeting.GreetingWorkItemHandler(ksession)
    
  13. Re-install jbpm demo (i.e. jbpm-console) and restart jboss.

  14. Load jbpm-console in a browser, start an instance of my process and attempt to invoke the domain-specific task.

After all this, when the domain-specific task is loaded and the custom work item handler is invoked, I get the following error in jbpm-console but nothing displayed in my jboss log:

Unexpected error encountered : java.lang.NoClassDefFoundError:acme/datamodel/UserBio

Are custom work item handlers not loaded with the same classloader as the process instances that invoke them?

I've tried adding the datamodel.jar into my workitems.jar under the META-INF/lib directory but that didn't seem to change anything.

Do I need to add datamodel.jar as a dependency to the Greeting.wid? How do I define the location of the jar file when it's already been uploaded to jbpm-console? Do I just copy datamodel.jar to the dependencies directory like I did for the workitems.jar and re-install the jbpm-console? Won't this cause potential collisions with the version uploaded to the jbpm-console in step 2?

These are just some of the things I've been wrestling with over the past few days. All the examples I've seen for dealing with custom work item handlers only ever seem to pass in primitives, never more complicated objects.

If anyone can explain where I've gone wrong or point me to better examples I'd very much appreciate it.

like image 288
Greg Moulds Avatar asked Nov 10 '22 01:11

Greg Moulds


1 Answers

I was able to successfully invoke the custom work item handler by deploying the datamodel jar along with the workitem jar. A few more details can be found here.

like image 146
Greg Moulds Avatar answered Nov 14 '22 21:11

Greg Moulds