You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
Since 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 session bean.
Note: I assume you know how to create and use session beans.
In 3 steps:
Step 1: Here is the code for the MDB (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>
OR YOU CAN ALSO MAKE THIS MORE SUCCINT, IF YOU LIKE:
<jms:managed-queue-sender name="phoneQueueSender" auto-create="true" queue-jndi-name="queue/phoneQueue"/>
Step 3: Here is the session bean function which sends a message
(YOU NEED TO IMPORT javax.jms.*)
Note: I have not shown the full body of the session bean, just the code relevant to the MDB.
@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);
}
}