MessageListener Dies

I am baffled by this one. I am running MQSeries and simply trying to register a MessageListener with a Receiver. If there is a message in the queue, the OnMessage method is called, then the Thread dies. If no message is in the queue the thread just dies. If I try to extend thread to keep it running, it simply doesn't consume at all. I've had to implement a while loop that sleeps to keep this thing running which makes no sense.

Any suggestions?

Code sample:

import com.ibm.mq.*;

import com.ibm.mq.jms.*;

import javax.jms.*;

import javax.naming.*;

import javax.naming.directory.*;

public class MessageGet implements MessageListener{

private static String HOSTNAME = "MyHOST";

private static String QMGRNAME="";

private static MQQueueManager qMgr;

public static void main(String[] args) {

MessageGet mg = new MessageGet();

mg.go();

}

public void go(){

QueueSession session2 = null;

QueueConnection connection = null;

QueueReceiver receiver1 = null;

try {

String outString = "This is a test";

boolean transacted = false;

Queue ioQueue= null;

//Set up the Connection Factory but don't use default binding

//Instead use Java binding

MQQueueConnectionFactory factory = new MQQueueConnectionFactory();

factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);

factory.setHostName(HOSTNAME);

//Create Queue Connection

connection = factory.createQueueConnection();

((MQQueueConnectionFactory)factory).setQueueManager(QMGRNAME);

connection = factory.createQueueConnection();

session2 = connection.createQueueSession(false,

Session.AUTO_ACKNOWLEDGE);

ioQueue = session2.createQueue("MYQUEUE");

receiver1 = session2.createReceiver(ioQueue);

receiver1.setMessageListener(this);

connection.start();

System.out.println("starting connection");

} catch (JMSException je) {

System.err.println("caught "+je);

Exception e = je.getLinkedException();

if (e != null) {

System.err.println("subexception: "+e);

}

} catch (Exception e) {

System.err.println("caught "+e);

}finally{

try{

receiver1.close();

session2.close();

connection.close();

}catch(Exception e){}

}

}

public void onMessage(Message message){

try{

//create a Message Processor(Message)

System.out.println("Entering onMessage");

if (message instanceof TextMessage) {

System.out.println("Message ="+ ((TextMessage)message).getText());

}

}catch (Exception e){

}

}

}

Thks...JAS

[2785 byte] By [jsleeman] at [2007-9-19]
# 1

onMessage is an non-blocking method of receiving messages. in order to prevent your main thread from exiting, you must block somewhere...

instead of the bogus while loop, try thread.join

try {

Thread.currentThread().join();

} catch (Exception e) {

e.printStackTrace();

}

}

turbobutton at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...