Thursday, June 7, 2012

EJB Mediator WSO2 ESB 4.5.0



WSO2 ESB ( EJB Mediator)

This article will basically provide the attention on how to configure WSO2 ESB to work with EJB services, with ESB 4.5.0 you will be able to invoke ejb 3.0 services host in remote Application server, so In order to achieve this we have introduced 2 new implementations which is known as EJB and Bean Mediators

Applies To

WSO2 ESB 4.5.0

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 the implementation is focused on statefull and stateless session beans, so later when dicussing the sample scenarios and will dig in to the details on how to configure those with the WSO2 Ejb mediator
So let us now focus now 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
  • Please download Jboss 7(jboss-as-7.1.1.Final Latest)  we do have test this with different application servers but for this demonstration we will be using Jboss 7 there you need to  copy jboss-client.jar to ESB_HOME/repository/components/lib
  • Please find the EJBDemo.jar (Ejb service) you will be find the EJBDemo jar attached with this article (will discuss what this demo service and its implementation seperately) please copy the EJBDemo.jar file to  JBOSS_HOME/standalone/deployments/  and  ESB_HOME/repository/components/lib
  • As next step you may have to configure AS connection properties and the you will be able define them in  synapse.properties (ESB_HOME/repository/conf ) it has following properties which is required to communicate with the AS,depending on the application server which you may use the configuration parameteres might get change, the following is required when you need to connect EJB services hosted in Jboss AS 7
  • Start ESB and Jboss AS
Synapse. properties configuration
synapse.beanstalks=jack
synapse.beanstalks.jack.java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
synapse.beanstalks.jack.cache.warn.limit.stateless=5
synapse.beanstalks.jack.cache.warn.limit.stateful=5
synapse.beanstalks.jack.cache.timeout.stateless=1
synapse.beanstalks.jack.cache.timeout.stateful=1
In case if you use Jboss 5 you may have to use the connection properties as follows
#synapse.beanstalks=jack
#synapse.beanstalks.jack.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
#synapse.beanstalks.jack.java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
#synapse.beanstalks.jack.java.naming.provider.url=localhost:1099
#synapse.beanstalks.jack.cache.warn.limit.stateless=5
#synapse.beanstalks.jack.cache.warn.limit.stateful=5
#synapse.beanstalks.jack.cache.timeout.stateless=1
#synapse.beanstalks.jack.cache.timeout.stateful=1

Configure Ejb Mediator with stateless bean

Idea of this demo such as when requests closest location he has to pass latitude and longitude (x,y coordination), in this scenario when the requests comes first we need to invoke the getClosestStore method which is exposed as EJB service, and to invoke the method it requires Location information then by processing in coming x,y assigning them to the location variables using Bean mediator we will call Ejb Mediator and the return resutl will be assign to the synapse property context and using the return value call getStoreById there it will return the store object ( a complex object) and using enrich mediator we can transform the incoming request as requires by the source, let see how this can be configured in ESB level



      

            
                
            
        
        
            
                
            
        
        
            
                
                    
                        ?
                        
?
?
  • 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 the store object return information and pass them back to the back end
Request and Response Sample
Stateless Session Beans
=======================

  
    
        78
        8
    
  

http://localhost:8280/services/StoreLocatorProxy


  
    
      
        Kadawatha                       
        
253, Kandy Road, Kadawatha
0112990789

Configure EJB Mediator with State-full Session Bean

In the above diagram we will simulate the shopping cart operation, there user is sending multiple items which he has selected, when message reaches to the ESB engine EJB Mediator mediator context is assigning unique ejb session id for the particular request and keep that in the synapse the memory , ejb session id will be use 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

        
        
            
                
                
                    
                        
                            
                                
                                    
                                    
                                
                            
                            
                        
                    
                
            
        
    
 
        
            
                
            
            
                
                
                    
                        
                            $1
                        
                    
                    
                        
                    
                
                
                
Request and Response Sample
Stateful Session Beans
======================

  
    
        001
        
            
                2150
                1
            
            
                1189
                2
            
            
                890
                4
            
        
    
  



  
    
      807.0
    
  

Resources : https://www.dropbox.com/s/ba9adt0k21dsqx5/ejb-2015-10-06.zip?dl=0

2 comments:

  1. Hi Dushan, while implementing the same its not going to sequence. Please suggest.

    ReplyDelete
  2. Hi Dushan, while implementing the statefull session,its not going to sequence. Please suggest.
    I tried in the below way:

    Have put EJBDemo.jar in the path D:\apache-tomcat-7.0.39\webapps\examples\WEB-INF\lib.

    Have put Bootstrap.jar in the path D:\apache-tomcat-7.0.39\webapps\examples\WEB-INF\lib as using tomcat as AS.

    Have put EJBDemo.jar in the path D:\apache-tomcat-7.0.39\webapps\examples\WEB-INF\lib.

    Alo have edited the synapse.properties in the path D:\ESB\repository\conf by adding the follows:

    synapse.beanstalks=jack
    synapse.beanstalks.jack.java.naming.factory.url.pkgs=org.apache.naming
    synapse.beanstalks.jack.cache.warn.limit.stateless=5
    synapse.beanstalks.jack.cache.warn.limit.stateful=5
    synapse.beanstalks.jack.cache.timeout.stateless=1
    synapse.beanstalks.jack.cache.timeout.stateful=1

    And have created a ESB config project in which we have created the above mentioned proxy and sequence and deployed as a car.

    But its not working.

    It is getting deployed successfully but while putting l;og in sequence i am not able to get that in logs.

    Please suggest and if poosible try to explain the flow and the attribute significance in ejb mediator.

    Pleas help me on this.

    ReplyDelete