Seam MDB example
mlavannis Jul 28, 2008 8:26 PMSince I could not find a simple example demonstrating Seam-MDB (Mesage Driven Bean) usage, I am posting one for the benefit of others. We will create a simple EJB3 MDB called "Phone" and send a message to it from a EJB3 stateless bean (I assume you know how to create and use stateless beans).
In 3 steps:
Step 1: Here is the code for the MDP (Phone.java):
-----------------------------------------------------
package com.mkl.message;
import javax.jms.*;
import javax.ejb.MessageDriven;
import javax.ejb.ActivationConfigProperty;
@MessageDriven(name="PhoneMessageBean", activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="queue/phoneQueue")
})
public class Phone implements MessageListener {
@Override
public void onMessage(Message msg) {
// TODO Auto-generated method stub
TextMessage textMsg = (TextMessage) msg;
try {
System.out.println("Received Phone Call:" + textMsg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------------------------
Step 2: Here is the component addition to components.xml
-----------------------------------------------------
<component name="phoneQueueSender"
class="org.jboss.seam.jms.ManagedQueueSender">
<property name="queueJndiName">queue/phoneQueue</property>
</component>
-----------------------------------------------------
Step 3: Here is the stateless bean function which sends a message
(YOU NEED TO IMPORT javax.jms.*)
-----------------------------------------------------
@In(create=true)
private transient QueueSender phoneQueueSender;
@In(create=true)
private transient QueueSession queueSession;
public void make_a_call (){
try
{
phoneQueueSender.send( queueSession.createTextMessage("You are in trouble"));
}
catch (Exception ex)
{
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
-----------------------------------------------------
In 3 steps:
Step 1: Here is the code for the MDP (Phone.java):
-----------------------------------------------------
package com.mkl.message;
import javax.jms.*;
import javax.ejb.MessageDriven;
import javax.ejb.ActivationConfigProperty;
@MessageDriven(name="PhoneMessageBean", activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="queue/phoneQueue")
})
public class Phone implements MessageListener {
@Override
public void onMessage(Message msg) {
// TODO Auto-generated method stub
TextMessage textMsg = (TextMessage) msg;
try {
System.out.println("Received Phone Call:" + textMsg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------------------------
Step 2: Here is the component addition to components.xml
-----------------------------------------------------
<component name="phoneQueueSender"
class="org.jboss.seam.jms.ManagedQueueSender">
<property name="queueJndiName">queue/phoneQueue</property>
</component>
-----------------------------------------------------
Step 3: Here is the stateless bean function which sends a message
(YOU NEED TO IMPORT javax.jms.*)
-----------------------------------------------------
@In(create=true)
private transient QueueSender phoneQueueSender;
@In(create=true)
private transient QueueSession queueSession;
public void make_a_call (){
try
{
phoneQueueSender.send( queueSession.createTextMessage("You are in trouble"));
}
catch (Exception ex)
{
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
-----------------------------------------------------