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

Sunday, July 17, 2016

Configuring XA transactions with mysql database in ESB



In this example scenario I'm showing you how to configure XA-Transaction with ESB using mysql database. First you need to login to the mysql database and create a database call "test". Inside that database execute the following command.

CREATE TABLE Persons
(
PersonID int,
Name varchar(255)
);

Use case:
  1. create new transaction
  2. insert into database
  3. Call a rest endpoint. 
  4. When response is received commit the  transaction.
When configuring XA transactions the best practice is to use datasource class name instead of using the driver class name. Please follow the below instructions to configure master-datasources.xml with datasource class name.
  • Open <ESB_HOME>/repository/conf/datasources/master-datasources.xml.
  • Add the below configuration under <datasources> tag. (You can change the datasource name to any name you like.)
<datasource>
    <name>MYSQLDS</name>
    <jndiConfig>
        <name>jdbc/MYSQLDS</name>
    </jndiConfig>
    <definition type="RDBMS">
        <configuration>
            <dataSourceClassName>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</dataSourceClassName>
            <defaultAutoCommit>false</defaultAutoCommit>
            <maxActive>150</maxActive>
            <InitialSize>100</InitialSize>
            <maxWait>60000</maxWait>
            <validationQuery>select 1</validationQuery>
            <validationInterval>30000</validationInterval>
            <testWhileIdle>true</testWhileIdle>
            <maxIdle>25</maxIdle>
            <minIdle>20</minIdle>
            <removeAbandonedTimeout>60</removeAbandonedTimeout>
            <removeAbandoned>true</removeAbandoned>
            <logAbandoned>true</logAbandoned>
            <testOnBorrow>true</testOnBorrow>
            <accessToUnderlyingConnectionAllowed>true</accessToUnderlyingConnectionAllowed>
            <dataSourceProps>
                <property name="serverName">localhost</property>
                <property name="portNumber">3306</property>
                <property name="databaseName">test</property>
                <property name="user">root</property>
                <property name="password">root</property>
            </dataSourceProps>
        </configuration>
    </definition>
</datasource>



  • Copy mysql driver library to <ESB_HOME>/repository/components/lib folder. 
  • Re-start the ESB server. 

Below is the ESB API configuration for this use case.

 <api context="/bpmn" name="xa_transaction_example">
        <resource faultSequence="faultSequenceREST" methods="POST" uri-template="/test">
            <inSequence>
                <log level="custom">
                    <property name="text" value="** In Sequencce started**"/>
                </log>
                <transaction action="new"/>
                <dbreport useTransaction="true">
                    <connection>
                        <pool>
                            <dsName>jdbc/MYSQLDS</dsName>
                        </pool>
                    </connection>
                    <statement>
                        <sql><![CDATA[INSERT INTO Persons (PersonID,Name) VALUES ('1234','Tom')]]></sql>
                    </statement>
                </dbreport>
                <call blocking="true">
                    <endpoint>
                        <address uri="http://www.mocky.io/v2/577f4bc2100000152df26247"/>
                    </endpoint>
                </call>
                <log level="full"/>
                <transaction action="commit"/>
                <log level="custom">
                    <property name="text" value="**Transaction commited**"/>
                </log>
                <respond/>
            </inSequence>
        </resource>
    </api>





Get Redirection URL from ESB

In this article I will show you how to get redirection URL from ESB. Assume that you call a back-end service from ESB proxy service and that back-end service re-direct to another service. In that case you will get a response from the server something like below.

HTTP/1.1 308 Redirection (308)[\r][\n]"
Server: Cowboy[\r][\n]
Connection: close[\r][\n]
Location: http://www.mocky.io/v2/57889ef20f0000701dbd16e3DDDD[\r][\n]
Content-Type: application/xml; charset=utf-8[\r][\n]
Date: Mon, 18 Jul 2016 05:40:21 GMT[\r][\n]
Via: 1.1 vegur[\r][\n]
[\r][\n]
<hi></hi>

If you want to get the re-direction url which is with the "Location" header,  you can use the the property mediator as below. 

 <property xmlns:ns="http://org.apache.synapse/xsd"
                   name="Location"
                   expression="$axis2:PRE_LOCATION_HEADER"/>

Wednesday, June 24, 2015

Enable Mutual SSL with WSO2 ESB and IBM MQ



This article will explain how to connect to an SSL enabled channel in MQ using WSO2 ESB. Here I’m using ESB version 4.8.1 and MQ 8.0.0.2 versions. Java version java 1.7.0-60 or above.


  • First we have to import the MQ certificate to WSO2 ESB’s client trust store. You can import the certificate using the following command in keytool.
    • ./keytool -import -file PATH_TO_MQ_CERTIFICATE/USU08QM2.cer -alias ALIAS_NAME_OF_MQ_CERTIFICATE -keystore PATH_TO_CLIENT_TRUST_STORE/client-truststore.jks -storepass password


  • Then you have to export the certificate from wso2carbon.jks and import that to MQ keystore in MQ server. To export the certificate from wso2carbon.jks use the following command in keytool.

    •  ./keytool -export -keystore PATH_TO_KEY_STORE/wso2carbon.jks -storepass KEY_SOTRE_PASSWORD -alias ALIAS_NAME_OF_WSO2_KEY_STORE -file PATH_TO_STORE_THE_CERTIFICATE/wso2esb.cer
  •  Then this certificate should be should be imported to MQ JKS using the command listed in step 1.

  • Then you have to specify the matching cipher suite for ESB. In IBM MQ when SSL is enabled, they specify a cipher spec to the channel. You have to find the matching cipher suite for that cipher spec and specify it in ESB as an environment variable. Cipher spec to cipher suite mapping can be found in here. Environment variable can be set in ESB by setting the following propery in wso2server.sh file.
    •  -DCMQC.SSL_CIPHER_SUITE_PROPERTY="SSL_RSA_WITH_3DES_EDE_CBC_SHA" \
  • Note:Here I have specified the cipher suite as SSL_RSA_WITH_3DES_EDE_CBC_SHA. The matching cipher spec for this cipher suite is TLS_RSA_WITH_3DES_EDE_CBC_SHA.
  • Next you have to copy MQ jta.jar to ESB_HOME/repository/components/lib folder. (Remove the old jar files if there are any existing ones). Make sure you copy MQ 8.0.0.2 jar files. The client jars in [1] is not working with SSL. Therefore you have to create a new OSGI bundle with new MQ 8.0.0.2 client jars. To do that you can clone the wmq-client-8.0.0.2 project from here and copy following jar files to lib folder in wmq-client-8.0.0.2/lib. Then build the project.
    • com.ibm.mq.allclient.jar
    • fscontext.jar
    • jms.jar
    • providerutil.jar
  • Note: Copy the wmq-client-8.0.0.2.jar file in target directory to ESB_HOME/repository/components/dropins folder. Remove if there are any old jar files.
  • Remove following line from <ESB_4.8.1_Home>\repository\conf\etc\launch.ini
    • javax.jms,\
  • Regenerate .bindings file with following property.
    • Provider Version : 8


Then restart the server and then you are ready to go with SSL. !!!


[1] http://nandikajayawardana.blogspot.com/2015/03/configuring-ibm-mq-with-wso2-esb.html