IVTMDB.java program

package ejbs;

// based on a sample program from IBM MQ
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;

import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class IVTMDB
implements MessageListener, MessageDrivenBean
{
private static final long serialVersionUID = -337338331639L;
public void onMessage(Message message)
{
Connection connection = null;
Session session = null;
MessageProducer producer = null;

try
{
InitialContext ctx = new InitialContext();
ConnectionFactory cf = (ConnectionFactory)ctx.lookup(“CF1”);

System.out.println(“ConnectionFactory ” + cf);

Destination dest = message.getJMSReplyTo();

System.out.println(this.getClass().getSimpleName()+” ReplytoQueue ” + dest );
// the above line gives output like IVTMDB ReplytoQueue queue://QMA/CP0002?targetClient=1
if (null == dest) {
throw new IllegalArgumentException(“The IVTMDB received a message which does not have a replyTo queue set”);
}
connection = cf.createConnection();
// connection.start(); //Starts (or restarts) a connection’s delivery of incoming messages. So not needed?

session = connection.createSession(false, 1);

producer = session.createProducer(dest);

TextMessage response = session.createTextMessage(“test response message from the WMQ_IVT_MDB”);
response.setJMSCorrelationID(message.getJMSMessageID());
producer.send(response);

return;
}
catch (Exception je)
{
System.out.println(“Something went wrong.” + je);
System.out.println(je.getStackTrace()[0].getFileName() + je.getStackTrace()[0]. getLineNumber());
}
finally
{
if (connection != null) {
try
{
connection.close();
}
catch (JMSException je)
{
System.out.println(“Something went wrong II.” + je);
}
}
}
}

public void ejbCreate() {
System.out.println(this.getClass().getSimpleName()+”:EJBCreate”);
}

public void ejbRemove()
{
System.out.println(this.getClass().getSimpleName()+”:EJBRemove”);
}

public void setMessageDrivenContext(MessageDrivenContext arg0)
// throws EJBException
{}
}