在异步Workitemhandler中处理任务

时间:2012-05-23 06:27:45

标签: jboss drools jbpm

我在一篇jbpm论坛文章https://community.jboss.org/thread/165545中读到,将HumanTaskHandler描述为异步工作项处理程序,当我们使用异步humantask处理程序时,我们必须使用 taskclient.complete()完成任何任务然后使用 ksession.getWorkItemManager()。completeWorkItem()向流程引擎发出信号。

这是使用异步workitemhandler完成任务的理想方法吗? 这就像完成任务两次。实际上,当我们调用complete()和completeWorkItem()时会发生什么 请澄清。

2 个答案:

答案 0 :(得分:2)

如果在调用taskClient.complete()方法时已正确设置了所有内容,则会通知会话并自动为您调用ksession.getWorkItemManager()。completeWorkItem()。没有必要自己打电话。您只需要记住将WorkItemHandler注册到会话并保持会话处于活动状态,以便通知它。 干杯

答案 1 :(得分:1)

对于JBPM 6,我已经实现了这样的异步工作项处理程序,因为管理器和ksession不能完成任务。 RuntimeManagerRegistry的Maven依赖是kie-internal版本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();
        }
    }