Wednesday, 14 September 2016

Developing and deploying Java Embedding activity in BPEL 2.0 in SOA Suite 11g calling a custom Java Class that has dependencies on 3rd party libraries

Java Embedded activity can call a custom Java class that relies on 3rd party Java libraries. This means that a lot of existing functionality from the Java open source community is at the disposal of the BPEL developer. This article shows a simple example of developing and deploying a BPEL process that uses Java Embedded Activity that calls a custom Java Class that uses Apache HttpClient to make Http POST calls. The article demonstrates how to develop the BPEL process, the Java Embedded activity, Java Class and how to deploy the SOA Composite application. It also presents the results of running the composite application.
Steps for a simple, straightforward demonstration:
1. Create SOA Composite application in JDeveloper 11g – based on the BPEL template
Image
Image
Image
2. Create the directory Application_Root\project\SCA-INF\lib
3. Copy 3rd party libraries to the directory created in the previous step
Image
4. Create the custom Java Class – in the SCA-INF\src directory
Image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package nl.amis.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class HttpPoster {
  public static String postMessageToUrl(String url, String message) {
      DefaultHttpClient httpClient = new DefaultHttpClient();
      String response;
        try {
            response = postToURL(url, message, httpClient);
        } catch (UnsupportedEncodingException e) { response = e.getMessage();
        } catch (IOException e) {
            response = e.getMessage();
        }catch (RuntimeException e) {
            response = e.getMessage();
        }
        httpClient.getConnectionManager().shutdown();
       return response;
  }
  private static String postToURL(String url, String message, DefaultHttpClient httpClient) throws IOException, IllegalStateException, UnsupportedEncodingException, RuntimeException {
      HttpPost postRequest = new HttpPost(url);
      StringEntity input = new StringEntity(message);
      input.setContentType("application/json");
      postRequest.setEntity(input);
      HttpResponse response = httpClient.execute(postRequest);
      if (response.getStatusLine().getStatusCode() != 200) {
          throw new RuntimeException("Failed : HTTP error code : "
                  + response.getStatusLine().getStatusCode());
      }
      BufferedReader br = new BufferedReader(
              new InputStreamReader((response.getEntity().getContent())));
      String output;
      StringBuffer totalOutput = new StringBuffer();
      System.out.println("Output from Server .... \n");
      while ((output = br.readLine()) != null) {
          System.out.println(output);
          totalOutput.append(output);
      }
      return totalOutput.toString();
  }
}
5. Add the 3rd party library JARs to the project definition in JDeveloper:
Image
6. Create the Java Embedding activity in the BPEL process
Image
7. (optionally rename the activity) and Double click the activity to start editing the Java snippet
Image
Image
1
2
3
4
5
6
7
String input = ((XMLText)getVariableData("inputVariable", "payload", "/client:process/client:input/text()")).getText();
System.out.println("Hello, World from BPEL with Java Embedded - input = "+input);
String message = "Message from BPEL process instance, invoked with input variable with value "+input;
String response = HttpPoster.postMessageToUrl(url, message);
System.out.println("response after posting message to url "+url+" is "+response);
setVariableData("outputVariable", "payload", "/client:processResponse/client:result", response);
8. Add import statements for the non-JKD classes used in the Java snippet
Image
9. Deploy the SOA Composite applications.
Note: all Java classes in SCA-INF/src and all JAR files in SCA-INF/lib are included in the SAR:
Image
10. Test-run the BPEL process through its Web Service interface
The Java snippet reads an input variable, writes some logging to the console, passes the value of the input variable to the custom Java class that performs an HTTP POST request (handled by an extremely simple Servlet that does nothing but write the request contents to the console and return a standard response). This response is put on the outputVariable by the Java snippet.
When making this call to the BPEL process:
Image
This output is found in the SOA Suite console:
Image
And the output from the Servlet is
Image
Finally the response from the BPEL process – including the response from servlet invoked by the Java snippet:

No comments:

Post a Comment