Message Driven Beans (MDB) using Seam and EJB 3.0

In my previous post, JMS Messaging with Seam, I showed you how to setup a queue and send a message to that queue using Seam. Now it is time to read that message from the queue and do something with it. As you guessed, it is very simple to do when using Seam and EJB 3.0.

The code below shows how to read a text message from the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package osbi.achs.messaging;

import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.ejb.ActivationConfigProperty;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;

@MessageDriven(name = "SearchQueueListenerBean", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/achsSearch") })
public class SearchQueueListenerBean implements MessageListener {
  @Logger
  private Log log;

  @Override
  public void onMessage(Message message) {
    try {
      TextMessage textMsg = (TextMessage) message;
      System.out.println(textMsg.getText());
    } catch (JMSException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

If you followed the previous post you should see the “Seam ROCKS!” message.

That’s great but in the real world you will probably be working with objects and not simple text messages. Don’t worry. With Seam and EJB 3.0 this is also a breeze.

Lets assume in the previous post I sent a Person object to the queue instead of a text message. The code below shows you how to read the Person object from the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package osbi.achs.messaging;

import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.ejb.ActivationConfigProperty;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;
import osbi.achs.model.Person;

@MessageDriven(name = "SearchQueueListenerBean", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/achsSearch") })
public class SearchQueueListenerBean implements MessageListener {
  @Logger
  private Log log;

  @Override
  public void onMessage(Message message) {
   
    try {
      ObjectMessage objectMessage = (ObjectMessage)message;
      Person person = (Person) objectMessage.getObject();
      // TODO Do something with message
      System.out.println("Person = " + person.getSurName() + ", " + person.getGivenName());
    } catch (JMSException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

Messaging couldn’t be any easier.



Comments

  1. Murali August 25th

    Comment Arrow

    How to remove queue name form the @ActivationConfigProperty(propertyName = “destination”, propertyValue = “/queue/achsSearch”) .

    I want to configure this queue name like dynamic loading. Loading Queue name is from properties file.

    Please suggest me?


  2. Matt July 23rd

    Comment Arrow

    Good example, but the real trick is doing transactional updates. Any perspective there? If you are just getting the bean invoked, yes piece of cake. The real issue is deciding what your layered architecture would look like to share services and persistence layer code between async and web thread usage. Still looking for solution. Any thoughts?


  3. Brian October 7th

    Comment Arrow

    Matt, The above example is pretty basic. For a layered service to share I would create a REST service that handles the JMS and transactional communication and just have my apps call the REST service.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Brian

Brian Abston is a Java Developer in Oklahoma who also loves technology.