Tuesday, June 14, 2011

JBoss MDB for Websphere MQ

Download and install websphere MQ. I downloaded latest available version 7.0. It has many rpm files to be installed. I am using ubuntu 11. So I used bellow command to install a one rpm.

rpm -ivh --nodeps --force-debian file_name.rpm

This installation didn’t have some requried files so I downloadd 7.0.1-WS-MQ-LinuxX64-FP0005.tar.gz patch and installed as previous installation.

Ok if you have installed it correctly now you can run MQ Explorer. That is a Nice GUI based on eclipse. You can create queues and put messages to that queue via this GUI. This UI has many features but it is out of topic.

Normally MQ installation will be in /opt/mqm. Switch to root and switch to mqm and goto /opt/mqm/bin and run ./strmqcfg. That will start MQ Explorer. If it does not start GUI without any message. you many need to give permission to mqm user to use your X resources. for that open a new terminal and enter ‘xhost +’ command. You can check whether you can run gui by running xclock application.

Ok After you starting MQ Explorer it will give a nice documentation about how to create a queue manager and queue. Follow Sending a message to a local queue to create your first queue. It says about queue manager QM_APPLE. follow that as it is because I am using default one to configure JBoss resource adapter.

Now you need to install MQ resource adapter in JBoss. copy /opt/mqm/java/lib/jca/wmq.jmsra.rar to $JBOSS_HOME/server/default/deploy. In addition to resource adapter you need to copy MQ client library to $JBOSS_HOME/server/default/lib. that file is availble in /opt/mqm/java/lib/com.ibm.mqetclient.jar.

Now create a file in $JBOSS_HOME/server/default/deploy/messaging/wmq.jmsra-ds.xml. Configure it as below.


<?xml version="1.0" encoding="UTF-8"?>

<connection-factories>

<!-- mbeans defining JCA administered objects -->
<mbean code="org.jboss.resource.deployment.AdminObject"
name="jca.wmq:name=ivtqueue">

<!-- Bind this AdminObject with the JNDI name myAppleQueue -->
<attribute name="JNDIName">
myAppleQueue
</attribute>

<!-- this MBean depends on the WebSphere MQ resource adapter -->
<depends optional-attribute-name="RARName">
jboss.jca:service=RARDeployment,name='wmq.jmsra.rar'
</depends>

<!-- this admin object is a javax.jms.Queue -->
<attribute name="Type">javax.jms.Queue</attribute>

<!--
Configuration for Queue Q1 on queue manager QM_APPLE.
-->
<attribute name="Properties">
baseQueueManagerName=QM_APPLE
baseQueueName=Q1
</attribute>
</mbean>


<!-- JCA Connection factory definitions -->
<tx-connection-factory>

<!-- Bind this ConnectionFactory with the JNDI name MyAppleCF -->
<jndi-name>MyAppleCF</jndi-name>

<!-- Indicate that the connection factory supports XA transactions -->
<xa-transaction />

<!-- rar-name is the actual RAR file name, in this case wmq.jmsra.rar -->
<rar-name>wmq.jmsra.rar</rar-name>

<!-- connection-definition is the ConnectionFactory interface
defined in the ra.xml -->
<connection-definition>
javax.jms.ConnectionFactory
</connection-definition>

<!--
Configuration for the ConnectionFactory. This defines the channel, hostname, port,
queueManager, and transportType properties for a client (TCP/IP) connection to WMQ
-->
<config-property name="channel" type="java.lang.String">
SYSTEM.DEF.SVRCONN
</config-property>
<config-property name="hostName" type="java.lang.String">
127.0.0.1
</config-property>
<config-property name="port" type="java.lang.String">
1414
</config-property>
<config-property name="queueManager" type="java.lang.String">
QM_APPLE
</config-property>
<config-property name="transportType" type="java.lang.String">
CLIENT
</config-property>

<!-- define security domain -->
<security-domain-and-application>JmsXARealm</security-domain-and-application>
</tx-connection-factory>

</connection-factories>


OK configurations done. Now write a MDB with given annotations. you can keep those annotated configurations in xml file if you need.
@MessageDriven(name = "myMqMDBean",
activationConfig =
{
@ActivationConfigProperty(propertyName = "messagingType", propertyValue = "javax.jms.MessageListener"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "myAppleQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "channel", propertyValue = "SYSTEM.DEF.SVRCONN"),
@ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "true")
})
@ResourceAdapter(value = "wmq.jmsra.rar")
public class MqMDBean implements MessageListener {

@Override
public void onMessage(Message message) {
try {

if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
System.out.println("Message Received ...");
System.out.println(" >> " + txtMsg.getText());
} else {
System.out.println("Unknown message received. type of message is " + message.getClass());
}
} catch (Exception e) {
System.out.println("Exception occurred while processing the message");
}

}
}

After deploying start JBoss. if there are no any exception you are lucky.

Ok now goto MQ explorer and put a message. Your MDB will grab it. You can find complete source in github. use bellow to get source
'git clone git@github.com:changit/jboss-mdb-with-wsmq.git'

1 comment:

  1. Do you see MDB ramp up and more load is sent through the queue?

    ReplyDelete