0

Im new to java message-driven beans and i have a little problem. I made a test web project(server is glassfish 4.0).

In messageclient, i'm sending 3 Strings to message queue. If i understand message-driven beans correctly, message-driven bean should write all values out in my console, but instead it writes only the first value in queue. I'm trying to implement this because i need different thread to send emails to customer from my web application. Is there maybe a better way?

here is the code of my message client:

@Named(value = "messageDrivenbean")
@ViewScoped
public class MessageDrivenBeanClient implements Serializable
{
ArrayList <String>mail;
@Resource(name="connFactory", mappedName="mailConnFactory")
private  QueueConnectionFactory QueueConnectionFactory;

@Resource(name="jmsQueue", mappedName="Queue")
private  Queue queue;

 /**
 * Creates a new instance of testingbean
 */
 public MessageDrivenBeanClient() 
 {
 }
 @PostConstruct
 public void init(){

 }
 Connection conn;
 public void start() throws JMSException
 {
  String text;
  Message msg = null;
 final int NUM_MSGS = 3;


  conn = QueueConnectionFactory.createConnection();

 Session sesion=conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
 MessageProducer msgProducer=sesion.createProducer(queue);
 msg=sesion.createTextMessage();


for (int i = 0; i < NUM_MSGS; i++) {
text = "This is message " + (i + 1);
System.out.println("Sending message: " + text);

msg.setStringProperty("message", text);
msgProducer.send(msg);
  }
 }

}

And my message-driven bean:

@MessageDriven(activationConfig =
{
 @ActivationConfigProperty
 (propertyName="destinationType",propertyValue="javax.jms.Queue"),            
 @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "Queue")
 })
 public class MsgDrivnBean implements MessageListener
 {

  @Resource
  private MessageDrivenContext mdc;

  public MsgDrivnBean()
 {
 }



@Override
  public void onMessage(Message message)
  {
    TextMessage tm=null;
    try{
      if(message instanceof TextMessage){
       tm= (TextMessage) message;
       System.out.println(message.getStringProperty("message"));
       System.out.print("sem v msgdrivenbeanu"+ tm.getStringProperty("message"));
      //logger.error(tm.getText());
      }
      else
      {
        System.out.println("Error");
      }
    }
    catch(JMSException jms)
    {
      jms.printStackTrace();
      mdc.setRollbackOnly();
    }
  }


}

My current output is:


Sending message: This is message 1
INFO:   Sending message: This is message 2
INFO:   Sending message: This is message 3
INFO:   This is message 1
INFO:   sem v msgdrivenbeanuThis is message 1

I think it should also write out message 2 and message 3, but it doesn't.

Please help me.

Salko
  • 43
  • 5
  • I would think so too. Not sure this is contributing to it, but I don't see you closing the session and connection in the client code. Maybe try adding that to the client. – mikemil Feb 17 '14 at 03:50
  • Tried but no luck.... – Salko Feb 17 '14 at 12:26

1 Answers1

0

This is a very common error, actually. In the JMS spec, whenever you connect you're always dealing with a pooled connection in an app server. The connections are always transacted.

You cannot reuse messages this way. Please instantiate a new message for each time you're sending one.

Please try adding message.acknowledge() to your MDB's code as well.

Edit: Also, if you're really on Java EE 7, you should consider switching to the JMSContext class to send messages. Much cleaner API.

John Ament
  • 11,595
  • 1
  • 36
  • 45