Exposing an EJB session bean as a service on the OSB using the EJB transport

As the version of OSB is 11gR1 we can use an EJB version 3.0 session bean in an OSB business service. Before 11gR1, only EJB version 2.1 was supported.

In this recipe, we will create and test a business service—JB and JEJB transport. The business service calls a session bean that returns the Java object in XML or as a Java object with the JEJB transport.

Exposing an EJB session bean as a service on the OSB using the EJB transport

Getting ready

Make sure that the EJB session bean is deployed to the OSB server as shown in the Introduction section of this chapter.

How to do it...

First we will have to register an EJB client JAR as a resource in Oracle Service Bus. The EJB client JAR contains the necessary interfaces and classes needed by the business service to access the EJB session bean.

In Eclipse OEPE, perform the following steps:

  1. Create a new OSB project and name it exposing-session-bean-as-webservice and create a jar, business, and proxy folder within it.
  2. Open a file explorer window and navigate to the ejb-jdev-workspaceejbdeploy folder.
  3. Drag the customerManagementInterface.jar file to the Eclipse OEPE and drop it on the jar folder of the project, as shown in the following screenshot:
    How to do it...
  4. On the File Operation pop-up window, select the Copy files option, and click on OK.

    Next we create the business service and register the EJB client JAR within the EJB transport configuration. In Eclipse OEPE, perform the following steps:

  5. In the business folder, create a new business service and name it CustomerManagement.
  6. On the General tab, select the Transport Typed Service option for Service Type.
  7. Navigate to the Transport tab.
  8. Select ejb for the Protocol.
  9. In the Endpoint URI field enter ejb::CustomerManagementService#cookbook.model.services.CustomerManagement
  10. Click on Add to add the URL to the Existings URIs.
  11. Navigate to the EJB Transport tab.
  12. Enable the EJB 3.0 checkbox.
  13. Click on Browse on the Client Jar field, select the customerManagementInterface.jar and click OK.
  14. Based on the information in the EJB client JAR, Eclipse OEPE shows the available methods with the signatures.
  15. Rename the Return name of the findAllCustomers operation to customers.
  16. Rename the first parameter in the Parameters section of the findCustomer operation to id.
  17. Rename the Return name of the findCustomer operation to customer:
    How to do it...
  18. Deploy the project to the OSB server.

    Now we can already test the business service directly through the OSB console. Perform the following steps:

  19. In the Project Explorer tree on the left, select the business folder of the exposing-session-bean-as-webservice project.
  20. Click on the Launch Test Console icon of the CustomerManagement business service.
  21. Select the findCustomer item from the Available Operations drop-down listbox.
  22. Change the id of the request payload to 100 and click on Execute.
  23. The Response Document section shows the response of the EJB automatically transformed to XML:
    How to do it...

    This business service, similar to any other business service, cannot be invoked from outside of OSB. To make it available, we can expose it as a web service.

    In order to expose the business service as a web service, we need to know the WSDL of the business service. To retrieve the WSDL of the business service, perform the following steps in the Service Bus console:

  24. In the Project Explorer tree on the left, select the business folder of the exposing-session-bean-as-webservice project.
  25. Click on the Export WSLD icon of the CustomerManagement business service.
  26. On the pop-up window click on Save File.
  27. Open the exported file CustomerManagment_wsdl.jar with an unzip program such as WinRAR.

    In Eclipse OEPE, perform the following steps:

  28. Create a wsdl folder in the OSB project.
  29. Drag the CustomerManagement.wsdl file from WinRAR and drop it on the wsdl folder.
    How to do it...
  30. On the File Operation pop-up window, select Copy Files, and click on OK.
  31. Create a new proxy service and name it CustomerManagement.
  32. Select WSDL Web Service as the Service Type.
  33. Click on Browse, navigate to the CustomerManagement.wsdl and select the JwsServiceSoapBinding (binding) node.
  34. Click on OK.
  35. Navigate to the MessageFlow tab.
  36. Drag a Route node to the message flow, name it InvokeEJBRoute, and add a Routing action.
  37. On the Properties tab of the Routing action, click on Browse and select the CustomerManagement.biz business service.
  38. Select the Use inbound operation for outbound option.
  39. Deploy the project to the OSB server.

Test the web service by either using Service Bus console or soapUI as shown in Chapter 1, Testing the proxy service through soapUI. Make sure that you test the findCustomer operation and pass 100 for the id.

How it works...

We have configured a business service to use the EJB transport so that we can invoke it from the message flow. The EJB transport uses the JAX-RPC stack to perform the Java to XML bindings and vice versa. That's why we have seen the response returned from the business service formatted as XML. The EJB transport natively supports all primitive types, XmlObject, schema-generated XML Beans, and JavaBean classes. If an EJB method uses parameters/return types that are not supported by the JAX-RPC engine or do not map directly to XML, an error will occur. This happens if Java Collections like List, Set, or Map objects are used. In order to support such cases, a custom converter needs to be written and registered to the EJB transport.

This business service can be used like any other business service. It provides a WSDL, holding the definition of the service interface. Such a business service can be used in Publish, Service Callout, and Routing actions.

We have shown the latter in this recipe and implemented a proxy service that exposes the functionality of the EJB session bean on the OSB as a SOAP-based web service. We have directly reused the WSDL the business service provides for simplicity. This should be used with care, as it tightly couples the consumer with the EJB session bean. It's usually far better to let the proxy service implement its own service interface based on a canonical data model, and then use a transformation action to convert between the different XML formats as demonstrated in Chapter 1, Using an XQuery transformation to map between different data models of the services.

There's more...

Locate an EJB in the JNDI tree

If we do not know the JNDI name for an EJB, we can browse the EJB Server JNDI tree. In the WebLogic console, perform the following steps:

  1. Click on Environment of the Domain Structure tree.
  2. Click on Servers on the detail window.
  3. Click on the OSB server, that is, AdminServer (admin) if it is an admin-only installation.
  4. Click on View JNDI Tree:
    Locate an EJB in the JNDI tree
  5. A new window with the JNDI Tree will show up.
  6. In the JNDI Tree Structure, navigate to the CustomerManagementService EJB session bean:
    Locate an EJB in the JNDI tree

Navigate to the model | services | CustomerManagement interface and the Binding Names on the detail view on the right side shows the JNDI name of the session bean.

What if the EJB method returns a list of objects?

If we invoke the findAllCustomers method, which returns a list of Customer instances, then the list is returned as a soap array in the result and the items of the collection are untyped. This can be seen in the following screenshot:

What if the EJB method returns a list of objects?

The reason for this is that the JAX RPC engine can't infer the type in the collection. Returning such a soap array can cause some serious interoperability issues, so we should try to avoid that.

The recipe, Using converter class with EJB transport to help converting data types, will show how this can be fixed by implementing a Converter class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset