Saturday, July 10, 2010

Sending XML content as a value in name value pair GET request using WSO2 ESB

In this blog post I am describing on how to invoke a RESTful service which required a GET request with name value pairs in the request including XML content to be there in one of the value. First I will describe on how to invoke a normal RESTful service via ESB.

If you want to send a request to a service like this

http://service.endpoint/ServiceName?name1=value1&name2=value2...

First create an address endpoint by keeping the address of http://service.endpoint and make the endpoint type as REST/GET if you are using the UI to configure ESB. So the Endpoint configuration will looks like this.

<endpoint xmlns="http://ws.apache.org/ns/synapse">
<address uri="http://service.endpoint/" format="get" >
<suspendOnFailure>
<initialDuration>10000</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>30000</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
<retryDelay>0</retryDelay>
</markForSuspension>
</address>
</endpoint>


You can simply send the content of the request like this

<ServiceName>
<name1>value1</name1>
<name2>value2</name2>
</ServiceName>

No matter what type of request you are sending because when WSO2 ESB start processing the request it sees only a soap message after getting called message builders and message formatters. So you can see that during the endpoint creation I have remove the ServiceName part from the endpoint since we are sending that in the request and when we mark the endpoint format=get Axis2 will add that part in to the HTTP GET request and append the name value pairs to the request and send the message properly.

So this scenario is very simple and now let's see a case where you need to send something more complex in a HTTP GET request. I came across a scenario where we need to send an encoded xml in the GET request including some of the normal name value pairs.

So the HTTP GET request looks like this,
Ex:
http://service.endpoint/ServiceName?name1=value1&name2=value2&name3=<encoded xml content>

In this kind of a scenario you might get confused on how do you have to send the request. First create an endpoint as if the above configuration by making the format as get. Then,if you are in a position that you can change the incoming request you can simply send the request content like this.

<ServiceName>
<name1>value1</name1>
<name2>value2</name2>
<name3><![CDATA[xmlcontent]]></name3>
</ServiceName>

When ESB send the message to the REST endpoint Axis2 will encode the content inside the CDATA and send the message properly as I have showed above. If you are not capable of sending the message with CDATA content you can simply send the required elements in the incoming request and do an XSLT transformation to create the message as above.



Friday, July 9, 2010

Keep live connection in WSO2 products with Mysql Server

When you are configuring WSO2 products with mysql there might be errors coming with normal configuration when you try to access the product console after a reasonable amount of time. So this is not an issue with the product neither Mysql server. You need to specifically configure to keep the connection live for a defined idle time. Please follow up the below steps to increase the timeout and make sure product will try to connect to the database once it timeouts.

