Calling a synchronous BPEL process from Java
This recipe explains how to call a synchronous BPEL process from Java. When a client calls a synchronous BPEL, it gets blocked until the BPEL process finishes the processing and returns the response. Usually, synchronous BPEL processes are designed for cases where the operation will be completed in a relatively short time. For long-running operations, we instead design asynchronous BPEL processes. This recipe will also cover how to prepare a Java package for integration with Java applications. From the client perspective, a synchronous BPEL process can be invoked in the same way a synchronous web service could. The client is most commonly called proxy
, and it is used to ease the connection between the two technologies. In our case, we would like to call the BPEL process, which is mainly the XML content, from Java and proxy is helping us to get across the gap between those two technologies.
How to do it…
In order to call a synchronous BPEL process, we will develop a Java client using the JDeveloper wizard. After completing this recipe, we will be able to call a synchronous BPEL process from the Java client application.
- First, we create another project in JDevelper. On the previously created BPEL sample project, we right-click, and select New…. From the Business Tier category, we select Web Services and then we select Web Service Proxy under Items as shown in the following screenshot:
- Then, we click on OK, which shows the welcome screen, and then we click on Next. We have to enter the WSDL location of the BPEL process in the next window. We also have the ability to choose whether we want to copy the WSDL file into the project.
Note
We can either choose the file location from the hard drive or enter the URL for the WSDL location of the BPEL process.
- In the next window, we have to enter the name of the Java packages. We have to enter the package name of the proxy files (that is, Root Package for Generated Types) and Package Name, which is where the files for the XML serialization will be placed, as shown in the following screenshot:
- We have inserted all the mandatory fields necessary to create the BPEL process proxy, so we conclude the wizard by clicking on Finish.
- In JDeveloper, we see that a number of files were generated in a separate project. The package
org.packt.bpel.sync.gen
contains the files that are used for transformation between XML and Java. - Let us check the
Process.java
file. We see that the class contains only one member as follows:@XmlElement(required = true) protected String input;
The code is annotated with the
@XmlElement
annotation, which indicates the usage of JAXB (Java Architecture for XML Binding). The JAXB implementation enables conversion from Java to XML and vice-versa. We need this conversion because data in Java is stored in objects, while BPEL holds data in XML format.Note
This variable presents the input parameter of the BPEL process. The class also contains two helper methods for setting the value and getting the value of the variable. Similarly, we can check the
ProcessResponse.java
class file which contains the output parameter of the BPEL process.The most interesting generated file is
HelloWorldProcess_ptClient.java
. This file contains skeleton prepared to call the BPEL process. - The code starts with the reference to the BPEL process. Since the BPEL process is exposed as a web service, the code is as follows:
@WebServiceRef private static Helloworldprocess_client_ep helloworldprocess_client_ep;
We can also note the
@WebServiceRef
annotation, which is used by JAX-WS (Java API for XML Web Services). The annotation indicates a reference to the web service port, or in our case, the BPEL process port. - We need to instantiate the proxy to the BPEL process and get the reference to the endpoint as follows:
helloworldprocess_client_ep = new Helloworldprocess_client_ep(); HelloWorldProcess helloWorldProcess = helloworldprocess_client_ep.getHelloWorldProcess_pt();
- We define the input and output variables as the
String
type as follows:String input = "Jurij"; String output;
- Remember that the BPEL process is taking
string
as an input. As a result, the BPEL process returns a concatenated string starting withHello
, followed by the input string, and concluded with three exclamation marks. Finally, we call the BPEL process and write results to the output as follows:output = helloWorldProcess.process(input); System.out.println("Business process returned:" + output);
- As a result of the BPEL process call, we receive the following output to JDeveloper:
- We can see that the BPEL process was executed in the Oracle Enterprise Manager Console as shown in the following screenshot:
- We can now see that the BPEL process completed successfully.
How it works…
With the wizard in JDeveloper, we prepare the Java proxy for calling the BPEL process using the wizards in JDeveloper. When the client is executed, it converts the parameters from Java types to XML types via JAX-WS. Then, the call to the BPEL process is performed, and after the BPEL process finishes, the client receives the result in XML. The XML types are then converted back to Java types.
There's more…
We can define different options when creating the BPEL process proxy as follows:
- By creating asynchronous methods
- By defining the security policies for the BPEL process
- By defining the handlers which deal with the web service messages