Book Image

BPEL and Java Cookbook

By : Jurij Laznik
Book Image

BPEL and Java Cookbook

By: Jurij Laznik

Overview of this book

The Business Process Execution Language (BPEL) has become the de-facto standard for orchestrating web services. BPEL and web services are both clamped into Service-oriented Architecture (SOA). Development of efficient SOA composites too often requires usage of other technologies or languages, like Java. This Cookbook explains through the use of examples how to efficiently integrate BPEL with custom Java functionality.If you need to use BPEL programming to develop web services in SOA development, this book is for you.BPEL and Java Cookbook will show you how to efficiently integrate custom Java functionality into BPEL processes. Based on practical examples, this book shows you the solutions to a number of issues developers come across when designing SOA composite applications. The integration between the two technologies is shown two-fold; the book focuses on the ways that Java utilizes the BPEL and vice-versa.With this book, you will take a journey through a number of recipes that solve particular problems with developing SOA composite applications. Each chapter works on a different set of recipes in a specific area. The recipes cover the whole lifecycle of developing SOA composites: from specification, through design, testing and deployment. BPEL and Java Cookbook starts off with recipes that cover initiation of BPEL from Java and vice-versa. It then moves on to logging and tracing facilities, validation and transformation of BPEL servers, embedding of third-party Java libraries into BPEL. It also covers manipulation with variables in BPEL different techniques of Java code wrapping for web service usage and utilization of XML fa?ßades. After reading BPEL and Java Cookbook you will be able to circumvent many of the issues that developers experience during SOA composite application development.  
Table of Contents (18 chapters)
BPEL and Java Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

  1. 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:

  2. 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.

  3. 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:

  4. We have inserted all the mandatory fields necessary to create the BPEL process proxy, so we conclude the wizard by clicking on Finish.

  5. 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.

  6. 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.

  7. 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.

  8. 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();
  9. We define the input and output variables as the String type as follows:

      String input = "Jurij";
      String output;
  10. Remember that the BPEL process is taking string as an input. As a result, the BPEL process returns a concatenated string starting with Hello, 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);
  11. As a result of the BPEL process call, we receive the following output to JDeveloper:

  12. We can see that the BPEL process was executed in the Oracle Enterprise Manager Console as shown in the following screenshot:

  13. 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