Also attached is the helper class
package com.dummy.util;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import org.jboss.example.jms.common.Util;
public class MessageUtility {
private ConnectionFactory cf = null;
public void drain(String queueName) throws Exception
{
System.out.println("Inside Message Utility:drain");
InitialContext ic = new InitialContext();
Queue queue = (Queue)ic.lookup(queueName);
ic.close();
System.out.println("Message Utility: Found teh queue");
Session session = null;
Connection conn = null;
try
{
conn = getConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
System.out.println("Inside Message Utility:Created Consumer");
Message m = null;
do
{
m = consumer.receive(1L);
}
while(m != null);
}
finally
{
if (conn != null)
{
closeConnection(conn);
}
}
}
public void send(String txt, String queueName) throws Exception
{
InitialContext ic = new InitialContext();
System.out.println("Inside Message Utility:send QueueName :"+
queueName);
if (!Util.doesDestinationExist(queueName,ic))
{
System.out.println("Destination " + queueName + " does not exist,
deploying it");
Util.deployQueue(queueName,ic);
}
Queue queue = (Queue)ic.lookup(queueName);
System.out.println("Inside Message Utility:send");
ic.close();
Session session = null;
Connection conn = null;
try
{
System.out.println("Inside Message Utility:send: Before getting
connection");
conn = getConnection();
System.out.println("Inside Message Utility:send:After getting
connection");
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("Inside Message Utility:send:session" + session);
MessageProducer producer = session.createProducer(queue);
TextMessage tm = session.createTextMessage(txt);
System.out.println("Inside Message Utility:send:producer is "+
producer);
producer.send(tm);
System.out.println("message " + txt + " sent to " +
queueName);
}
finally
{
if (conn != null)
{
closeConnection(conn);
}
}
}
public int browse(String queueName) throws Exception
{
InitialContext ic = new InitialContext();
Queue queue = (Queue)ic.lookup(queueName);
ic.close();
System.out.println("Inside Message Utility:browse");
Session session = null;
Connection conn = null;
try
{
conn = getConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
QueueBrowser browser = session.createBrowser(queue);
ArrayList list = new ArrayList();
for(Enumeration e = browser.getEnumeration(); e.hasMoreElements(); )
{
list.add(e.nextElement());
}
return list.size();
}
finally
{
if (conn != null)
{
closeConnection(conn);
}
}
}
public String receive(String queueName) throws Exception
{
InitialContext ic = new InitialContext();
Queue queue = (Queue)ic.lookup(queueName);
ic.close();
System.out.println("Inside Message Utility:receive");
Session session = null;
Connection conn = null;
try
{
conn = getConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
System.out.println("blocking to receive message from queue " +
queueName + " ...");
TextMessage tm = (TextMessage)consumer.receive(5000);
if (tm == null)
{
throw new Exception("No message!");
}
System.out.println("Message " + tm.getText() + "
received");
return tm.getText();
}
finally
{
if (conn != null)
{
closeConnection(conn);
}
}
}
public Connection getConnection() throws Exception
{
Connection connection = null;
System.out.println("Inside Message Utility:send.getConnection:cf"+cf);
try
{
InitialContext ic = new InitialContext();
System.out.println("Inside Message Utility:send.getConnection:ic"+ic);
//cf = (ConnectionFactory)ic.lookup("java:JmsXA");
//cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
cf = (ConnectionFactory)ic.lookup("java:/ConnectionFactory");
System.out.println("Inside Message Utility:send.getConnection:create
connection");
connection = cf.createConnection();
System.out.println("Inside Message Utility:send.getConnection:before
connection start:conn is"+ connection);
connection.start();
ic.close();
}
catch(Exception e )
{
e.printStackTrace();
if(connection != null)
{
closeConnection(connection);
}
System.out.println("Failed to get connection...exception is " + e);
throw e;
}
return connection;
}
public void closeConnection(Connection con) throws Exception
{
try
{
con.close();
}
catch(JMSException jmse)
{
System.out.println("Could not close connection " + con +"
exception was " + jmse);
throw jmse;
}
}
public void initiate()
{
try
{
InitialContext ic = new InitialContext();
//cf = (ConnectionFactory)ic.lookup("java:/JmsXA");
cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
ic.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Please help in fixing this issue
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4080047#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...