In a previous post, I described how to implement
Messaging in Java EE 5 using annotation. This post is an extension of that post to describe how to implement Messaging in Java EE 5 using a deployment descriptor. To implement this, you can use the same client that was described in the other post. The only change needed is in the MDB part. Here's how to implement a Message Driven bean using the deployment descriptor.
- Create the Message driven bean: The message driven bean here is a simple Java class that implements the MessageListener interface. All the configuration will be done in the deployment descriptors.
package jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Messaging3Mdb implements MessageListener {
static final Logger logger = Logger.getLogger("MDB");
public Messaging3Mdb() {
}
public void onMessage(Message inMessage) {
TextMessage msg = null;
logger.setLevel(Level.ALL);
try {
if (inMessage instanceof TextMessage) {
msg = (TextMessage) inMessage;
logger.info("MESSAGE BEAN: Message received: " + msg.getText());
} else {
logger.warn("Message of wrong type: " + inMessage.getClass().getName());
}
} catch (JMSException e) {
logger.error("MessageBean.onMessage: JMSException: " + e.toString());
} catch (Throwable te) {
logger.error("MessageBean.onMessage: Exception: " + te.toString());
}
}
}
Messaging3Mdb.java - The sun-ejb-jar.xml file: This file contains the mapping of the JMS resources.
<?xml version="1.0" encoding="UTF-8"?>
<sun-ejb-jar>
<enterprise-beans>
<name>Ejb3DD</name>
<ejb>
<ejb-name>Messaging3Mdb</ejb-name>
<jndi-name>jms/testQueue</jndi-name>
<mdb-connection-factory>
<jndi-name>jms/connectionFactory</jndi-name>
</mdb-connection-factory>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
META-INF/sun-ejb-jar.xml
The EJB deployment descriptor: <?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>Messaging3Mdb</ejb-name>
<ejb-class>jms.Messaging3Mdb</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<message-destination-type>javax.jms.Queue</message-destination-type>
</message-driven>
</enterprise-beans>
</ejb-jar>
META-INF/ejb-jar.xml
Environment: This example was implemented on Glassfish v1, Milestone 7
No comments:
Post a Comment