问题
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
First of all install jbpm in eclipse
Create a jBPM project in eclipse (tick Build the project using Maven)
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 ! "); } }
Build a jar file of this project.
From the workbench, go to Artifact, upload the jar
click on this icon, then artifactsfrom the settings of your project, go to dependencies, and add from repository the uploaded artifact
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()
)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"
]
- 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