How do I add and call a custom Java class inside a jBPM Process?

Nenotlep :

I have a local jBPM 7.33 with a simple process. At one point in the process, I need to generate a PDF file.

I want to do it by creating a very basic Java class that is run in a Task. The class would get variables from the process scope, generate the PDF and save the generated blob (or filesystem path) as a process variable.

How do I add a custom class and then call that class?

Bashir :

that's what we call it WorkItemHandler , your java class will be a customized jbpm task

  1. First of all install jbpm in eclipse

  2. Create a jBPM project in eclipse (tick Build the project using Maven)

  3. create a java class that implements WorkItemHandler. it will be in this format.

    package com.example;
    import org.kie.api.runtime.process.WorkItem;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.drools.core.process.instance.WorkItemHandler;
    import org.kie.api.runtime.process.WorkItemManager;
    
    public class WorkItemTest implements WorkItemHandler {
    
        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            workItem.getParameters().toString();
    
            /**Input Variables***/
            String stringVar = (String) workItem.getParameter("stringVar");
    
    
            /***
             * 
             * 
             * YOUR CODE
             * 
             */
    
            String msg = "done";
    
            /**Output Variables in a HashMap***/
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("Result", msg); //("name of variable", value)
            manager.completeWorkItem(workItem.getId(), resultMap);
        }
    
        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            System.out.println("Aborted ! ");
    
        }
    
    }
    
  4. Build a jar file of this project.

  5. From the workbench, go to Artifact, upload the jar
    click on this icon, then artifacts enter image description here

  6. from the settings of your project, go to dependencies, and add from repository the uploaded artifact enter image description here

  7. from the settings of your project, go to Deployments / Work Item Handler and add a new work Item Handler : type its name and how to instantiate it (new com.example.WorkItemTest()) enter image description here

  8. Finally, go to the Asset "WorkDefinitions" , define your work item (so you can see it in the workflow designer tool) as follow

  [
    "name" : "WorkItemTest",
    "parameters" : [ //inputs
        "stringVar " : new StringDataType(),
    ],
    "results" : [ //outputs
        "Result" : new ObjectDataType(),
    ],
    "displayName" : "WorkItemTest",
    "icon" : "defaultservicenodeicon.png"
  ]
  1. you can now find this task in "service tasks" of your workflow designer tool (refresh before)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238140&siteId=1