## Apache Synapse

## Sample 470: Introduction to the EJB Mediator I - Invoking Stateless Session Beans

<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="StoreLocatorProxy" transports="https http" startOnLoad="true" trace="disable"> <target> <!-- First call StoreLocator#getClosestStore(), then call StoreRegistry#getStoreById() with the result. --> <inSequence> <bean action="CREATE" class="samples.bean.Location" var="loc"/> <bean action="SET_PROPERTY" var="loc" property="latitude" value="{//m:latitude}" xmlns:m="http://services.samples"/> <bean action="SET_PROPERTY" var="loc" property="longitude" value="{//m:longitude}" xmlns:m="http://services.samples"/> <ejb class="samples.ejb.StoreLocator" beanstalk="demo" method="getClosestStore" target="store_id" jndiName="StoreLocatorBean/remote"> <args> <arg value="{get-property('loc')}"/> </args> </ejb> <ejb class="samples.ejb.StoreRegistry" beanstalk="demo" method="getStoreById" target="store" jndiName="StoreRegistryBean/remote"> <args> <arg value="{get-property('store_id')}"/> </args> </ejb> <!-- Prepare the response. --> <enrich> <source type="inline" clone="true"> <getClosestStoreResponse xmlns=""> <store> <name>?</name> <address>?</address> <phone>?</phone> </store> </getClosestStoreResponse> </source> <target type="body"/> </enrich> <bean action="GET_PROPERTY" var="store" property="name" target="{//store/name/text()}"/> <bean action="GET_PROPERTY" var="store" property="address" target="{//store/address/text()}"/> <bean action="GET_PROPERTY" var="store" property="phoneNo" target="{//store/phone/text()}"/> <!-- Send the response back to the client of the ESB. --> <respond/> </inSequence> </target> </proxy> </definitions>

### Objective

Demonstrate the usage of the EJB mediator for invoking EJB Stateless Session Beans hosted on a remote EJB Container.

### Pre-requisites

- Build the backend EJB jar to be hosted on the EJB Container by changing the directory to SYNAPSE_HOME/samples/axis2Server/src/EJBSampleBeans and invoking:
  mvn clean install
- Deploy the built EJB jar (SYNAPSE_HOME/samples/axis2Server/src/EJBSampleBeans/target/synapse-samples-ejb-1.0.0.jar) in an EJB Container such as JBoss or GlassFish.
- Add minimal client JARs of your EJB Container to SYNAPSE_HOME/lib. E.g. If you are using JBoss 7, it is sufficient to add the jboss-client.jar file.
- Add the synapse-samples-ejb-1.0.0.jar to SYNAPSE_HOME/lib. (Note: adding only the remote interfaces of the EJBs will suffice. Here we are using the complete jar file for simplicity.)
- Configure a beanstalk named demo in SYNAPSE_HOME/repository/conf/synapse.properties. You will need to specify the JNDI properties of your EJB Container in this configuration. Some example configurations are shown below.
  For JBoss 7:
  synapse.beanstalks=demo,foo # JNDI properties synapse.beanstalks.demo.java.naming.factory.url.pkgs=org.jboss.ejb.client.naming # Cache settings synapse.beanstalks.demo.cache.warn.limit.stateless=256 synapse.beanstalks.demo.cache.warn.limit.stateful=256 synapse.beanstalks.demo.cache.timeout.stateless=30 synapse.beanstalks.demo.cache.timeout.stateful=30
  For JBoss 6:
  synapse.beanstalks=demo,foo # JNDI properties synapse.beanstalks.demo.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory synapse.beanstalks.demo.java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces synapse.beanstalks.demo.java.naming.provider.url=localhost:1099 # Cache settings synapse.beanstalks.demo.cache.warn.limit.stateless=256 synapse.beanstalks.demo.cache.warn.limit.stateful=256 synapse.beanstalks.demo.cache.timeout.stateless=30 synapse.beanstalks.demo.cache.timeout.stateful=30
- If the JNDI names assigned to the EJBs by your EJB Container differ from the JNDI names specified in the sample 470 configuration file (repository/conf/sample/synapse_sample_470.xml), edit the jndiName attribute of all < ejb /> mediator invocations in the synapse_sample_470.xml accordingly.
- Start Synapse using the configuration numbered 470 (repository/conf/sample/synapse_sample_470.xml):
  Unix/Linux: sh synapse.sh -sample 470
  Windows: synapse.bat -sample 470

### Executing the Client

Send the following request to http://localhost:8280/services/StoreLocatorProxy using a tool such at TCPMon or curl.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <getClosestStore xmlns="http://services.samples"> <latitude>78</latitude> <longitude>8</longitude> </getClosestStore> </soapenv:Body> </soapenv:Envelope>

When the StoreLocatorProxy receives the request, it first creates an instance of samples.bean.Location using the Bean mediator. Then, it sets the properties of the newly created bean with the values extracted from the incoming SOAP message, again using the Bean mediator.

The subsequent EJB mediator invokes the getClosestStore() method on the remote stateless session bean, StoreLocator, with the previously populated Location object as an argument and stores the result in the store_id message context property. Another EJB mediator that follows calls the getStoreById() method on a second stateless session bean,StoreRegistry, to obtain store details encapsulated in a JavaBean (an instance of samples.bean.Store) and stores this resulting bean in a message context property named store. The demo beanstalk provides all necessary configurations needed for the two remote EJB invocations.

Finally, Enrich and Bean mediators are used to build the response message extracting properties from the JavaBean stored in the store message context property.

A sample response is shown below.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <getClosestStoreResponse> <store> <name>Kadawatha</name> <address>253, Kandy Road, Kadawatha</address> <phone>0112990789</phone> </store> </getClosestStoreResponse> </soapenv:Body> </soapenv:Envelope>

Back to Catalog