14 Replies Latest reply on Mar 27, 2016 3:33 PM by zouhaoui

    Remote Rest Api - Evaluation Example

    damien.s

      @

      Hi everybody,

       

      I'm trying to execute the Evaluation example with the remote rest api (deploying jbpm 6.0).

      When i'm using the jbpm console to create new instance of this wf, it's works fine. I view each task for each use (krisv, john, mary) in the good order.

      But when i'm using a junit test calling jbpm with REST API i cant retrieve the tasks for john or mary. The first task for krisv works.

       

      Here is my code :

                URL baseURl = new URL("http", "localhost", 8080, "/jbpm-console/");
                RemoteRestRuntimeFactory  restSessionFactory  = new RemoteRestRuntimeFactory("org.jbpm:Evaluation:1.0", baseURl, "krisv", "krisv");
                RuntimeEngine engine = restSessionFactory.newRuntimeEngine();
                KieSession ksession = engine.getKieSession();
                
                // start a new process instance
                Map params = new HashMap();
                params.put("employee", "krisv");
                params.put("reason", "Yearly performance evaluation");
                ProcessInstance processInstance = 
                     ksession.startProcess("evaluation", params);
                System.out.println("Process started ...");
                
                // complete Self Evaluation
                TaskService taskService = engine.getTaskService();
                List tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");
      
                TaskSummary task = tasks.get(0);
                for(TaskSummary tasksummarry : tasks) {
                     if (tasksummarry.getProcessInstanceId() == processInstance.getId())  {
                          task = tasksummarry;
                          break;
                     }
                }
      
                System.out.println("'krisv' completing task " + task.getName() + ": " + task.getDescription());
                taskService.start(task.getId(), "krisv");
                Map results = new HashMap();
                results.put("performance", "exceeding");
                taskService.complete(task.getId(), "krisv", results);
      
                // john from HR
                taskService = engine.getTaskService();
                tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
                task = null;
                for(TaskSummary tasksummarry : tasks) {
                     if (tasksummarry.getProcessInstanceId() == processInstance.getId())  {
                          task = tasksummarry;
                          break;
                     }
                }
                System.out.println("'john' completing task " + task.getName() + ": " + task.getDescription());
                taskService.claim(task.getId(), "john");
                taskService.start(task.getId(), "john");
                results = new HashMap();
                results.put("performance", "acceptable");
                taskService.complete(task.getId(), "john", results);
      
                // mary from PM
                tasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
                task = null;
                for(TaskSummary tasksummarry : tasks) {
                     if (tasksummarry.getProcessInstanceId() == processInstance.getId())  {
                          task = tasksummarry;
                          break;
                     }
                }
                System.out.println("'mary' completing task " + task.getName() + ": " + task.getDescription());
                taskService.claim(task.getId(), "mary");
                taskService.start(task.getId(), "mary");
                results = new HashMap();
                results.put("performance", "outstanding");
                taskService.complete(task.getId(), "mary", results);
      
                assertProcessInstanceCompleted(processInstance.getId(), ksession);
                System.out.println("Process instance completed");

       

      May be i missed an call ? or anything else ? or there is a problem in the remote api.

       

      Thanks for your help.

        • 1. Re: Remote Rest Api - Evaluation Example
          salaboy21

          Do you see any error or exception? or the task is not there only?

          • 2. Re: Remote Rest Api - Evaluation Example
            damien.s

            In my test, when i execute :

             

            tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");

             

            The result list(tasks) is empty and i see no exception or error in my jboss log.

             

            In database, in the PEOPLEASSIGNMENTS_POTOWNERS, i see tasks link to krisv (user), HR or PM (group).

            When i log in the jbpm console with john i see the task.

            • 3. Re: Remote Rest Api - Evaluation Example
              salaboy21

              That sounds ultra strange.. unless the queries are not working in the rest interface.

              Are you using the latest snapshots to see if the same behaviour popups up?

               

              Try using 6.1.0-SNAPSHOT please.

              • 4. Re: Remote Rest Api - Evaluation Example
                damien.s

                I will try with the latest snapshot.

                 

                 

                I'll let you know the result.

                 

                • 5. Re: Remote Rest Api - Evaluation Example
                  marcj

                  Same behavior here. The jBPM Console shows several tasks available for John, but somehow the query doesn't seem to work. The task list is empty...

                   

                  @Damien: Does changing "List tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");" to  "List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");" change anything for you?

                  • 6. Re: Remote Rest Api - Evaluation Example
                    marco.rietveld

                    Hi Damien,

                     

                    It looks like you need to change users when you query for john's and mary's tasks: only john has access to tasks assigned to john, and the same goes for mary.

                     

                    I believe you'll see the same behaviour in jbpm-console?

                    1 of 1 people found this helpful
                    • 7. Re: Remote Rest Api - Evaluation Example
                      marcj

                      Hi Marco,

                      just tried that:

                      .....

                      // john from HR

                              user = "john";

                              password = "john";

                              restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                              engine = restSessionFactory.newRuntimeEngine();

                              ksession = engine.getKieSession();

                             

                              taskService = engine.getTaskService();

                              tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK"); 

                              task = tasks.get(0);

                      .....

                       

                      Works perfect, now the tasks list contains all tasks assigned to john.

                      • 8. Re: Remote Rest Api - Evaluation Example
                        marcj

                        So after all this Rest test should work:

                         

                         

                        ____________________________________

                         

                         

                        package com.sample;

                         

                         

                        import static org.junit.Assert.*;

                        import java.net.MalformedURLException;

                        import java.net.URL;

                        import java.util.HashMap;

                        import java.util.List;

                        import java.util.Map;

                         

                         

                        import org.jbpm.test.JbpmJUnitBaseTestCase;

                        import org.junit.Test;

                        import org.kie.api.KieServices;

                        import org.kie.api.logger.KieRuntimeLogger;

                        import org.kie.api.runtime.KieSession;

                        import org.kie.api.runtime.manager.RuntimeEngine;

                        import org.kie.api.runtime.manager.RuntimeManager;

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

                        import org.kie.api.task.TaskService;

                        import org.kie.api.task.model.TaskSummary;

                        import org.kie.services.client.api.RemoteRestRuntimeFactory;

                        import org.kie.services.client.api.command.RemoteRuntimeEngine;

                         

                         

                        /**

                        * This is a sample test of the evaluation process.

                        */

                        @SuppressWarnings({"unused", "restriction", "unchecked", "rawtypes"})

                        public class RestTest {

                         

                         

                          @Test

                          public void test() throws MalformedURLException {

                          String deploymentId = "org.jbpm:Evaluation:1.0";

                                URL appUrl = new URL("http://localhost:8080/jbpm-console/");

                                String user = "krisv";

                                String password = "krisv";

                         

                         

                          RemoteRestRuntimeFactory  restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                                RemoteRuntimeEngine engine = restSessionFactory.newRuntimeEngine();

                         

                         

                                KieSession ksession = engine.getKieSession();

                             

                                // start a new process instance

                                Map params = new HashMap();

                                params.put("employee", "krisv");

                                params.put("reason", "Performance Evaluation");

                                ProcessInstance processInstance =

                                     ksession.startProcess("evaluation", params);

                                System.out.println("Process started ...");

                             

                             // complete Self Evaluation

                                TaskService taskService = engine.getTaskService();

                                List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");

                         

                         

                                TaskSummary task = tasks.get(0);

                                for(TaskSummary tasksummary : tasks) {

                                     if (tasksummary.getProcessInstanceId() == processInstance.getId())  {

                                          task = tasksummary;

                                          break;

                                     }

                                }

                             

                                System.out.println("'krisv' completing task " + task.getName() + ": " + task.getDescription());

                                taskService.start(task.getId(), "krisv");

                                Map results = new HashMap();

                                results.put("performance", "exceeding");

                                taskService.complete(task.getId(), "krisv", results);

                          

                             // john from HR

                                user = "john";

                                password = "john";

                                restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                                engine = restSessionFactory.newRuntimeEngine();

                                ksession = engine.getKieSession();

                             

                                taskService = engine.getTaskService();

                                tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");

                                task = tasks.get(0);

                             

                                for(TaskSummary tasksummary : tasks) {

                                     if (tasksummary.getProcessInstanceId() == processInstance.getId())  {

                                          task = tasksummary;

                                          break;

                                     }

                                }

                             

                                System.out.println("'john' completing task " + task.getName() + ": " + task.getDescription());

                                taskService.claim(task.getId(), "john");

                                taskService.start(task.getId(), "john");

                                results = new HashMap();

                                results.put("performance", "acceptable");

                                taskService.complete(task.getId(), "john", results);

                             

                             // mary from PM

                                user = "mary";

                                password = "mary";

                                restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                                engine = restSessionFactory.newRuntimeEngine();

                                ksession = engine.getKieSession();

                             

                                taskService = engine.getTaskService();

                                tasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");

                                task = tasks.get(0);

                                for(TaskSummary tasksummarry : tasks) {

                                     if (tasksummarry.getProcessInstanceId() == processInstance.getId())  {

                                          task = tasksummarry;

                                          break;

                                     }

                                }

                                System.out.println("'mary' completing task " + task.getName() + ": " + task.getDescription());

                                taskService.claim(task.getId(), "mary");

                                taskService.start(task.getId(), "mary");

                                results = new HashMap();

                                results.put("performance", "outstanding");

                                taskService.complete(task.getId(), "mary", results);

                         

                         

                                System.out.println("Process instance completed");

                          }

                        }

                        • 9. Re: Remote Rest Api - Evaluation Example
                          vrotat

                          Hi everybody,

                           

                          I was wondering why it is required to set up the session each time we changed the user ?

                          -----------------

                          restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                          engine = restSessionFactory.newRuntimeEngine();

                          ksession = engine.getKieSession();

                          -----------------

                           

                          Why it is not possible to retrieve the potential task of a specified user when this is not the one related to the session ? It seems a bit logical but is this the only way to do (change the session each time retrieving the potential tasks of each user of the process)?

                           

                          Thanks in advance

                          • 10. Re: Remote Rest Api - Evaluation Example
                            damien.s

                            The Test posting by

                            Finally, I had forgot to change the logged user.

                            • 11. Re: Remote Rest Api - Evaluation Example
                              guyr

                              FYI, I've been learning the ins and outs of the REST (and JMS) remote APIs over the last couple days.  With REST, I discovered that when you create the RemoteRestRuntimeFactory, the userid you specify must belong to the same groups as the users you wish to query.  I originally started out using the admin user, which only belongs to groups admin,analyst.  So, when I tried to getTasksAssignedAsPotentialOwner for mary, JBPM returned no tasks; that's because mary belongs to groups analyst,HR.  Since admin doesn't belong to HR, JBPM will not report any tasks for that group.

                               

                              The solution is to create RemoteRestRuntimeFactory using user salaboy, who belongs to *all* groups.  Then, you can getTasksAssignedAsPotentialOwner for any user (mary, john, krisv).  Since salaboy belongs to all groups, you'll get tasks reported for any of them.

                               

                              Hope this helps.

                              • 12. Re: Remote Rest Api - Evaluation Example
                                marcj

                                Hi Guy,

                                that is quite interesting - I'll test  that right away. Did you discover a similar behavior with JMS? So in practice, a "technical user" with a membership in all existing groups would be a convenient solution for testing.

                                • 13. Re: Remote Rest Api - Evaluation Example
                                  guyr

                                  Unfortunately, I had no luck with JMS.  Setting it up was easy; I've posted in another thread a setup routine that will allow code to use the JMS and REST remote runtime engines interchangeably; the remainder of your code is unaffected.  However, when I used the REST version, I got back 3 tasks, while when I used JMS, I got back no tasks. I opened a new discussion thread to ask about that, but at this point I consider the JMS implementation non-functional.  Before opening that discussion thread, I searched this forum for "JMS" and got only 3 hits.  Apparently, there isn't much interest in it, so I'm not surprised at its current state.

                                  • 14. Re: Remote Rest Api - Evaluation Example
                                    zouhaoui

                                    after debug for your code a i have this warnings and errors , i use eclipse kepler ,JDK 1.8 and jbpm version 6.3.0 Capture.PNG

                                    code java

                                    package com.sample;

                                     

                                     

                                    import static org.junit.Assert.*;

                                    import java.net.MalformedURLException;

                                    import java.net.URL;

                                    import java.util.HashMap;

                                    import java.util.List;

                                    import java.util.Map;

                                     

                                     

                                    import org.jbpm.test.JbpmJUnitBaseTestCase;

                                    import org.junit.Test;

                                    import org.kie.api.KieServices;

                                    import org.kie.api.logger.KieRuntimeLogger;

                                    import org.kie.api.runtime.KieSession;

                                    import org.kie.api.runtime.manager.RuntimeEngine;

                                    import org.kie.api.runtime.manager.RuntimeManager;

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

                                    import org.kie.api.task.TaskService;

                                    import org.kie.api.task.model.TaskSummary;

                                    import org.kie.services.client.api.RemoteRestRuntimeFactory;

                                    import org.kie.services.client.api.command.RemoteRuntimeEngine;

                                     

                                     

                                    /**

                                    * This is a sample test of the evaluation process.

                                    */

                                    @SuppressWarnings({"unused", "restriction", "unchecked", "rawtypes"})

                                    public class RestTest {

                                     

                                     

                                      @Test

                                      public void test() throws MalformedURLException {

                                      String deploymentId = "org.jbpm:Evaluation:1.0";

                                            URL appUrl = new URL("http://localhost:8282/kie-wb/");

                                            String user = "krisv";

                                            String password = "krisv";

                                     

                                     

                                      RemoteRestRuntimeFactory  restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                                            RemoteRuntimeEngine engine = restSessionFactory.newRuntimeEngine();

                                     

                                     

                                            KieSession ksession = engine.getKieSession();

                                        

                                            // start a new process instance

                                            Map params = new HashMap();

                                            params.put("employee", "krisv");

                                            params.put("reason", "Performance Evaluation");

                                            ProcessInstance processInstance =

                                                 ksession.startProcess("evaluation", params);

                                            System.out.println("Process started ...");

                                        

                                         // complete Self Evaluation

                                            TaskService taskService = engine.getTaskService();

                                            List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");

                                     

                                     

                                            TaskSummary task = tasks.get(0);

                                            for(TaskSummary tasksummary : tasks) {

                                                 if (tasksummary.getProcessInstanceId() == processInstance.getId())  {

                                                      task = tasksummary;

                                                      break;

                                                 }

                                            }

                                        

                                            System.out.println("'krisv' completing task " + task.getName() + ": " + task.getDescription());

                                            taskService.start(task.getId(), "krisv");

                                            Map results = new HashMap();

                                            results.put("performance", "exceeding");

                                            taskService.complete(task.getId(), "krisv", results);

                                     

                                         // john from HR

                                            user = "john";

                                            password = "john";

                                            restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                                            engine = restSessionFactory.newRuntimeEngine();

                                            ksession = engine.getKieSession();

                                        

                                            taskService = engine.getTaskService();

                                            tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");

                                            task = tasks.get(0);

                                        

                                            for(TaskSummary tasksummary : tasks) {

                                                 if (tasksummary.getProcessInstanceId() == processInstance.getId())  {

                                                      task = tasksummary;

                                                      break;

                                                 }

                                            }

                                        

                                            System.out.println("'john' completing task " + task.getName() + ": " + task.getDescription());

                                            taskService.claim(task.getId(), "john");

                                            taskService.start(task.getId(), "john");

                                            results = new HashMap();

                                            results.put("performance", "acceptable");

                                            taskService.complete(task.getId(), "john", results);

                                        

                                         // mary from PM

                                            user = "mary";

                                            password = "mary";

                                            restSessionFactory  = new RemoteRestRuntimeFactory(deploymentId, appUrl, user, password);

                                            engine = restSessionFactory.newRuntimeEngine();

                                            ksession = engine.getKieSession();

                                        

                                            taskService = engine.getTaskService();

                                            tasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");

                                            task = tasks.get(0);

                                            for(TaskSummary tasksummarry : tasks) {

                                                 if (tasksummarry.getProcessInstanceId() == processInstance.getId())  {

                                                      task = tasksummarry;

                                                      break;

                                                 }

                                            }

                                            System.out.println("'mary' completing task " + task.getName() + ": " + task.getDescription());

                                            taskService.claim(task.getId(), "mary");

                                            taskService.start(task.getId(), "mary");

                                            results = new HashMap();

                                            results.put("performance", "outstanding");

                                            taskService.complete(task.getId(), "mary", results);

                                     

                                     

                                            System.out.println("Process instance completed");

                                      }

                                    }