Hi everyone,
This week I was tasked with getting a MDB in Glassfish to receive messages from ActiveMQ. It took a little bit of work to get it figured out, but now that I know how it works it is actually quite simple. Below is a description of exactly what I had to do. Id also like to thank Ramesh and Zoltan for their help with this!
The first step is to deploy the ActiveMQ resource adapter. The resource adapter comes with the ActiveMQ instillation and can be found at ACTIVEMQ_HOME\lib\optional\activemq-rar-4.1.1.rar. I deployed the resource adapter using the app server admin console by selecting Applications->Connector Modules->Deploy. Since ActiveMQ was running on the same system I accepted all default properties.
Next we need to create an Admin Object Resource that will link the jms queue or topic to our newly deployed resource adapter. From the admin console select Resources->Connectors->Admin Object Resources->New. For JNDI Name I used jms/TestTopic, Resource Type is javax.jms.Topic and be sure to choose the ActiveMQ Resource Adapter. In the Additional Properties set the property Name=TestTopic
The final bit of configuration is to create a Physical Destination. In the Admin Console select Configuration->Java Messaging Service->Physical Destinations->New. Create a Physical Destination with a Name of TestTopic and a Type of javax.jmsTopic. No additional properties are needed.
The code for the Message Driven Bean is as follows:
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName="destination", propertyValue="TestTopic")
},
mappedName = "jms/TestTopic"
)
public class MessageBean implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Message Received:" + textMessage.getText());
} catch ( JMSException e ) {
e.printStackTrace();
}
}
}
Because we want our MDB to receive messages from ActiveMQ we must also provide a sun-ejb-jar.xml deployment descriptor that specifies what resource adapter should be used. Below is the complete sun-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "
http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>MessageBean</ejb-name>
<mdb-resource-adapter>
<resource-adapter-mid>activemq-rar-4.1.1</resource-adapter-mid>
</mdb-resource-adapter>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
Below is a simple stand alone java program that posts JMS messages to a local ActiveMQ instance. If you have the setup correct, the MDB will receive the messages.
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQTest {
public static void main(String[] argv) throws Exception{
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TestTopic");
MessageProducer publisher = session.createProducer(topic);
publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
connection.start();
for(int x=0;x<5;x++){
TextMessage message = session.createTextMessage("Test message "+x);
System.out.println("Sending message: "+message.getText());
publisher.send(message);
}
connection.stop();
connection.close();
System.out.println("Done.");
}
}
[Message sent by forum member 'mikehake' (mikehake)]
http://forums.java.net/jive/thread.jspa?messageID=254034