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.










Add Yours
YOU