This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

MQ JMS MessageListener problem  RSS feed

 
Greenhorn
Posts: 4
  • 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();
}
}

}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!