1. Open the my.conf file in mysql installed machine( it's it's a linux machine it will be in /etc/my.conf and if it's a windows machine it will be there in your mysql installed directory) and add the following entries

wait_timeout=259200
interactive_timeout=259200
(this is the keep hte idle time out value for three days in seconds and if you want to keep more just increase the value)

2. Open the CARBON_HOME/repository/conf/registry.xml and CARBON_HOME/repository/conf/user-mgt.xml change url of the mysql database configuration like this

jdbc:mysql://localhost:3306/regdb2?autoReconnect=true

appened "?autoReconnect=true" element to the url.


WSO2 ESB and Qpid setup Guide

This document explains how to do ESB + Qpid setup from the scratch which includes setting up ESB/QPID and registry instances and this Guide doesn't contains any internal message flow configurations of ESB. In this setup we are forwarding incoming messages to Qpid from ESB1 and ESB2 instance is picking messages from the Qpid instance and forward to another endpoint. For this scenario I am just explaining on how to setup the initial configuration and will be posting on how to configure message flows of ESB instances.

1. Download Qpid from here http://www.apache.org/dist/qpid/0.6/qpid-java-0.6.tar.gz and extract Qpid and and open the QPID_HOME/etc/config.xml
change enable ssl false as below.

<management>
<enabled>true</enabled>
<jmxport>8999</jmxport>
<ssl>
<enabled>false</enabled>
<!-- Update below path to your keystore location, eg ${conf}/qpid.keystore -->
<keyStorePath>${prefix}/../test-profiles/test_resources/ssl/keystore.jks</keyStorePath>
<keyStorePassword>password</keyStorePassword>
</ssl>
</management>

2. If you want to use qpid in a clustering mode and extract another qpid instance and do the step 1 and edit QPID_HOME/etc/config.xml and change the ports
configuration by editing port and sslport elements.

<port>5673</port>
<sslport>8673</sslport>

3. Start Qpid by running QPID_HOME/bin/qpid-server. If you are using a cluster start all the instances.


Configuring ESB instance which is forwarding incoming messages to Qpid instance

1. Create a server.properties file in CARBON_HOME/repository/conf/ directory and add the following entries to the properties file.

connectionfactory.qpidConnectionfactory=amqp://guest:guest@clientid/test?brokerlist='tcp://10.3.24.99:5672'
destination.directQueue=direct://amq.direct//ServiceName
destination.replyQueue =direct://amq.direct//ServiceNameReply

In this configuration 10.3.24.99 is the IP address of the machine which Qpid is running and qpidConnectionfactory is a reference we are using for our reference in axis2.xml.

If you are using Qpid in a cluster please create another properties file and define the same configuration with a different file name and let's assume your slave configuration file is server1.properties.

2. Now open the CARBON_HOME/repository/conf/axis2.xml add the following entry

Adding transportRecievers

<transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
<parameter name="default" locked="false">
<parameter name="java.naming.factory.initial" locked="false">org.apache.qpid.jndi.PropertiesFileInitialContextFactory</parameter>
<parameter name="java.naming.provider.url" locked="false">/home/wso2/WSO2_SETUP1/wso2esb-3.0.0-1/repository/conf/server.properties</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">qpidConnectionfactory</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
</parameter>
<parameter name="slave" locked="false">
<parameter name="java.naming.factory.initial" locked="false">org.apache.qpid.jndi.PropertiesFileInitialContextFactory</parameter>
<parameter name="java.naming.provider.url" locked="false">/home/wso2/WSO2_SETUP1/wso2esb-3.0.0-1/repository/conf/server1.properties</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">qpidConnectionfactory</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
</parameter>
</transportReceiver>

Please change the java.naming.privider.url element according to your setup(colored in red).

Adding transportSenders

<transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"/>

3. If you need only one Qpid instance please remove the second parameter list and keep the default configuration(parameter set with name="default") and note the name of the second parameter set is "slave" and
we need to use that configuration to be added in to synapse configuration when we configure failover endpoints.

4. Copy following jars in to CARBON_HOME/repository/components/lib directory and please download the merged jar from the given location and rest of the jars can be found in QPID_HOME/lib directory.
qpid-common-merged.jar http://people.apache.org/~lahiru/qpid-common-merged-0.6.jar
qpid-client-0.6.jar
slf4j-api-1.4.0.jar

5. Start the ESB instance by running CARBON_HOME/bin/wso2server.sh or .bat file, now it will connect to the Qpid instance running successfully.

Configuring ESB instance which picks incoming messages from Qpid and forward them to the actual service

Perform all the steps from step 1-5 in the ESB in main setup where we call the MemberVerification service directly. This ESB has to configure with Qpid to pick messages from the queues and send the request to the actual
service and once the response comes back put the reponse in to the reply queue defined in server.properties file.

But in the server.properties file you do not need to add a replyQueue in the configuration and and server.properties file looks like this.

connectionfactory.qpidConnectionfactory=amqp://guest:guest@clientid/test?brokerlist='tcp://10.3.24.99:5672'
destination.directQueue=direct://amq.direct//ServiceName

Assume 10.3.24.99 is the IP address of the machine where you run the Qpid instance.


I will be posting on how to configure two proxy services in ESB's to perform the above mentioned tasks in near future.