Table of Content
This article discusses how to configure the WSO2 ESB to work with EJB services. With version 4.5 you will be able to invoke ejb 3.0 services host in a remote application server. In order to achieve this we have introduced two new implementations such as EJB and Bean Mediators.
The configuration Language of new mediators
Bean mediator - Bean mediator can manipulate a JavaBean that is bound to the Synapse message context as a property. This mediator can be used to create a new bean (CREATE action), remove an existing bean (REMOVE action), set a property of an existing JavaBean (SET_PROPERTY action) or to retrieve a property of an existing JavaBean (GET_PROPERTY action). Classes of objects manipulated by this mediator must follow the JavaBeans specification.
EJB Mediator - EJB mediator calls an external Enterprise JavaBean(EJB) and stores the result in the message payload or in a message context property. Currently, this mediator supports EJB3 Stateless Session Beans and Stateful Session Beans.
In the context of EJB, the implementation is focused on stateful and stateless session beans, so later when discussing the sample scenarios and will dig in to the details on how to configure those with the WSO2 EJB mediator.
So let's focus on configuring EJB service invocation with WSO2 ESB.
Probably the WSO2 ESB 4.5.0 onwards the EJB features are included with GUI features as well so you may find it easy to config them without any issues.
- Download Jboss 7(jboss-as-7.1.1.Final Latest) we did test EJB mediation scenarios with different application servers but for this demonstration we will be using Jboss 7, there you need to copy jboss-client.jar (is shipping with Jboss application server) to ESB_HOME/repository/components/lib.
- Use EJBDemo jar attached with this article (which as all the EJB demo services implemented to cover the demonstration) copy the EJBDemo.jar file to JBOSS_HOME/standalone/deployments/ and ESB_HOME/repository/components/lib.
- Next you may have to configure Jboss application server connection parameters, those configuration need to provide in synapse.properties (ESB_HOME/repository/conf ). Please use the following properties as it will be required to communicate with the application server,depending on the application server version those parameters might be varied.
- Start ESB and Jboss AS.
Synapse. properties configuration
In case if you use Jboss 5 you may have to use the connection properties as follows,
Configure EJB Mediator with stateless session bean
This demo will show how to get the closest location by passing latitude and longitude (x,y coordinates). In this scenario when the request comes, first we need to invoke the getClosestStore method which is exposed as EJB service. The return information then will be passed to invoke the location information.
To begin with, when request comes the x,y coordinates are assigned to the location instance using Bean mediator.Then will call EJB Mediator and the return result will be assign to the synapse property context , and subsequently calling getStoreById, and the returned store object (a complex object) is enriched as required by the source.
- Create instance of Location object loc using bean mediator assign latitude and longitude values to the variable of loc instance.
- Call getClosestStore passing location instance as argument parameter using EJB mediator and the return variable will be assgined to loc_id.
- Call getStoreById using EJB mediator pass the loc_id returns form the earlier EJB invocation assign the return value the synapse property context with “store”.
- Do the enrich process.
- Assign the store object which is returning from EJB services and return it back to the client.
Request and Response Sample
Stateless Session Beans
253, Kandy Road, Kadawatha
Configure EJB Mediator with stateful session bean
In the above diagram it simulates the shopping cart operation, when user sends multiple items in the cart, the incoming message enriched at and EJB mediator and the context is assigning with unique ejb session id for the particular request and keep that in the synapse the memory
EJB session ID will be used in later to invoke other total amount calculated for the cart items, so the process is then goes on by iterating the each items to the ejb service and populate the cart element, and later when iteration on completes it just invoke the getTotal by requesting the ejb session context generated earlier.
Request and Response Sample
Stateful Session Beans
Conceptually EJB is another way of achieving enterprise integration, but limited to java implementation,the idea of integrating EJB mediator gives advantage in SOA, since you will be able to call EJB services hosted from any other client protocols such as SOAP to EJB, REST to EJB, JMS to EJB, FIX to EJB etc ,thus this will definitely reduces the programmer effort to write codes to integrate their business functions implemented in EJB.
I believe the features that we are releasing with ESB 4.5.0 has quite a good ability to address most of the requirement though there are ares to be improved as well, so you can try this feature and hopefully would like to invite any thoughts of how this great feature can be improved further.
Senior software Engineer