0

I read in a jbpm forum article https://community.jboss.org/thread/165545 describing the HumanTaskHandler as Asynchronous workitem handler and when we are using an asynchronous humantask handler we have to complete any task with taskclient.complete() and then signal the process engine with ksession.getWorkItemManager().completeWorkItem().

Is this an ideal approach to complete a task while using asynchronous workitemhandler.?
This is like completing a task twice. Actually whats happening when we call complete() and completeWorkItem().?
Kindly clarify.

Bashir
  • 2,057
  • 5
  • 19
  • 44
Arun Selva
  • 117
  • 3
  • 15

2 Answers2

2

If you have everything set up correctly when you call the taskClient.complete() method the session will notified and the ksession.getWorkItemManager().completeWorkItem() will be called automatically for you. There is no need to call it by yourself. You only need to remember to register the WorkItemHandler to the session and keep the session alive so it can be notified. Cheers

salaboy
  • 4,123
  • 1
  • 14
  • 15
  • The documentation clearly states that in case of asynchronous workitem handlers we have to use Completeworkitem(). If client.complete() is sufficient in which scenario the completworkitem() would be required.? I'm just eager to know it. – Arun Selva May 24 '12 at 14:07
  • Got it.When we are restart the process engine we have to use completeworkitem() on the task of an interrupted process to continue the execution. – Arun Selva May 25 '12 at 07:26
1

For JBPM 6 I have implemented an asynchronous workitem hanler like this because the manager and ksession don't work to complete the task. Maven dependency for RuntimeManagerRegistry is kie-internal version 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();
        }
    }