File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MQ JMS MessageListener problem

 
bala.v reddy
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using MQSeries with JMS for asynchronous messaging. I am implimenting MessageListener interface and implimenting onMessage() method. when i don't put main thread on wait(), the listener is not invoking. if i put main thread in wait() then, onMessage() method is being called and consuming msgs. But in Asynchronous messaging, we should not put main thread in wait() isn't it??

Please let me know the solution how to impliment without wait() in main() method.

thanx in advance. i am pasting the code here


import com.ibm.mq.jms.*;
import javax.jms.*;

public class JMSMQReceive implements MessageListener
{


private String CHANNEL ="SYSTEM.DEF.SVRCONN";
private boolean quit = false;
private QueueReceiver queueReceiver=null;


private QueueConnectionFactory mqFactory = null;
private QueueConnection queueConnection = null;
private QueueSession session = null;
private boolean transacted =false;


private void openMqConnection(String aQueueMagager,String aPort,String mqChannel ,String aServer)
throws javax.jms.JMSException
{
mqFactory = new MQQueueConnectionFactory();
if( aQueueMagager== null) {aQueueMagager = new String();}
if( aPort== null) { aPort= new String("1414");}
if( mqChannel == null) { mqChannel = new String(this.CHANNEL);}
if( aServer == null) {aServer = new String("localhost");}
System.out.println("aServer:"+aServer);
((MQQueueConnectionFactory)mqFactory).setTransportType(JMSC.MQJMS_TP_BINDINGS_MQ);
((MQQueueConnectionFactory)mqFactory).setQueueManager(aQueueMagager);
//((MQQueueConnectionFactory)mqFactory).setHostName(aServer);
//((MQQueueConnectionFactory)mqFactory).setChannel(mqChannel);
//((MQQueueConnectionFactory)mqFactory).setPort(Integer.decode(aPort).intValue());

queueConnection = mqFactory.createQueueConnection();
System.out.println("After :"+aServer);
Queue tmp_queue = this.getQueueSession().createQueue("QUEUE1");
queueReceiver = this.getQueueSession().createReceiver(tmp_queue);
queueReceiver.setMessageListener(this);
queueConnection.start();
System.out.println("After start");


}


private QueueSession getQueueSession()
throws javax.jms.JMSException
{
if (this.session != null)
{
return this.session;
}else
{
this.session = this.queueConnection.createQueueSession(this.transacted,Session.AUTO_ACKNOWLEDGE);
return this.session;
}
}

private void closeQueueSession()
throws javax.jms.JMSException
{
if ( this.session != null)
{
this.session.close();
this.session = null;
}
}



public JMSMQReceive(String aQueueMagager,String aPort,String aServer)
throws javax.jms.JMSException
{
this.openMqConnection(aQueueMagager,aPort,this.CHANNEL,aServer);

}


public static void main(String[] args)
{
try{

JMSMQReceive mQHandlerBean = new JMSMQReceive(null,null,"sce3.de.db.com");

try{

synchronized(mQHandlerBean) {
while (! mQHandlerBean.quit) {
try {
mQHandlerBean.wait();
} catch (InterruptedException ie) {}
}
}
mQHandlerBean.closeQueueSession();


}catch(javax.jms.JMSException e)
{
System.out.println(e.getMessage());

}

}catch(javax.jms.JMSException e)
{
System.out.println(e.getMessage());

}
}

public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}

System.out.println("Message Received: "+ msgText );

if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic