| JBoss MDB 3.0 |
|
|
|
| Written by Mark S. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thursday, 25 September 2008 14:00 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Message-driven beans can receive JMS messages and process them using the same robust component-based infrastructure that session and entity beans enjoy. While a message-driven bean is responsible for processing messages, its container takes care of automatically managing the component’s entire environment including transactions, security, resources, concurrency, message acknowledgment, etc. One of the most important aspects of message-driven beans is that they can consume and process messages concurrently. This capability provides a significant advantage over traditional JMS clients, which must be custom-built to manage resources, transactions, and security in a multi-threaded environment. Before the release 3.0 of EJB also MDB needed a configuration file describing its properties, now let's see an example of MDB using annotations. it turns out that writing an MDB is just a matter of a few minutes:
package com.sample;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
@MessageDriven(name = "MessageMDBSample", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MyQueue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class DummyMDBean implements MessageListener {
@Resource
private MessageDrivenContext context;
@Resource(name = "OracleDS")
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@PostConstruct
public void initialize() {
try {
connection = dataSource.getConnection();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
@PreDestroy
public void cleanup() {
try {
connection.close();
connection = null;
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
public void onMessage(Message message) {
try {
Statement statement = connection.createStatement();
statement.execute("INSERT INTO ...........");
} catch (JMSException jmse) {
jmse.printStackTrace();
context.setRollBackOnly();
} catch (SQLException sqle) {
sqle.printStackTrace();
context.setRollBackOnly();
}
}
}
MDB 3.0 Annotationsthe @MessageDriven annotation identifies this object as an MDB and specifies the MDB configuration, the activationConfig property of the message driven annotation lets you provide messaging-system specific configuration information through an array of ActivationConfigProperty instances. @MessageDriven(name = "MessageMDBSample", activationConfig = { In our case we define the destionationType of our MDB as Queue listening on the "queue/MyQueue". Last property is the acknowledge Mode which is set to Auto-acknowledge. Other EJB 3.0 AnnotationsBesides MDB annotations here you can see an interesting example of Resource injection: when the MDB is initialized a DataSource is injected using the following setter method: @Resource(name = "OracleDS") The connection will be then used to insert data in the onMessage method (here we don't use a DAO just to keep the example very simple and short). We need as well to inject a MessageDrivenContext context in order to rollback the transaction in case of error. That's all. With this simple class you can manage asynchronously your transactions, without even the need for a configuration file. Packaging your MDB:packaging your mdb is simply a matter of putting the .class file in a jar archive, that's all ! $ jar cvf mdbExample.jar com An MDB client:We add here a simple servlet client which you might use to test your MDB: pack your servlet in a warfile, your ejb in a jar file and put everything in an ear archive.
package com.sample.servlet;
import java.io.*;
import java.util.*;
import javax.jms.*;
import javax.jms.Queue;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class testJMS extends HttpServlet {
Context context = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
boolean esito = true;
public void init() {
try {
context = new InitialContext();
getQueueSender();
context.close();
} catch(Exception exc) {
System.out.println(exc.toString());
esito = false;
}
}
public void getQueueSender() throws JMSException,NamingException {
QueueConnectionFactory queueFactory = (QueueConnectionFactory)context.lookup("ConnectionFactory");
queueConnection = queueFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,
javax.jms.Session.AUTO_ACKNOWLEDGE);
queue = (Queue)context.lookup("queue/MyQueue");
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
}
public void sendMsg(String testo) throws JMSException {
message.setText(testo);
queueSender.send(queue, message);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
String whereToForward = null;
String testo = request.getParameter("text");
if (text==null) {
text="dummy message";
}
if(esito) {
try {
sendMsg(text);
whereToForward = "success";
} catch (JMSException jme) {
System.out.println(jme.toString());
whereToForward = "failure";
}
} else {
whereToForward = "failure";
}
System.out.println("Done!");
}
public void destroy() {
try {
if(queueSender != null)queueSender.close();
} catch (JMSException jme) {
System.out.println(jme.toString());
} finally {
queueSender = null;
}
try {
if(queueSession != null)queueSession.close();
} catch (JMSException jme) {
System.out.println(jme.toString());
} finally {
queueSession = null;
}
try {
if(queueConnection != null)queueConnection.close();
} catch (JMSException jme) {
System.out.println(jme.toString());
} finally {
queueConnection = null;
}
}
}
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Last Updated ( Monday, 06 October 2008 19:51 ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||



