You will find here code for a Sample Message Driven Bean along with the ejb-jar.xml and jboss.xml. The MDB, below, is listening to a queue named testQueue which comes configured as part of jboss(the configuration can be found in jbossmq-destinations-service.xml file).
SampleMDBean.java:
package sample; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import javax.ejb.MessageDrivenBean; import javax.ejb.MessageDrivenContext; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; /** * Sample Message driven bean * */ public class SampleMDBean implements MessageDrivenBean, MessageListener { /** * Message driven context */ private MessageDrivenContext messageDrivenContext; /** * */ public void ejbCreate() { } /** * @see javax.ejb.MessageDrivenBean#ejbRemove() */ public void ejbRemove() { } /** * @see javax.ejb.MessageDrivenBean#setMessageDrivenContext(javax.ejb.MessageDrivenContext) */ public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) { this.messageDrivenContext = messageDrivenContext; } /** * @see javax.jms.MessageListener#onMessage(javax.jms.Message) */ public void onMessage(Message message) { //do some logic in this method System.out.println("Entered the onMessage method of MDB"); } } //end of MDB
ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar > <description><![CDATA[No Description.\]\]\></description> <display-name>Generated by XDoclet</display-name> <enterprise-beans> <!-- Message Driven Beans --> <message-driven > <description><![CDATA[My first Message Driven Bean(MDB) \]\]\></description> <ejb-name>MyFirstMDB</ejb-name> <ejb-class>sample.SampleMDBean</ejb-class> <transaction-type>Bean</transaction-type> <acknowledge-mode>Auto-acknowledge</acknowledge-mode> <message-driven-destination> <destination-type>javax.jms.Queue</destination-type> </message-driven-destination> </message-driven> </enterprise-beans> </ejb-jar>
jboss.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd"> <jboss> <security-domain>java:/jaas/http-invoker</security-domain> <enterprise-beans> <message-driven> <ejb-name>MyFirstMDB</ejb-name> <destination-jndi-name>queue/testQueue</destination-jndi-name> </message-driven> </enterprise-beans> </jboss>
Referenced by:
Comments