Showing posts with label WSO2 ESB. Show all posts
Showing posts with label WSO2 ESB. Show all posts

Saturday, April 23, 2016

In and Out of JMS queue using WSO2 ESB

In this example we'll take a look at inserting a message to a JMS queue via a proxy service in WSO2 ESB, and how to pull it from the queue using an Inbound Endpoint


Part 1: Get Message From Queue

For the first part we will be using Sample 901 which ships with ESB 4.9.0.

Install ActiveMQ and follow steps in the following sample 901:
https://docs.wso2.com/display/ESB490/Sample+901%3A+Inbound+Endpoint+JMS+Protocol+Sample


For the first part you need to start the ESB with Sample configuration 901.
in <ESB_HOME>/bin
wso2esb-samples.bat -sn 901 for Windows

./wso2esb-samples.sh -sn 901 in Linux


Deploy SimpleStockQuoteService:

Run ant from <ESB_HOME>/samples/axis2Server/src/SimpleStockQuoteService


Then start the Axis2 Server at <ESB_HOME>/samples/axis2Server and send a message to the queue as shown in the sample.


Output - you will see a stock quote for IBM generated  in the Axis2 Server console.


Part 2: Put message to queue using proxy service

Create a pass through proxy and modify the source configuration with the following:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="StockQuoteProxyToJMSQueue"
       transports="http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="OUT_ONLY" value="true"/>
         <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
         <send>
            <endpoint>
               <address uri="jms:/ordersQueue?transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;java.naming.provider.url=tcp://127.0.0.1:61616&amp;transport.jms.DestinationType=queue"/>
            </endpoint>
         </send>
      </inSequence>
   </target>
   <description/>
</proxy>


The above is a pretty simple configuration that will forward the message sent to the proxy to the JMS queue.

You can get the WSDL file for the proxy service and invoke the proxy via SoapUI with the same payload shown in Sample 901. The end-to-end scenario will be when you send a message to the proxy the proxy will forward it to the JMS queue, and the inbound endpoint will pick it from the queue and send it to the SimpleStockQuoteService

--

Friday, September 5, 2014

Protocol Conversion from REST to SOAP using Messaging Bridge Enterprise Integration Pattern (EIP) and Content Type Negotiation using WSO2 ESB

Say you have a SOAP service. You need to expose it as a REST service. Furthermore, you may need other mediations such as XML to JSON conversion to happen.

This could be done by implementing the Messaging Bridge Enterprise Integration Pattern (EIP).
Ref: https://docs.wso2.com/display/IntegrationPatterns/Messaging+Bridge

Furthermore, with the WSO2 ESB Content type negotiations (conversions) such as XML to JSON is just a simple configuration that would take only a few seconds.

For the back-end service we'll use the ZooDataService we built in a previous example using WSO2 DSS to expose the data as SOAP services: Expose Data as a Service using WSO2 Data Services Server (DSS)

Now that we've got a service we'll see how to do the SOAP to REST conversion, and the XML to JSON conversion.

1. First lets take a look at the SOAP to REST Conversion:

For the SOAP to REST conversion we will adhere to the Messaging Bridge Integration pattern as mentioned above.

We will create an API in the WSO2 ESB for this purpose and expose the SOAP service as a REST service via the said API.

Below is the architecture for our system:




Now lets look at how to Create the ESB API:

Go to the ESB management console and add an API

Switch to Source View and enter the following configuration:

<api xmlns="http://ws.apache.org/ns/synapse" name="ZooData" context="/zoodata">
   <resource methods="GET" uri-template="/{animalID}">
      <inSequence>
         <payloadFactory media-type="xml">
            <format>
               <p:getAnimalByID xmlns:p="http://ws.wso2.org/dataservice">
                  <a:AnimalID xmlns:a="http://ws.wso2.org/dataservice">$1</a:AnimalID>
               </p:getAnimalByID>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('uri.var.animalID')"></arg>
            </args>
         </payloadFactory>
         <log level="full"></log>
         <call>
            <endpoint>
               <address uri="http://10.100.5.132:9769/services/ZooDataService/select_with_key_animal_operation" format="soap12"></address>
            </endpoint>
         </call>
         <property name="messageType" value="application/json" scope="axis2"></property>
         <respond></respond>
      </inSequence>
   </resource>
