对于 JBPM 6,我已经实现了一个像这样的异步工作项处理程序,因为管理器和 ksession 无法完成任务。RuntimeManagerRegistry 的 Maven 依赖项是 kie 内部版本 6.0.1.Final,jsoup 1.7.2。
import java.io.IOException;
import org.drools.core.process.instance.impl.WorkItemImpl;
import org.jsoup.Jsoup;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.runtime.process.WorkItemManager;
import org.kie.internal.runtime.manager.RuntimeManagerRegistry;
import org.kie.internal.runtime.manager.context.EmptyContext;
public class AsyncWorkItemHandler implements WorkItemHandler {
boolean abort = false;
@Override
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
manager.abortWorkItem(workItem.getId());
abort = true;
}
@Override
public void executeWorkItem(final WorkItem workItem, final WorkItemManager manager) {
final String depId = ((WorkItemImpl) workItem).getDeploymentId();
new Thread(new Runnable() {
String url = (String) workItem.getParameter("url");
org.jsoup.nodes.Document document = null;
public void run() {
while (true) {
if (abort == true) {
break;
}
// example of polling a web site for specific text
// before task can complete say "release"
try {
document = Jsoup.connect(url).timeout(3000).get();
}
catch (IOException e) {
e.printStackTrace();
}
String info = document.text();
if (info.contains("release")) {
if (depId != null) {
RuntimeManager manager = RuntimeManagerRegistry.get().getManager(depId);
RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
engine.getKieSession().getWorkItemManager().completeWorkItem(workItem.getId(), null);
manager.disposeRuntimeEngine(engine);
}
else {
// this is for eclipse testing because depId is not
// available
manager.completeWorkItem(workItem.getId(), null);
}
break;
}
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}