0

I am using a Message Driven Bean for storing messages in a list as you can see in the code given below:

/**
 *
 * @author sana-naeem
 */
@MessageDriven(mappedName = "jms/Queue-0", activationConfig = {
     @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MyMessageBean implements MessageListener{
   private static   ArrayList<String> list = new ArrayList<String>();

    public MyMessageBean() {
    }

    @Override
   public void onMessage(Message message) {
       Textmessage msg = (TextMessage) message;
    try {

            if (message instanceof TextMessage) {

                list.add("Messages:     "+msg.getText());

            } else {
                System.out.println("No Text!!!");
                 }
        } catch (JMSException ex) { 
                System.out.println("JMS.Exception....!!!");

        }

    }


  public static ArrayList<String> getList() {
        return list;
}

   public void setList(ArrayList<String> list) {

    this.list = new ArrayList<String>();

   }

}

Now the problem is when I access the getter method from another Java Class; it is displaying list size=0;
Can I please know why is this happening;
I want to get that list in another Java Class;
If there is something wrong with the Queue, kindly let me know How to fix it???
It was working fine before;
Actually, previously I was using a Servlet to send messages but now, I am using a simple Java class with some initial context parameters defined...; so now the list is not working as expected...
Any advice or suggestion would be highly appreciable. Thank you!

Developer
  • 161
  • 2
  • 3
  • 15

1 Answers1

0

You shouldn't be using mutable static fields in an MDB. When you think MDB, think stateless, because that's how the container treats them. Here is a good reference;

http://www.coderanch.com/t/312086/EJB-JEE/java/Clarification-static-fields

Unfortunately, there isn't one single answer for your question. Without knowing anything about the rest of your application, how it is designed, what components you are using, how they interact, how it will be deployed, etc., it is difficult to give advice. But the first thing that comes to mind, given what limited information I have, is that you can create a separate object to wrap a Private ArrayList, and store a reference to that object in an appropriate context. Your MDB can then access that object. In that case, you'd have methods in the object to modify/read the ArrayList, and you'd synchronize these methods internally on the underlying ArrayList because the methods will be accessible by multiple threads.

But this is a brute force approach, not knowing anything else about your application. And depending on how your application is designed and deployed, this might not be the right advice. But the point here is that;

  • You need to separate your stateful object from what is intended to be a stateless object.
  • You need to make it accessible from an appropriate context.
  • You need to synchronize modify/read methods on the underlying POJO (ArrayList) if the containing object will be accessible by multiple threads.
RickF
  • 36
  • 5
  • but Sir, the point is I need to store data in that list and then get it afterwards from another Java class; How can I do it then?? – Developer May 08 '14 at 00:26
  • Actually, as I said it was wrking fine previously; I was using a Servlet to send messages but now, I am using a simple Java class wid some initial context parameters defined...; so now the list is nt wrking as expected... – Developer May 08 '14 at 03:34
  • Anyway, it is not the correct way to use an MDB, so you will get inconsistent results. I've updated my post above. – RickF May 08 '14 at 03:37
  • yeah, may be that's nt a good programming practice; but I need to get the list of messages – Developer May 08 '14 at 03:38
  • can you please elaborate your answer; what do u mean by create a separate object to contain the ArrayList privately? – Developer May 08 '14 at 05:02