Unable To Recevie a message from Queue using JNDI
dkiran Mar 1, 2010 3:15 AMHello,
I'm unable to receving a queue message from queue,
the receiver code is:
public class QReceiver implements MessageListener { String
QueueConnectionFactory
qconFact;QueueConnection
qcon;QueueSession
qsession;QueueReceiver
qreceiver;javax.jms.Queue
queue;TextMessage
msg;
// MessageListener interface method , called when there is a message.
public void onMessage(Message msg) {
try {
String rmsg;
// as there are different types of msg's we are testing it here.
if (msg instanceof TextMessage) {
rmsg = ((TextMessage) msg).getText();
}
else {rmsg = msg.toString();
}
System.
out.println(rmsg);
// acknowledge the receipt of the message
msg.acknowledge();
}
catch (Exception jmse) {jmse.printStackTrace();
}
}
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
/* get the connection and create the connection object */
qconFact = (QueueConnectionFactory) ctx.lookup(connfactname);
qcon = qconFact.createQueueConnection();
/* using connection create a non transactional session */
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
/* look up for the destination ie queue */
queue = (javax.jms.Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
}
public void close() throws JMSException {
qreceiver.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = new InitialContext();
QReceiver qr =
new QReceiver();System.
out.println(" Trying to initialize the QReceiver");qr.init(ic,
"/queue/exampleQueue");System.
out.println("Receiving messages");
}
}
when i use synchronize the object in onMessage method and wait method in main Then I'm getting/receiving a message
onMessage(){
.............
synchronized(this) {
this.notifyAll();
}
}
------------------------------------------------
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QReceiver qr = new QReceiver();
System.out.println(" Trying to initialize the QReceiver");
qr.init(ic, "/queue/exampleQueue");
System.out.println("Receiving messages");
synchronized(qr) { try { qr.wait(); // waits till we get notification
System.out.println(" Stoping the main thread"); //qr.close();
} catch (InterruptedException ie) {} } }
}
Is it required to maintain a synchronization in HornetQ?
Is any other way to Implement Message Receiver?
Please help!
Regards,
Kiran