Using the REST URLs from a web application to control a process and its tasks in JBPM 6

Version 1

    I've been exploring JBPM 6 the past few weeks. My objective was to be able to access and control a process created in JBPM using the web console from an external web application. The documentation specifies 2 ways of doing this: Using the Remote Java API and the REST URL calls.

     

    I faced some exceptions when I tried using the RemoteRestRuntimeFactory and RemoteJmsRuntimeEngineFactory classes. But the attempt using the REST calls was successful. I'll briefly explain with some snippets of the code of how it was done. I used Apache's HttpClient (org.apache.http) to make the REST calls. The below code snippets can start the Evaluation process already present in the JBPM server and can then start and complete the tasks within that process.

     

    //common variables defined

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

    private String url = "http://localhost:8080/jbpm-console/";

    private String user = "krisv";

    private String password = "krisv";

    private String process = "evaluation";

     

    //Initializing the HttpClient

    private void initializeHttpClient() {

      try {

           httpclient = new DefaultHttpClient();

           credentials = new UsernamePasswordCredentials(user, password);

           scheme = new BasicScheme();

      } catch (Exception ex) {

           ex.printStackTrace();

      }

      }

     

    //Starting the process

    private void startProcessRestMethod() {

           restCall = "http://" + "localhost" + ":" + "8080" + "/"

                + "jbpm-console" + "/rest/runtime/" + deploymentId

                + "/process/" + process + "/start";

     

           try {

                httppost = new HttpPost(restCall);

                authorizationHeader = scheme.authenticate(credentials, httppost);

                httppost.addHeader(authorizationHeader);

                response = httpclient.execute(httppost);

     

                if (response != null) {

                log.info("Response status line: " + response.getStatusLine());

                if (response.getStatusLine() != null) {

                     log.info("Response status code: "

                          + response.getStatusLine().getStatusCode());

                }

     

                entity = response.getEntity();

     

                if (entity != null) {

                     String output = EntityUtils.toString(entity);

                     createProcessInstanceResponse(output);

                }

           }

          } catch (ClientProtocolException ex) {

                ex.printStackTrace();

           } catch (AuthenticationException ex) {

                ex.printStackTrace();

           } catch (Exception ex) {

                ex.printStackTrace();

           } finally {

                closeHttpClient();

           }

      }

     

    //Getting the process start response

    private void createProcessInstanceResponse(String output) {

           try {

                JAXBContext jaxbContext = JAXBContext.newInstance(JaxbProcessInstanceResponse.class);

                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

                processResponse = (JaxbProcessInstanceResponse) unmarshaller.unmarshal(new StringReader(output));

                log.info("processResponse: " + processResponse);

                log.info("id: " + processResponse.getId());

                log.info("process id: " + processResponse.getProcessId());

                log.info("process state: " + processResponse.getState());

           } catch (Exception e) {

                e.printStackTrace();

           }

      }

     

    //Getting tasks for a user

    private void getTasksForUser() {

           // get list of tasks

           restCall = "http://" + "localhost" + ":" + "8080" + "/"

                + "jbpm-console" + "/rest/task/query?taskOwner=" + user;

     

           try {

     

                httpget = new HttpGet(restCall);

                authorizationHeader = scheme.authenticate(credentials, httpget);

                httpget.addHeader(authorizationHeader);

                response = httpclient.execute(httpget);

     

                if (response != null) {

                     log.info("Task Response status line: "+ response.getStatusLine());

                     if (response.getStatusLine() != null) {

                          log.info("Task Response status code: "+ response.getStatusLine().getStatusCode());

                     }

     

                entity = response.getEntity();

                String output = EntityUtils.toString(entity);

     

                if (entity != null) {

                     createTaskSummaryResponse(output);

                }

                }

           } catch (ClientProtocolException ex) {

                ex.printStackTrace();

           } catch (AuthenticationException ex) {

                ex.printStackTrace();

           } catch (Exception ex) {

                ex.printStackTrace();

           } finally {

                closeHttpClient();

           }

    }

     

    //Process the response for the task query

    private void createTaskSummaryResponse(String output) {

      try {

           JAXBContext jaxbContext = JAXBContext.newInstance(JaxbTaskSummaryListResponse.class);

           Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

           JaxbTaskSummaryListResponse response = (JaxbTaskSummaryListResponse) unmarshaller.unmarshal(new StringReader(output));

           taskSummary = response.getResult();

           log.info("taskSummary:"+ taskSummary+ ", length:"+ (taskSummary != null ? taskSummary.size(): "taskSummary is null"));

      } catch (Exception ex) {

           ex.printStackTrace();

      }

      }

     

    //Start a task

    private void startTask(String taskId) {

           restCall = "http://" + "localhost" + ":" + "8080" + "/"

                + "jbpm-console" + "/rest/task/" + taskId + "/start";

     

           try {

                httppost = new HttpPost(restCall);

                authorizationHeader = scheme.authenticate(credentials, httppost);

                httppost.addHeader(authorizationHeader);

                response = httpclient.execute(httppost);

     

                if (response != null) {

                     log.info("Task start Response status line: "+ response.getStatusLine());

                     if (response.getStatusLine() != null) {

                          log.info("Task start Response status code: "+ response.getStatusLine().getStatusCode());

                }

                }

           } catch (ClientProtocolException ex) {

                ex.printStackTrace();

           } catch (AuthenticationException ex) {

                ex.printStackTrace();

           } catch (Exception ex) {

                ex.printStackTrace();

           } finally {

                closeHttpClient();

           }

    }


    //Complete a task

    private void completeTask(String taskId) {

           restCall = "http://" + "localhost" + ":" + "8080" + "/"

                + "jbpm-console" + "/rest/task/" + taskId + "/complete";

     

      try {

      httppost = new HttpPost(restCall);

      authorizationHeader = scheme.authenticate(credentials, httppost);

      httppost.addHeader(authorizationHeader);

      response = httpclient.execute(httppost);

     

      if (response != null) {

           log.info("Task complete Response status line: "+ response.getStatusLine());

           if (response.getStatusLine() != null) {

                log.info("Task complete Response status code: "+ response.getStatusLine().getStatusCode());

           }

      }

      } catch (ClientProtocolException ex) {

           ex.printStackTrace();

      } catch (AuthenticationException ex) {

           ex.printStackTrace();

      } catch (Exception ex) {

           ex.printStackTrace();

      } finally {

           closeHttpClient();

      }

      }