0

I need to consume messages from a JMS queue deployed on JBoss 7.2. My consumer is particular in the sense that it needs to keep the processed message, in order to "chain it" with the next message : Mn = Mn-1 + Mn, before sending it to a DB.

I know MDB are stateless, so I can't use them for this specific use case.

I opted for a classic Java Thread, applying an endless loop inside its run method, and call queue recieve.

public void run() {
     // queue to listen to
     consumer = session.createConsumer(myQueue);
     // start
     con.start();
     while (true) {
        // listen ...
        ObjectMessage recievedObj = (ObjectMessage) consumer.receive();
        // do some work

        lastRecievedObj = recievedObj;
     }
}

The fact is that it's not recommended to handle threads in a Java EE environment (except for Java EE 7).

What do you think? Do you have a better idea?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
HassLion
  • 1
  • 4
  • Old school approach: put your receiver logic in some Servlet, which may start threads at it's discretion. Then invoke the EJB when all messages have arrived. – Gyro Gearless Sep 26 '14 at 08:01
  • which EJB to invoke ? – HassLion Sep 26 '14 at 08:06
  • Some application servers (IBM WebSphere, Oracle WebLogic) support CommonJ WorkManager in a way that you can do a JNDI lookup and get a managed thread from container. – Magic Wand Sep 26 '14 at 08:06
  • Is this possible for JBoss 7.2 ? i'm affraid we have no choice but to use JBoss 7.2. – HassLion Sep 26 '14 at 16:22
  • >`which may start threads at its discretion` - This is not entirely correct. The "don't use threading" recommendation indeed says "EJB", but at the time EJB was thought to be a kind of synonym for Java EE. So the recommendations were most likely intended to account for Servlets, EJBs, and all else there is in Java EE. – Arjan Tijms Sep 26 '14 at 19:30
  • Can't you just `@Inject` an `@ApplicationScoped` or `@Singleton` EJB into your MDB to get the next counter value? – Alexander Langer Sep 26 '14 at 20:39
  • @Alexander Langer an MDB is a stateless EJB, it can't be ApplicationScoped nor Singleton – HassLion Sep 29 '14 at 08:04
  • That's why you shall inject another bean that holds the global counter. – Alexander Langer Sep 29 '14 at 08:11
  • I will try this, thanks , but i don't have a good feeling, because this counter will be accessed by one or more MDB, i don't how it will react and if it's threadsafe – HassLion Sep 29 '14 at 16:14
  • this didn't work. i think i really need to use Threads, because i want the number of threads or worker configurable, and each thread or worker should keep the state (lastprocessedMsg). Has any one already tried this solution http://stackoverflow.com/questions/13932083/jboss-java-ee-container-and-an-executorservice ?? – HassLion Sep 30 '14 at 15:58

0 Answers0