Using Websphere MQ with JBoss AS 5

IBM WebSphere MQ is a family of network software products launched by IBM in March 1992. It was previously known as MQSeries, a trademark that IBM rebranded in 2002 to join the suite of WebSphere products. WebSphere MQ, which is often referred to simply as “MQ” by users, is IBM’s Message Oriented Middleware offering(JMS).

Lets see how we can configure MQ with Jboss as a pert of JMS.

Step 1: Install JBoss 5
To install JBoss AS 5, You can download it from this link http://www.jboss.org/jbossas/downloads/

Step 2: Install Webpshere MQ 7
To install visit IBM website and download a free copy of trial.

Step3: Install wmq.jmsra.rar
The copy of wmq.jmsra.rar could be found in Inside WebSphere mq install directory e.g  wmq/java/lib/jca, Also the same can be found  if you have WAS installed. Once you got this copy put it in JBOSS_HOME/server/default/deploy

Step 4: Create wmq.jmsra-ds.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<connection-factories>
<tx-connection-factory>
<jndi-name>IVTCF</jndi-name>
<xa-transaction />
<rar-name>wmq.jmsra.rar</rar-name>
<connection-definition>javax.jms.ConnectionFactory</connection-definition>
<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”>1417</config-property>
<config-property name=”queueManager” type=”java.lang.String”>JBOSS_QM</config-property>
<config-property name=”transportType” type=”java.lang.String”>CLIENT</config-property>
<security-domain-and-application>JmsXARealm</security-domain-and-application>
</tx-connection-factory>
<mbean code=”org.jboss.resource.deployment.AdminObject” name=”jca.wmq:name=ivtqueue”>
<!– Bind this AdminObject with the JNDI name IVTQueue –>
<attribute name=”JNDIName”>IVTQueue</attribute>
<depends optional-attribute-name=”RARName”>jboss.jca:service=RARDeployment,name=’wmq.jmsra.rar'</depends>
<attribute name=”Type”>javax.jms.Queue</attribute>
<attribute name=”Properties”>
baseQueueManagerName=JBOSS_QM
baseQueueName=JBOSS_Q
</attribute>
</mbean>
</connection-factories>

Step 5:  Create a EJB 3.0 project in JBoss Studio or in Eclipse IDE.

Step 6: Write a Message Driven Bean

package com.sa.jms;

import java.io.ByteArrayOutputStream;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.jboss.ejb3.annotation.ResourceAdapter;

/**
* Message-Driven Bean implementation class for: MQJbossExample
*
*/

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = “destinationType”,
propertyValue = “javax.jms.Queue”),

@ActivationConfigProperty(propertyName = “messagingType”,
propertyValue = “javax.jms.MessageListener”),

@ActivationConfigProperty(propertyName = “destination”, propertyValue = “JBOSS_Q”),

@ActivationConfigProperty(propertyName = “queueManager”, propertyValue = “JBOSS_QM”),
@ActivationConfigProperty(propertyName = “hostName”, propertyValue = “127.0.0.1”),
@ActivationConfigProperty(propertyName = “port”, propertyValue = “1417”),
@ActivationConfigProperty(propertyName = “channel”, propertyValue = “SYSTEM.DEF.SVRCONN”),

@ActivationConfigProperty(propertyName = “transportType”,propertyValue = “CLIENT”), },

messageListenerInterface = MessageListener.class)

