Pular para o conteúdo principal

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.

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

Postar um comentário

Postagens mais visitadas deste blog

Simplest JavaFX ComboBox autocomplete

Based on this Brazilian community post , I've created a sample Combobox auto complete. What it basically does is: When user type with the combobox selected, it will work on a temporary string to store the typed text; Each key typed leads to the combobox to be showed and updated If backspace is type, we update the filter Each key typed shows the combo box items, when the combobox is hidden, the filter is cleaned and the tooltip is hidden:   The class code and a sample application is below. I also added the source to my personal github , sent me PR to improve it and there are a lot of things to improve, like space and accents support.

Genetic algorithms with Java

One of the most fascinating topics in computer science world is Artificial Intelligence . A subset of Artificial intelligence are the algorithms that were created inspired in the nature. In this group, we have Genetic Algorithms  (GA). Genetic Algorithms  To find out more about this topic I recommend the following MIT lecture and the Nature of Code book and videos created by Daniel Shiffman. Genetic Algorithms using Java After I remembered the basics about it, I wanted to practice, so I tried my own implementation, but I would have to write a lot of code to do what certainly others already did. So I started looking for Genetic Algorithm libraries and found Jenetics , which is a modern library that uses Java 8 concepts and APIs, and there's also JGAP . I decided to use Jenetics because the User Guide was so clear and it has no other dependency, but Java 8. The only thing I missed for Jenetics are more small examples like the ones I will show i...

Dancing lights with Arduino - The idea

I have been having fun with Arduino these days! In this article I am going to show how did I use an electret mic with Arduino to create a Dancing Lights circuit. Dancing Lights   I used to be an eletronician before starting the IT college. I had my own electronics maintenance office to fix television, radios, etc. In my free time I used to create electronic projects to sell and I made a few "reais" selling a version of Dancing lights, but it was too limited: it simply animated lamps using a relay in the output of a 4017 CMOS IC. The circuit was a decimal counter  controlled by a 555. 4017 decimal counter. Source in the image When I met Arduino a few years ago, I was skeptical because I said: I can do this with IC, why should I use a microcontroller. I thought that Arduino was for kids. But now my pride is gone and I am having a lot of fun with Arduino :-) The implementation of Dancing Lights with Arduino uses an electret mic to capture the sound and light leds...