• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

is using while(true) loop in run mthd of thread good idea 2 make it wait indefinatley

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats fine.
boolean aVar = true;
while (aVar)
{
// check for if there are any messages
// if there are any messages in the queue, process it
//or just sleep for next 10 seconds
Thread.sleep(10000);
}

But in this case MessgaeReceiver thread should be run as a Daemon thread in the application sothat when there no non-daemon threads, this Receiver will automatically exit. Otherwise you may not be able to kill the thread cleanly. I am not sure about how to stop JBOSS server.
 
rahul batra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi lakshamanam,
there are two problems with my code.
if i run a infinite while loop in run() the cPU USAGE becomes 100%.
and second as u said abt the daemon threadthing, i dont want my MessagesReciever to Stop. It should run forever once started since my application is real time, it will be deployed with MessaageReceiver started and then it should be ready to listen for messages on the queue forever.
thanks
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,

Did you have a look at MDBs (Message Driven Beans)?

It may solve your problem. MDBs are special EJBs which are message listeners, on receipt of a message in the queue, the container will call the onMessage() method where you can have your business logic.

regards,
Vishnu
 
rahul batra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vishnu,thanks, that sounds interesting,
can u please tell me two things more
like does the Message Driven Beans handle multithreading also. i mean lets say i get 30-40 messages in a minute on my queue. then what would be the behaviour of MDB's. will it pass all those messages to OnMessage , if yes then i guess i will have to write my threading code in OnMessage where i assign task of each message to a new thread so that my MDB is free to get next message, right ?
please clear my doubts if u can.
thanks once again
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MDBs will do the vast majority of the tricky bits for you. Your onMessage() method is called by the container, just like any method on a public session bean. You don't have to worry about looping or polling or any of that hard stuff.

You can configure the number of instances of the MDB. Each one runs on its own thread. That sets a max on how many you can handle at once, which is a good way to avoid being flooded by incoming messages.

Let us know if that works out!
 
reply
    Bookmark Topic Watch Topic
  • New Topic