Showing posts with label Mediator. Show all posts
Showing posts with label Mediator. Show all posts

Getting Started with Simple wso2 ESB EJB Mediator Sample


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

Before Start first look what is EJB Mediator and what is EJB now Let's see how to Run a Simple Hello Name using EJB Mediator. in order to run the the sample we need to configure our Environment So the Requirements are
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.

Configure the ESB

  • 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 HelloName.jar (Ejb service) you will be find the HelloName  jar attached with this article (will discuss what this demo service and its implementation separately) please copy the HelloName.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

Configure Ejb Mediator for stateless bean

Idea of this demo such as when requests closest location he has to pass the name and  in this scenario when the requests comes first we need to invoke the sayHello method which is exposed as EJB service, and to invoke the method it requires to check HelloWorld so it we picking the name and pass it to ejb Container and the return result will be assign to the synapse property context  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

ejb Proxy

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ejb"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="response" value="init" scope="default" type="STRING"/>
<log level="custom">
<property name="response" value="{//name}"/>
</log>
<ejb beanstalk="jack"
class="org.NewBeanRemote"
method="sayHello"
target="response"
jndiName="ejb:/EJBModule3/NewBean!org.NewBeanRemote">
<args>
<arg xmlns:m="http://org" value="{//m:name}"/>
</args>
</ejb>
<payloadFactory media-type="xml">
<format>
<ejb xmlns="">
<response>$ctx.response</response>
</ejb>
</format>
<args/>
</payloadFactory>
<respond/>
</inSequence>
</target>
<description/>
</proxy>

in above proxy we need to set beanstalk , class, method, target and  jndiName we already looked about these parameters in previous post about EJB Mediator. here my beanstalk id is jack bu we can set unique id for that the important thing is we are going to use this name in synapse properties to set the property values. my class name is org.NewBeanRemote and method name is sayHello . the value which my method returning is naming as response in target finally we can catch the jndi name from JBoss server console.

synapse property

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
after  set the synapse properties value we will sent the below request to the proxy which we create as ejb.

Soap Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<sayHello xmlns="http://org">
<name>rajjaz</name>
</sayHello>
</soapenv:Body>
</soapenv:Envelope> 

 if everything success you will get bellow response to your soap Request

Soap Response

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ejb>
<response>Hello rajjaz</response>
</ejb>
</soapenv:Body>
</soapenv:Envelope>

Debug Log


