segunda-feira, 10 de novembro de 2014

Using the REST Task from jBPM 6

In this post I am going to share my experience using the BPM Suite 6 REST Task.

BPM Suite  X jBPM


jBPM is a JBoss community project to offer an open source and efficient BPM Suite and BPM Suite is a product created by Red Hat based on jBPM. In this post I will be using BPM Suite because I have it installed already, but what I am going to described should work for jBPM as well.

Authoring

 

REST Task

 The REST Task is used to invoke a REST resource (or perform an HTTP Request) from your Business Process. You can set the URL, HTTP Method, timeout and credentials from the Process Modeller interface and when the process gets into that task, it will generate a HTTP Request and return the response as String. It is available on the left side pallet under the "Service Tasks" tab:





The Target REST Web Service



The service we are going to access from our process is a really simple one, that's why it is called simple-hello-rs and it is based on JBoss helloworld-rs quickstart.. It has only one simple WEB Service that receives a String parameter called name and returns a greeting. Speaking in Java, here is the code:


The Business Process


Our process could be simpler if didn't have any  parameters to pass to the REST WS, but it would be not fun.
Our process has the following variables, all of type String:

  •  name: When starting the process, the name should be set and it will be sent to the REST WS;
  •  requestBody: It's a variable that contains the HTTP body of the request that will be performed by the REST Task;
  •  requestResponse: This variable has the response of the REST task call.


Here's our process diagram:


So we have two script task and one task used to invoke the WEB Service we described previously. Here is the explanation of each Task:

  • T1: Create Request Body: This script task will take the variable name that the user entered when the process starts and creates the body that will be stored in the requestBody  variable. Here is the script to do this:
kcontext.setVariable("requestBody", "name=" + name);
  •  T2: Perform the REST Request: This is the REST Task and here we have no code, just variables assignment. The requestBody variable will be bound to the Task's Content variable, that is used by the REST Task as the HTTP body. The variable ContentType contains the value of the Content-type HTTP header(in our case it is application/x-www-form-urlencoded), url is the URL of the WS endpoint and Method is the HTTP method of the request:
Parameters


Parameters assignment
  • T3: Prints the REST response: The last script task will simply print the response of the server
System.out.println("Response from the REST WEB Service: " + requestResponse);

Conclusion


In this quick post we show how to invoke a REST Web Service from your process in BPM Suite 6 without having to write Java code. The source code is at my github.

10 comentários:

  1. if i want to make the Url dynamic, diff for diff env, what is the best practice??

    ResponderExcluir
    Respostas
    1. I would do the same as I did for the body of the request: use a process variable that can be changed using, for example, script tasks, and assign it to the url parameter of the rest task.

      Hope this helps :)

      Excluir
  2. Thanks for the post. I am designing a workflow that has REST service task. I have specified the URL as http://localhost:8080/abcService/perform with the Content-Type as application/json.

    When I start the process, I get "ProtocolException: Target host is not specified" exception.

    Below is the stacktrace.
    Caused by: org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) [httpclient-4.3.6.jar:4.3.6]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) [httpclient-4.3.6.jar:4.3.6]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) [httpclient-4.3.6.jar:4.3.6]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) [httpclient-4.3.6.jar:4.3.6]
    at org.jbpm.process.workitem.rest.RESTWorkItemHandler.doRequestWithAuthorization(RESTWorkItemHandler.java:401) [jbpm-workitems-6.3.0.Final.jar:6.3.0.Final]
    ... 85 more
    Caused by: org.apache.http.ProtocolException: Target host is not specified
    at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:69) [httpclient-4.3.6.jar:4.3.6]
    at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124) [httpclient-4.3.6.jar:4.3.6]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183) [httpclient-4.3.6.jar:4.3.6]
    ... 89 more

    Note - I am using JBPM 6.3 and the Spring MVC based REST service is also deployed on Wildfly server

    ResponderExcluir
    Respostas
    1. That looks like a problem with the URL value. The URL must not be empty and must be a valid URL, for example:

      http://www.redhat.com

      Excluir
    2. I'm having the exact same problem with the latest JBPM distribution (6.3) - the url is valid (as in Iythia's example), and I can invoke it with curl. But when I use it as the URL parameter of the REST task, I get the same error.

      So this is not, I believe, an issue with the URL being invalid.

      Excluir
    3. I faced same issue "Caused by: org.apache.http.client.ClientProtocolException" exception. I have noticed a strange behaviour. There is an issue with "=" operator. If i assigned a value like url="http://www.google.com" its not working then i tired with kContent. It works "kcontext.setVariable("url", "http://www.google.com");".

      Excluir
    4. I am facing same problem. I debugged JBPM code as well as Apache client code to find the root cause. The issue I found is that the url gets encoded in the task definition xml. Example: http://www.google.com becomes http%3A%2F%2Fwww.google.com. This url doesn't get decoded when the request is made. One workaround is to store the url in a process variable and assign it to Url task variable. This worked fine for me.

      Excluir
  3. Hi am getting the below error while calling a service task
    Unexpected error encountered : java.lang.ClassNotFoundException:_B7085B3A-1E74-4E8A-9B37-3B5133378065_ServiceInterface

    I am using workbench 6.2 version
    please help

    ResponderExcluir