Wednesday, April 12, 2017

IBM MQ from Spring using JMS

2 Ways to Connect to IBM MQ from Spring using JMS

Java Messaging Service (JMS) provides a standard way for Java applications to communicate with messaging systems like IBM MQ. In Spring applications, there are two primary approaches to connect to IBM MQ using JMS.

1. Using com.ibm.mq.jms.context.WMQInitialContextFactory

This method relies on JNDI-based lookups to retrieve connection factories and destinations. It is typically used in environments where resources such as ConnectionFactory and Queue are preconfigured on the application server (e.g., WebSphere).

This approach is suitable when you want to externalize configuration and rely on a container-managed environment.

java.naming.factory.initial = com.ibm.mq.jms.context.WMQInitialContextFactory
java.naming.provider.url = tcp://hostname:1414

2. Using com.ibm.mq.jms.MQQueueConnectionFactory

This is a programmatic approach that integrates seamlessly with Spring’s configuration. You can define the MQ connection directly in your Spring configuration files. Here's an example:

<!-- JMS Connection Factory -->
<bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
    <property name="queueManager" value="QM1"/>
    <property name="hostName" value="localhost"/>
    <property name="port" value="1414"/>
    <property name="channel" value="DEV.APP.SVRCONN"/>
    <property name="transportType" value="1"/>
</bean>

<!-- JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
</bean>

Using XA (Transactional) Connections

For distributed transactions that span across MQ and databases, configure XA support with Spring and IBM MQ as shown below:

<!-- Transaction Proxy Template -->
<bean id="txProxyTemplate" abstract="true"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
        <props>
            <prop key="archiveTrades">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="processUpload">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="sendRTM">PROPAGATION_REQUIRED,-Exception</prop>
        </props>
    </property>
</bean>

<!-- JOTM Transaction Manager -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>

<bean id="transactionManager"
      class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction" ref="jotm"/>
</bean>

<!-- XA DataSource -->
<bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource"
      destroy-method="shutdown">
    <property name="transactionManager" ref="jotm"/>
    <property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:oci:@DB_NAME"/>
    <property name="user" value="username"/>
    <property name="password" value="password"/>
</bean>

<!-- Pooled XA DataSource -->
<bean id="connectionDS" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
      destroy-method="shutdown">
    <property name="dataSource" ref="innerDataSource"/>
    <property name="user" value="username"/>
    <property name="password" value="password"/>
    <property name="maxSize" value="10"/>
</bean>

<!-- XA-aware MQ Connection Factory -->
<bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQXAQueueConnectionFactory">
    <property name="queueManager" value="QM_hostname"/>
    <property name="hostName" value="hostname"/>
    <property name="port" value="1414"/>
    <property name="transportType" value="1"/>
</bean>

<!-- MQ Queue Destination -->
<bean id="destination" class="com.ibm.mq.jms.MQQueue">
    <property name="targetClient" value="1"/>
</bean>

<!-- JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
</bean>

Summary

Method Use Case Benefits
WMQInitialContextFactory Managed server environments (e.g., WebSphere) Centralized config via JNDI
MQQueueConnectionFactory Standalone Spring Boot or XML apps Direct, programmatic setup
MQXAQueueConnectionFactory Transactional messaging (with XA) Full integration with JTA and DB transactions

With either method, Spring’s abstraction via JmsTemplate makes working with IBM MQ and JMS much easier. Choose the right approach based on your deployment model and whether or not you need distributed transactions.

Friday, January 13, 2017

SSL certification of you web site

This will create a certification file and send to free certification authority "https://certbot.eff.org" sponsored by linux foundation.

Following are for apache deployment on Ubuntu compatible.
For other configurations visit https://certbot.eff.org

sudo apt-get install python-letsencrypt-apache
sudo letsencrypt --apache

Tuesday, June 10, 2014

Code coverity using GCC gcov



Create a cpp source file with main function and name it main.cpp

Build source with gcov enabled
g++ -Wall -fprofile-arcs -ftest-coverage main.cpp
                It will generate main.gcno file
Run binary
                ./a.out
                It will generate main.gcda file
Run gcov for the source file
                gcov main.cpp
                it will generate main.cpp.gcov file
Open main.cpp.gcov and analyse ##### lines for un tested code lines.