[2015-10-09 10:39:09,699] DEBUG - wire >> "POST /services/ejb.ejbHttpSoap11Endpoint HTTP/1.1[\r][\n]"
[2015-10-09 10:39:09,700] DEBUG - wire >> "Accept-Encoding: gzip,deflate[\r][\n]"
[2015-10-09 10:39:09,700] DEBUG - wire >> "Content-Type: text/xml;charset=UTF-8[\r][\n]"
[2015-10-09 10:39:09,700] DEBUG - wire >> "SOAPAction: "urn:mediate"[\r][\n]"
[2015-10-09 10:39:09,701] DEBUG - wire >> "Content-Length: 201[\r][\n]"
[2015-10-09 10:39:09,701] DEBUG - wire >> "Host: rajjaz-ThinkPad-T540p:8280[\r][\n]"
[2015-10-09 10:39:09,701] DEBUG - wire >> "Connection: Keep-Alive[\r][\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> "User-Agent: Apache-HttpClient/4.1.1 (java 1.5)[\r][\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> "[\r][\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">[\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> " <soapenv:Body>[\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> " <sayHello xmlns="http://org">[\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> "<name>rajjaz</name>[\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> " </sayHello>[\n]"
[2015-10-09 10:39:09,702] DEBUG - wire >> " </soapenv:Body>[\n]"
[2015-10-09 10:39:09,703] DEBUG - wire >> "</soapenv:Envelope>"
[2015-10-09 10:39:09,707] DEBUG - ProxyServiceMessageReceiver Proxy Service ejb received a new message from : 127.0.0.1
[2015-10-09 10:39:09,708] DEBUG - ProxyServiceMessageReceiver Message To: /services/ejb.ejbHttpSoap11Endpoint
[2015-10-09 10:39:09,708] DEBUG - ProxyServiceMessageReceiver SOAPAction: urn:mediate
[2015-10-09 10:39:09,708] DEBUG - ProxyServiceMessageReceiver WSA-Action: urn:mediate
[2015-10-09 10:39:09,708] DEBUG - ProxyService Setting default fault-sequence for proxy
[2015-10-09 10:39:09,708] DEBUG - ProxyServiceMessageReceiver Using the anonymous in-sequence of the proxy service for mediation
[2015-10-09 10:39:09,708] DEBUG - SequenceMediator Start : Sequence <anonymous>
[2015-10-09 10:39:09,708] DEBUG - SequenceMediator Sequence <SequenceMediator> :: mediate()
[2015-10-09 10:39:09,708] DEBUG - SequenceMediator Mediation started from mediator position : 0
[2015-10-09 10:39:09,708] DEBUG - SequenceMediator Building message. Sequence <SequenceMediator> is content aware
[2015-10-09 10:39:09,709] DEBUG - PropertyMediator Start : Property mediator
[2015-10-09 10:39:09,709] DEBUG - PropertyMediator Setting property : response at scope : default to : init (i.e. constant : init)
[2015-10-09 10:39:09,709] DEBUG - PropertyMediator End : Property mediator
[2015-10-09 10:39:09,709] DEBUG - LogMediator Start : Log mediator
[2015-10-09 10:39:09,709] INFO - LogMediator response = {//name}
[2015-10-09 10:39:09,710] DEBUG - LogMediator End : Log mediator
[2015-10-09 10:39:09,933] DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>> <pfPadding><ejb><response>Hello rajjaz</response></ejb></pfPadding>
[2015-10-09 10:39:09,933] DEBUG - RespondMediator Start : Respond Mediator
[2015-10-09 10:39:09,940] DEBUG - RespondMediator End : Respond Mediator
[2015-10-09 10:39:09,940] DEBUG - SequenceMediator End : Sequence <anonymous>
[2015-10-09 10:39:09,941] DEBUG - wire << "HTTP/1.1 200 OK[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "Host: rajjaz-ThinkPad-T540p:8280[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "SOAPAction: "urn:mediate"[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "Accept-Encoding: gzip,deflate[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "Content-Type: text/xml;charset=UTF-8; charset=UTF-8[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "Date: Fri, 09 Oct 2015 05:09:09 GMT[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "ce[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ejb><response>Hello rajjaz</response></ejb></soapenv:Body></soapenv:Envelope>[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "0[\r][\n]"
[2015-10-09 10:39:09,941] DEBUG - wire << "[\r][\n]"

References

WSO2 ESB EJB Mediator

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

 

 

 

 Syntax


<ejb beanstalk="string" class="string" [sessionId="string"] [remove="true | false"] 
[method="string"] [target="string | {xpath}"] [jndiName="string"] />
<args>
<arg (value="string | {xpath}")/>*
</args>
</ejb>

This is how EJB Mediator will be in proxy

Parameter Name
Description
Add ArgumentCan be used to define the arguments which is required for the particular ejb method to be invoked Expression/Value. 
Beanstalk IDReference to the application server specific connection source information, which is defined at the synapse.properties.
ClassThis required the remote interface definition provided in the EJB 3.0 (EJB service invocation remote/home interface).
JNDI NameThe Java Naming and Directory Interface (JNDI) is an application programming interface (API) for accessing different kinds of naming and directory services. JNDI is not specific to a particular naming or directory service. It can be used to access many different kinds of systems including file systems; distributed objects systems such as CORBA, Java RMI, and EJB; and directory services such as LDAP, Novell NetWare, and NIS+.
RemoveThis parameter specifies whether the Enterprise Entity Manager should remove the EJB context related parameters once the state full/stateless session is invoked.
Session IDWhen the EJB context is invoked in the form state-full bean then the related ejb session status specified will be stored in here. Possible values are as follows.
  • Value: If this is selected, the session ID can be entered as a static value.
  • Expression: If this is selected, an XPath expression can be entered to evaluate the session ID.
TargetIf a particular EJB method returns, then the return object can be saved against the the name provided in the target at the synapse property context.
 

Sample ejb Syntax

    <ejb beanstalk="rajjaz"
class="org.NewBeanRemote"
method="sayHello"
target="response"
jndiName="ejb:/EJBModule3/NewBean!org.NewBeanRemote">
<args>
<arg xmlns:m="http://org" value="{//m:name}"/>
</args>
</ejb>

above one is the sample catch the value come as name and pass it to the method sayHello in ejb class JNDI Named as NewBeanRemote then it will catch the output come within response tag.

the values for the JNDI Properties will be set the Synapse.properties

Synapse.properties


synapse.beanstalks=rajjaz
synapse.beanstalks.rajjaz.java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
synapse.beanstalks.rajjaz.cache.warn.limit.stateless=5
synapse.beanstalks.rajjaz.cache.warn.limit.stateful=5
synapse.beanstalks.rajjaz.cache.timeout.stateless=1
synapse.beanstalks.rajjaz.cache.timeout.stateful=1 

lets discuss how to run helloworld sample using WSO2 ESB ejb mediator in next post

Source code

References

WSO2 ESB Mediator

Mediators provide an easy way of extending the ESB functionalities. WSO2 ESB is based on the WSO2 Carbon platform, which uses OSGI as the underlying technology. It implies everything that runs inside the WSO2 ESB to be OSGI bundles.



There are two ways of writing the ESB mediator:
When adding a mediator to a sequence, you can configure the mediator in design view or in source view, which allows you to edit the source XML (different mediators have their own XML configurations).

The message content is accessed by some mediators in some mediation scenarios while it is not accessed in the other scenarios. Mediators can be classified as follows based on this aspect.
  • Content-aware mediators: These mediators always access the message content when mediating messages (e.g., Enrich mediator).
  • Content-unaware mediators: These mediators never access the message content when mediating messages (e.g., Send mediator).
  • Conditionally content-aware mediators: These mediators could be either content-aware or content-unaware depending on their exact instance configuration. For an example a simple Log Mediator instance (i.e. configured as <log/>) is content-unaware. However a log mediator configured as <log level=”full”/> would be content-aware since it is expected to log the message payload.

    Mediators are considered to be one of the main mechanisms for extending an ESB. You can write a custom mediator and add it to the ESB. This custom mediator and any other built-in mediator will be exactly the same as the API and the privileges.
    The standard mediators in WSO2 ESB are listed in the table below. Click a link for details on that mediator. There are also many samples that demonstrate how to use mediators.

    The Mediator Catalog

    Category
    Name
    Description
    CoreCallInvoke a service in non blocking synchronous manner
    EnqueueUses a priority executor to ensure high-priority messages are not dropped
    SendSends a message
    LoopbackMoves the message from the In flow to the Out flow, skipping all remaining configuration in the In flow
    SequenceInserts a reference to a sequence
    RespondStops processing on the message and sends it back to the client
    EventSends event notifications to an event source, publishes messages to predefined topics
    DropDrops a message
    Call TemplateConstructs a sequence by passing values into a sequence template
    EnrichEnriches a message
    PropertySets or remove properties associated with the message
    LogLogs a message
    FilterFilterFilters a message using XPath, if-else kind of logic
    OutApplies to messages that are in the Out path of the ESB
    InApplies to messages that are in the In path of the ESB
    ValidateValidates XML messages against a specified schema.
    SwitchFilters messages using XPath, switch logic
    RouterRoutes messages based on XPath filtering
    Conditional RouterImplements complex routing rules (Header based routing, content based routing and other rules)
    TransformXSLTPerforms XSLT transformations on the XML payload
    FastXSLTPerforms XSLT transformations on the message stream
    URLRewriteModifies and rewrites URLs or URL fragments
    XQueryPerforms XQuery transformation
    HeaderSets or removes SOAP headers
    Fault (also called Makefault)Create SOAP Faults
    PayloadFactoryTransforms or replaces message content in between the client and the backend server
    AdvancedCacheEvaluates messages based on whether the same message came to the ESB
    ForEachSplits a message into a number of different messages by finding matching elements in an XPath expression of the original message.
    CloneClones a message
    StoreStores messages in a predefined message store
    IterateSplits a message
    AggregateCombines a message
    CalloutBlocks web services calls
    TransactionExecutes a set of mediators transactionally
    ThrottleLimits the number of messages
    DBReportWrites data to database
    DBLookupRetrieves information from database
    EJBCalls an external Enterprise JavaBean(EJB) and stores the result in the message payload or in a message context property.
    RuleExecutes rules
    EntitlementEvaluates user actions against a XACML policy
    OAuth2-legged OAuth support
    SmooksUsed to apply lightweight transformations on messages in an efficient manner.
    ExtensionBeanManipulates JavaBeans
    ClassCreates and executes a custom mediator
    POJOCommandExecutes a custom command
    ScriptExecutes a mediator written in Scripting language
    SpringCreates a mediator managed by Spring
    AgentBAMCaptures data events and sends them to the BAM server


    References