2 Replies Latest reply on Mar 12, 2013 10:48 PM by thomas.setiabudi

    Invoke Human Task Flow - Using API

    balajiora

      I am new to JBPM and I appreciate, if you help me in below points.

       

      1. How to Invoke human task flow and complete using Knowledge API?

       

      (Ex: I have deployed the simple human workflow in Guvnor, logged in as Krisv and created the task.  I do not want to login as Mary to act on the work item in JBPM Console, instead using JBPM API;I need to complete this task)

       

      2. Example to read the instances deployed in Guvnor.

       

      3. How to pause / suspend the Process using JBPM API?

       

      Balaji S

        • 1. Re: Invoke Human Task Flow - Using API
          roxy1987

          To start a process

           

           

          public static void startProc() throws Exception
           {
            try {
             KnowledgeBase kbase = readKnowledgeBase();
             StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
             KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newThreadedFileLogger(ksession, "test", 1000);
             ksession.startProcess("com.sample.demoProc");
             if(humanTaskHandler.isConnected())
             {
              humanTaskHandler.dispose();
             }
             System.out.println("Process started ...");
             logger.close();
            } catch (Throwable t) {
             t.printStackTrace();
            }
           }
           private static KnowledgeBase readKnowledgeBase() throws Exception {
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
            kbuilder.add(ResourceFactory.newClassPathResource("demoProc.bpmn"), ResourceType.BPMN2);
            return kbuilder.newKnowledgeBase();
           }
           
           private static StatefulKnowledgeSession createKnowledgeSession(KnowledgeBase kbase) {
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            humanTaskHandler = new HornetQHTWorkItemHandler(ksession);
            humanTaskHandler.setIpAddress("localhost");
            humanTaskHandler.setPort(5153);
            ksession.getWorkItemManager().registerWorkItemHandler("Human Task", humanTaskHandler);  
            return ksession;
           }
          
          

           

          To complete a human task

           

           

          public static void completeTask(long taskId, String userId) throws Exception
           {
            String name = "client 1"+UUID.randomUUID();
            TaskClient client = new TaskClient(new HornetQTaskClientConnector(name, new HornetQTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
            BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();
            ContentData contentData = null;
            client.connect(ipAddress, port);
            try
            {
             client.start(taskId, userId, responseHandler);
             responseHandler = new BlockingTaskOperationResponseHandler();
             responseHandler.waitTillDone(5000);
             client.complete(taskId, userId, contentData, responseHandler);
             responseHandler.waitTillDone(5000);
             BpmAPI.completeWorkItem(taskId);
            }
            catch(Exception e)
            {
             BpmExceptionHandler.handleException(e);
            }
            finally
            {
             if(client != null)
             {
              client.disconnect();
             } 
            }
           }

           

          To Pause/Suspend

           

           

          public static void completeTask(long taskId, String userId, Map<String, Object> map) throws Exception
           {
            String name = "client 1"+UUID.randomUUID();
            TaskClient client = new TaskClient(new HornetQTaskClientConnector(name, new HornetQTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
            BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();
            client.connect(ipAddress, port);
            try
            {
             client.suspend(taskId, userId, responseHandler);
             responseHandler.waitTillDone(5000);
            }
            catch(Exception e)
            {
             BpmExceptionHandler.handleException(e);
            }
            finally
            {
             if(client != null)
             {
              client.disconnect();
             } 
            }
           }
          
          

           

          I dont use Guvnor so cant say about that.

           

          Regards.

          1 of 1 people found this helpful
          • 2. Re: Invoke Human Task Flow - Using API
            thomas.setiabudi

            Hi Balaji Subramaniam,

             

            About getting process definition from guvnor, you can see how JBPM Console server do it in

             

            https://github.com/droolsjbpm/jbpm/blob/master/jbpm-gwt/jbpm-gwt-core/src/main/java/org/jbpm/integration/console/kbase/DefaultKnowledgeBaseManager.java

             

            They have a utility to connect to guvnor

             

            https://github.com/droolsjbpm/jbpm/blob/master/jbpm-gwt/jbpm-gwt-shared/src/main/java/org/jbpm/integration/console/shared/GuvnorConnectionUtils.java

             

             

            Basically the idea is to build a knowledge agent that has all process definition from guvnor, then create a knowledge base using that knowledge agent

             

            Regards,

            Thomas Setiabudi