</api>
                        
                        

Now you can invoke the service Restully using the following GET request:

http://localhost:8281/zoodata/003

you will get the following XML output:

<animalCollection>
<animal>
<AnimalID>003</AnimalID>
<Species>Penguin</Species>
<Name>Rico</Name>
<Country>Madagaskar</Country>
<Speciality>Demolitions</Speciality>
</animal>
</animalCollection>


2. Next we'll look at how to convert the content types (Content Type Negotiation):

To convert the content type from XML to JSON  in the outgoing response simple change the value attribute of the 'messageType' property to 'application/json'. This works vice versa as well.

<property name="messageType" value="application/json" scope="axis2"></property>

now you'll get the output in JSON format:

{"animalCollection":{"animal":{"AnimalID":"003","Species":"Penguin","Name":"Rico","Country":"Madagaskar","Speciality":"Demolitions"}}}




Furthermore:
Now since you have achieved protocol & content type conversion you can use the WSO2 API Manager to expose this as a Managed API if you wish. You will be using the API Façade pattern in doing so.


For further information on API Façade pattern refer:
http://wso2.com/blogs/architecture/2013/05/a-pragmatic-approach-to-the-api-faade-pattern/
 


Monday, June 9, 2014

Scheduled Tasks with WSO2 ESB

This article demonstrates how to create a simple scheduled task using the WSO2 ESB.

This is an example that you can complete withing 5 minutes and will give you a basic understanding of how easy it is to deploy a scheduled task using the WSO2 ESB.

Steps:
1. Create Task class
2. Export task class as a jar
3. Add to WSO2 ESB classpath
4. Create Scheduled task

Step 1:
First of all you need to create a Task Class. For this example we will create a simple class that prints out "HELLO" and a timestamp.

First download the synapse-core-1.2.jar from either the Synapse site or from the Maven Repository

Create a Java Project in eclipse (or any IDE you preffer) and create the following class:

package wso2.demo.taskt;

import java.io.BufferedWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import org.apache.synapse.ManagedLifecycle;

import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.startup.Task;

public class SayHello implements Task, ManagedLifecycle {


private SynapseEnvironment synapseEnvironment;

@Override

public void destroy() {
// TODO Auto-generated method stub

}


@Override

public void init(SynapseEnvironment arg0) {
this.synapseEnvironment = synapseEnvironment;
}

@Override

public void execute() {

try {
File file = new File("E:\\sayhello.txt");
 
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

BufferedWriter bw = new BufferedWriter(fw);
bw.write("---------------- HELLO ----------------- "+ (new Date()).toString() + "\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}



Step 2:
Export the above class as a Jar file. Say with the name SayHello.jar

Step 3:
Add the above class to the ESB Class path by placing it in the <ESB_HOME>/repository/components/lib directory.


Step 4:

Now you can create the Scheduled task in your ESB.

1. Login to the ESB

2. Select "Scheduled Tasks" under the "Main" tab

























3. Add a new task



Replace the Task Implementation field with the qualified name of the class you created.

In the next section the Trigger type would be what type of scheduled task you want it to be. By simply defining the count and interval, or by providing a cron expression. Lets select "simple" for this example.

Count - defines how many times your job needs to run
Interval - specifies time gap between task executions

Click Schedule to save the task.

Aaaand That's it ...

Your output file would show the following:

---------------- HELLO ----------------- Mon Jun 09 20:11:37 IST 2014
---------------- HELLO ----------------- Mon Jun 09 20:11:42 IST 2014
---------------- HELLO ----------------- Mon Jun 09 20:12:46 IST 2014
---------------- HELLO ----------------- Mon Jun 09 20:12:51 IST 2014
---------------- HELLO ----------------- Mon Jun 09 20:12:56 IST 2014
---------------- HELLO ----------------- Mon Jun 09 20:13:01 IST 2014


For more options in creating scheduled tasks visit the following page:
Adding and Scheduling tasks in WSO2 ESB