@ResourceAdapter(“wmq.jmsra.rar”)

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class MQJbossExample implements MessageListener, MessageDrivenBean {

private int maxMessageLength = 50820;

private BytesMessage byteMessage = null;

private static final long serialVersionUID = 1L;

String recievedMessageFromWSMq = “”;

/**
* Default constructor.
*/
public MQJbossExample() {
// TODO Auto-generated constructor stub
}

/**
* @see MessageDrivenBean#ejbRemove()
*/
public void ejbRemove() {
// TODO Auto-generated method stub
}

/**
* @see MessageDrivenBean#setMessageDrivenContext(MessageDrivenContext)
*/
public void setMessageDrivenContext(MessageDrivenContext arg0) {
// TODO Auto-generated method stub
}

/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {

if (message != null) {

if (message instanceof TextMessage) {

recievedMessageFromWSMq = ((TextMessage) message).getText();
System.out.println(” recievedMessageFromWSMq ”
+ recievedMessageFromWSMq);

} else if (message instanceof BytesMessage) {

byteMessage = (BytesMessage) message;

byte[] buffer = new byte[maxMessageLength];

int byteRead;

ByteArrayOutputStream bOut = new ByteArrayOutputStream();

while ((byteRead = byteMessage.readBytes(buffer)) != -1) {

bOut.write(buffer, 0, byteRead);

}

try {

bOut.flush();

recievedMessageFromWSMq = new String(bOut.toByteArray());
System.out.println(” recievedMessageFromWSMq ”
+ recievedMessageFromWSMq);

bOut.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

} catch (JMSException e1) {

e1.printStackTrace();

}
}

}

Step 7:  Now Build the project.Start the server, If all the configuration are correct then you will see binding information of the JNDI name in the server logs, This is the first indication that Link between MQ and JBoss has successfully created.

e.g
21:13:29,812 INFO [ConnectionFactoryBindingService] Bound ConnectionManager ‘jboss.jca:service=ConnectionFactoryBinding,name=JmsXA’ to JNDI name ‘java:JmsXA’
21:13:29,984 INFO [AdminObject] Bound admin object ‘com.ibm.mq.connector.outbound.MQQueueProxy’ at ‘IVTQueue’

Step 8:  Now deploy the application on Jboss.

21:15:42,515 INFO  [EJBContainer] STARTED EJB: com.sa.jms.MQJbossExample ejbName: MQJbossExample

Step 9: Now Open Websphere MQ Explorer and put a message on the Queue.

Step 10: Once you put a dummy message on the Queue, See in the server logs to observe the message recived by our Message Driven bean.

See how simple it is to integrate Jboss with MQ. Hope this helps 🙂


Thanks
R Vashi

Advertisements

JPA/Hibernate:- java.lang.IllegalStateException: No data type for node

Hi,

Few days back I got struck with one of the hiberante exception I was facing while running one named query.

Exception Details:

Exception in thread "main" java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode
 \-[IDENT] IdentNode: 'dpt' {originalText=dpt}

As I haven’t work as that extensive on JPA, due to that I was not able to catch early the root cause of this exception 😦 , After carefully examining around all the entity classes, Then I came to know about the orgin of this issue.

Named Query which was used to fetch the department details.(The below query scenario represent imaginary situation)

select dpt.id.deptId from Dept dept

The root cause was the alias name being used for the Entity reference. Alias name ‘dept‘ should have been used in the SELECT Clause of the HQL. Where as it was referring to ‘dpt‘ and was causing this exception.

Right named query:

select dept.id.deptId from Dept dept

Hope this helps.


Thanks,
R Vashi

Binding Specific IP in Jboss Server

Hi,

By Default Jboss gets a binding with loopback/Floating  IP Address (127.0.0.1). This cause a problem when we are developing web services on Jboss and try to access the service or access the deployed application from different machine.

There is a simple solution for the same.

If you are using Windows machine. Follow the below Steps:

1. Go to Jboss Home
2. Navigate to “bin” directory
3. Edit the run.bat and add this entry “-Djboss.bind.address=10.10.10.10”
4. Start the server

On Linux/Unix Machines:
1. Go to Jboss Home
2. Navigate to “bin” directory
3. Edit the run.sh and add this entry “-b 10.10.10.10”

Hope this helps.


Thanks
R Vashi

Deploying Service Archive on Jboss Server (SAR) for startup service

Hi,

SAR known as Service Archive file is created with the .sar extension same like other archive files (WAR, JAR, EAR) created. There are many things we can achieve with SAR  i.e we can use it like a independent service component, startup service etc..

It gets automatically deployed when application server get starts.  Most of the times these SAR files are used for writing thread related components which have to be running independently.

SAR in JBoss
JBoss’s service archive architecture is based on the Java Extension Management (JMX). During the server bootup process, the service archive deployer (SARDeployer) instantiates the JBoss service classes and exposes them as manageable beans through JMX. You can view all of registered JBoss service components in the JBoss JMX Console web application.

Let’s create and deploy the SAR file on Jboss GA 5.


Dependences required:

Jakarta Commons IO,
Jakarta Commons Logging,
jboss-common.jar,
jboss-jmx.jar ,
jboss-system.jar,
jboss-xml-binding.jar
 

The Management Interface

 public interface TestServiceMBean implements ServiceMBean {
  //add any other methods
  String getServiceName();
 }
 

Now we have to implement our management interface and the start/stop lifecyle methods. We must implement our management according to the JMX spec and we have to extend


The Service Implementation


public class TestService extends ServiceMBeanSupport implements TestServiceMBean {

 // The lifecycle methods
   protected void startService() throws Exception
   {
      System.out.print(“Starting Service”);   }

   protected void stopService() throws Exception
   {
      log.info(“Stoping Service”);
   }

//our management interface method implementation
public String getServiceName(){
 return “TestService”;
}
  
 }


To Create a new implementation class, TestService that implements TestServiceMBean.
As advised by the standard MBean specs in Jboss, the implemented class must have the same name as the MBean interface minus the “MBean” suffix. Now we have created the service component, its time to package and deploy the component.

 

[IMPORTANT]

The deployment descriptor (jboss-service.xml)

<?xml version=”1.0″ encoding=”UTF-8″?>

<server>
  <mbean code=”TestService” name=”default.test:service= TestService “></mbean>
</server>

Now copy the jboss-sevice.xml  inside the META-INF folder.

Now create a SAR for your service

To create the sar file, just bundle all the files(similar like JAR file) with the following structure:

test-service.sar
test-service.sar/META-INF/jboss-service.xml
test-service.sar/TestService.class
test-service.sar/TestServiceMBean.class

Once all done, copy the SAR into jboss-home/server/default/deploy, And start the Jboss server, you will notice in the initialization of the service in the console with printed message(“Starting Service”).

Happy learning!! 🙂


Thanks
R Vashi