It is a way how to start a process by sending HTTP request to "Message Start Event", the Data Receiving Icon in process models (process definition). In this article, we will tell you about how to develop applications using the HTTP communication library of Java "HttpComponents". And sample code as well.
See the following manual page for details of Message Start Event.
We assume that a Process Model which contains a Message Start Event has been created and activated in advance.
The process Model in this sample is available to download in our archive.
Archive file of this Process Model
Message Start Event is started by receiving HTTP request.
Here we use HttpComponents as a library for HTTP communication in Java. See the following library.
If you are using Maven, library can be added by the following setting.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.2</version>
</dependency>
First, we shows the code which does not contain file type data to pass to the start messages event.
Will explain about the case that includes File type data in the next section.
First, create HttpClient Object and HttpGet or HttpPost Object.
HTTP method can be either of GET or POST when File type data is not included. Here is an example of using POST.
Check the URL to connect in the detail screen of Process Model.
Although there are URL described in the detail screens that several parameters have been specified in Query String, please exclude the Query String from a URL when you send POST. Specify later as parameters of POST.
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://s.questetra.net/00000000/System/Event/MessageStart/start");
Then set parameters.
Three parameters, processModelInfoId, nodeNumber, key, are included in the URL listed in the process model detail screen.
In addition, set Subject and process data as needed. In this sample, Subject and one String type data are added.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("processModelInfoId", "1"));
params.add(new BasicNameValuePair("nodeNumber", "0"));
params.add(new BasicNameValuePair("key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"));
// subjectparams.add(new BasicNameValuePair("title", "Subject"));
// string type
params.add(new BasicNameValuePair("data[0].input", "String type"));
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
At last, send the request.
HttpResponse response = client.execute(post);
If the process start was successful, the status code 200 response will return.
If it failed to start the process due to an error in the parameter, status code 500 response will return.
If it failed to start, the body of the response contains an XML describing the contents of the error.
The entire sample code including error processing has been published in the following locations
Sample Code to Launch Message Start Event (Java)
When handling File type data in Message Start Event, request parameters need to be sent multipart POST.
Uses MultipartEntity rather than UrlEncodedFormEntity as a parameters container.This is a sample that using FileBody.
Parameters other than File type data is specified by StringBody, File type data is specified by FileBody and ByteArrayBody.
Charset charset = Charset.forName(HTTP.UTF_8);
MultipartEntity params = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, charset);
params.addPart("processModelInfoId", new StringBody("1", charset));
params.addPart("nodeNumber", new StringBody("0", charset));
params.addPart("key", new StringBody("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", charset));
// Subject
params.addPart("title", new StringBody("Subject", charset));
// String type
params.addPart("data[0].input", new StringBody("String type", charset));
// File type
params.addPart("data[5].upload", new FileBody(new File("sample.png"), "image/png"));
post.setEntity(params);
No need to change on creating HttpClient and HttpPost, sending request, processing error.
The entire sample code including error processing has been published in the following locations
Sample Code to Launch Message Start Event: Compliant with File type data (Java)