4 Replies Latest reply on Mar 4, 2014 12:50 PM by Rahul Magdum

    get thread id for async task in jBPM.

    Rahul Magdum Novice

      Hi, i have modified given UserCommand class in examples as follows -

       

      public class UserCommand implements Command{

         

          private static final Logger logger = LoggerFactory.getLogger(UserCommand.class);

       

       

          public ExecutionResults execute(CommandContext ctx) {

              logger.info("Command executed on executor with data {}", ctx.getData());

              logger.info("Thread ID : ",Thread.currentThread().getId());

              WorkItem workItem = (WorkItem) ctx.getData("workItem");

              User user = (User) workItem.getParameter("UserIn");

              user.setName(user.getName() + "_m_"+ Thread.currentThread().getId());

              Thread t = Thread.currentThread();

              t.setName(user.getName());

              logger.info("Thread Name: ",t.getName());

              logger.info("Thread ID: ",t.getId());

              ExecutionResults executionResults = new ExecutionResults();

              executionResults.setData("UserOut", user);

              return executionResults;

          }

         

      }


      I call this from attached bpmn with singleton strategy.
      But Thread Name , Thread ID prints nothing in logs

       

       

      13:21:11,233 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-19-thread-1) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:2.4, workItem=WorkItem 287 [name=async, state=0, processInstanceId=185, parameters{TaskName=async, UserIn=[Name=rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=185, businessKey=185:287, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

      13:21:11,251 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-19-thread-1) Thread ID :

      13:21:11,253 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread Name:

      13:21:11,254 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID:

      13:21:14,225 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:2.4, workItem=WorkItem 288 [name=async, state=0, processInstanceId=185, parameters{TaskName=async, UserIn=[Name=rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=185, businessKey=185:288, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

      13:21:14,247 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID :

      13:21:14,250 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread Name:

      13:21:14,251 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID:

      13:21:17,229 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:2.4, workItem=WorkItem 289 [name=async, state=0, processInstanceId=185, parameters{TaskName=async, UserIn=[Name=rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=185, businessKey=185:289, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

      13:21:17,250 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID :

      13:21:17,251 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread Name:

      13:21:17,252 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID:

      13:21:20,227 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:2.4, workItem=WorkItem 290 [name=async, state=0, processInstanceId=185, parameters{TaskName=async, UserIn=[Name=rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=185, businessKey=185:290, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

      13:21:20,241 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID :

      13:21:20,243 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread Name:

      13:21:20,245 INFO  [org.jbpm.examples.cmd.UserCommand] (rahul_m_176) Thread ID:

       

       

      salaboy21 swiderski.maciej

       

      Why is it so? How i can identify that async task is running as a separate technical thread?

        • 1. Re: get thread id for async task in jBPM.
          Mauricio Salatino Master

          The command is executed by another thread, an executor thread. Why do you want to know the thread name? what's the point of that?

          • 2. Re: get thread id for async task in jBPM.
            Rahul Magdum Novice

            So the command is executed by job executor thread?
            if there are 4 async tasks invoked from process, all these tasks will create a separate technical thread on system ?
            or only job executor thread takes one task at a time from pool and execute that task?

            • 3. Re: get thread id for async task in jBPM.
              Mauricio Salatino Master

              Hi Rahul,

              That's the whole point of the executor service, to take care of your async tasks. There is a thread pool that the executor uses to execute those tasks. If it runs out of threads it waits until there is one available to execute the pending tasks.

               

              So you don't need to worry about it.

              1 of 1 people found this helpful
              • 4. Re: get thread id for async task in jBPM.
                Rahul Magdum Novice

                Yes Mauricio, agree with you. Thanks for reply. I tried same example by setting thread pool to 100 and called async task 1000 times.
                i got following output :

                 

                23:14:12,518 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-12) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:1.0, workItem=WorkItem 561 [name=async, state=0, processInstanceId=199, parameters{TaskName=async, UserIn=[Name=Rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=199, businessKey=199:561, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

                23:14:12,526 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-12) user info set

                23:14:12,528 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-12) task endedThread[pool-42-thread-12,5,main]

                23:14:12,835 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-46) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:1.0, workItem=WorkItem 562 [name=async, state=0, processInstanceId=199, parameters{TaskName=async, UserIn=[Name=Rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=199, businessKey=199:562, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

                23:14:12,847 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-46) user info set

                23:14:12,849 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-46) task endedThread[pool-42-thread-46,5,main]

                23:14:13,833 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-23) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:1.0, workItem=WorkItem 563 [name=async, state=0, processInstanceId=199, parameters{TaskName=async, UserIn=[Name=Rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=199, businessKey=199:563, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

                23:14:13,841 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-23) user info set

                23:14:13,843 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-23) task endedThread[pool-42-thread-23,5,main]

                23:14:14,836 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-47) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:1.0, workItem=WorkItem 564 [name=async, state=0, processInstanceId=199, parameters{TaskName=async, UserIn=[Name=Rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=199, businessKey=199:564, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

                23:14:14,858 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-47) user info set

                23:14:14,863 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-47) task endedThread[pool-42-thread-47,5,main]

                23:14:15,835 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-24) Command executed on executor with data {deploymentId=org.jbpm.example:async-examples:1.0, workItem=WorkItem 565 [name=async, state=0, processInstanceId=199, parameters{TaskName=async, UserIn=[Name=Rahul, age=20], CommandClass=org.jbpm.examples.cmd.UserCommand}], processInstanceId=199, businessKey=199:565, callbacks=org.jbpm.executor.impl.wih.AsyncWorkItemHandlerCmdCallback}

                23:14:15,857 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-24) user info set

                23:14:15,862 INFO  [org.jbpm.examples.cmd.UserCommand] (pool-42-thread-24) task endedThread[pool-42-thread-24,5,main]

                 

                which tells there are multiple threads handling async request.
                Thank you..:-)

                1 of 1 people found this helpful