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

烂漫一生 提交于 2020-06-23 08:30:08

问题


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?


回答1:


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

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

  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())

  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)


来源:https://stackoverflow.com/questions/60654953/how-do-i-add-and-call-a-custom-java-class-inside-a-jbpm-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!