4 Replies Latest reply on Nov 21, 2014 10:37 AM by ivanice

    unable to start a process with async workItemHandler

    ivanice

      Hi to all,

      like the title i have a process with two custom Service task ( the same task ).

      I have implemented the async task like the example in the userguide of jbpm but the process finished before perfoming all tasks.

      This is the log error:

       

      INFO: HHH000232: Schema update complete

      SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".

      SLF4J: Defaulting to no-operation MDCAdapter implementation.

      SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.

      Saluti a tutti

      nov 20, 2014 11:26:24 PM org.hibernate.ejb.AbstractEntityManagerImpl joinTransaction

      WARN: HHH000326: Cannot join transaction: do not override hibernate.transaction.factory_class

      Exception in thread "Thread-4" javax.persistence.TransactionRequiredException: No active JTA transaction on joinTransaction call

          at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1233)

          at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1183)

          at org.drools.persistence.jpa.AbstractPersistenceContextManager.getCommandScopedEntityManager(AbstractPersistenceContextManager.java:110)

          at org.drools.persistence.jpa.JpaPersistenceContextManager.getCommandScopedPersistenceContext(JpaPersistenceContextManager.java:65)

          at org.drools.persistence.jpa.processinstance.JPAWorkItemManager.completeWorkItem(JPAWorkItemManager.java:110)

          at com.sample.Smile$1.run(Smile.java:31)

          at java.lang.Thread.run(Thread.java:745)

       

       

      the code of workitem is:

       

      package com.sample;

       

      import org.kie.api.runtime.process.WorkItem;

      import org.kie.api.runtime.process.WorkItemHandler;

      import org.kie.api.runtime.process.WorkItemManager;

       

      public class Smile implements WorkItemHandler{

       

           public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {

                  manager.abortWorkItem(workItem.getId());

                }

       

          public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

              final Object p= workItem.getParameter("param1");

              final long myId=workItem.getId();

              final WorkItemManager man=manager;

       

              new Thread(new Runnable() {

                    public void run() {

                           System.out.println(p);

                           man.completeWorkItem(myId, null);

                    }

                  }).start();        

            }   

         

      }

       

      Please somebody can help me???

        • 1. Re: unable to start a process with async workItemHandler
          swiderski.maciej

          problem is that you try to use WorkItemManager instance from background thread that will be actually invoked after transaction (it was associated with) is already completed. You need to use ksession to get work item manager and then complete it from within your background thread. See an example here.

           

          HTH

          Maciej

          • 2. Re: unable to start a process with async workItemHandler
            ivanice

            thanks for your response, but now i have change the row:"man.completeWorkItem(myId, null);" that i have put after the row:"}).start();"

            Now the process works but you think that this is the right way to works with serviceTask with thread?

            • 3. Re: unable to start a process with async workItemHandler
              ivanice

              indeed I think that in this way I don't resolve my problem

              • 4. Re: unable to start a process with async workItemHandler
                ivanice

                I think that i resolved in this way:

                public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

                        final Object p= workItem.getParameter("param1");

                       

                        final String deploymentId = nonNull(((WorkItemImpl)workItem).getDeploymentId());

                        final long processInstanceId = workItem.getProcessInstanceId();

                        final long workItemId = workItem.getId();

                 

                        new Thread(new Runnable() {

                 

                 

                              public void run() {

                                  //Map<String, Object> output = new HashMap<String, Object>();

                                     System.out.println(p);

                                     RuntimeManager manager = RuntimeManagerRegistry.get().getManager(deploymentId);

                                     if (manager != null) {

                                     RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));

                                     engine.getKieSession().getWorkItemManager().completeWorkItem(workItemId, null);

                                     manager.disposeRuntimeEngine(engine);

                                     } else {

                                     // in case there is no RuntimeManager available use available ksession,

                                     // as it might be used without runtime manager at all

                                     ksession.getWorkItemManager().completeWorkItem(workItemId, null);

                                     }

                                    

                              }

                            }).start();

                 

                 

                Thanks Maciej for your link