My Requirement
my Application is deployed on
JBOSS which has its JMS. now lets say i have defined a Queue named "MyAppQueue" and i know that all the senders whereever they may be will post there messages on this queue.
Now my responsibility is to write a MessageReceiver which is continously running and checking this queue for messages. whenever it receives a message it immidiately calls the messagesListener which processes the message.
So sow my requirement of Writing such MessageReceiver is that , it should always be in running mode(that means once i start it, it starts forever since this is going to be a REal time application) and continously checking the queue, as soon as some message comes it calls messageListener. and also that this messageReceiver should be fast , i mean coz many messages may come at once say 40-50 messages in a minute.
so please tell me how do i now write this MessageReceiver.
the way i have written
My Code
package test.jms;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MyThreadReceiver implements Runnable{
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;
MyQueueListener myQueueListener = new MyQueueListener();
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
String j;
public void run() {
// TODO Auto-generated method stub
while(true){
try {
// Thread.sleep(10000);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThreadReceiver myReceiver = new MyThreadReceiver();
myReceiver.establishConnection();
/* after connection is established to the queue, i am not closing the connection.i am making a
thread and passing my class object to it and in run method i have written while(true ) loop to somehow make my MessageReceiver program run indefinately.. */
Thread thread = new Thread(myReceiver);
thread.start();
System.out.println("finished");
}
/**
this method opens connection to the queue. and i am not closing this connection .
*/
private void establishConnection(){
queueName = "queue/testQueue";
try {
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
jndiContext = new InitialContext();
}catch (NamingException e) {
System.out.println("Could not create JNDI API " + "context: " + e.toString());
System.exit(1);
}
try {
queueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
}catch (NamingException e) {
System.out.println("JNDI API lookup failed: " + e.toString());
System.exit(1);
}
try {
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueReceiver.setMessageListener(myQueueListener);
queueConnection.start();
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
//if (queueConnection != null) {
//try {
//queueConnection.close();
//}catch (JMSException e) {}
//}
}
}
}
please correct my code. thanks in advance