jms listener with ejb message bean
Introduces receiving a message using an EJB message bean.
Messaging decouples a sending task from a receiving/processing task, allowing for batch processing or load balancing of tasks to another machine. The original task can respond immediately to the user without waiting for the task complete. The servlet creates a message and sends it to a queue. The servlet immediately completes and when the batch process is ready, it processes the message. Messaging is therefore comprised of three main components:
In this example, the Producer is a Servlet which sends a simple message.
To simplify the example, we'll use the The servlet uses CDI
dependency injection to get the import javax.inject.Inject; import java.util.concurrent.BlockingQueue; public class MessageServlet extends GenericServlet { @Inject BlockingQueue _sender; ... String message = "sample message"; _sender.put(message); } In this configuration, the The The Queue delivers messages to the Consumer at the consumer's rate. When the Consumer finishes processing a message the Queue will deliver the next available message. The Consumer implements In this example, the Consumer just logs the message, but more realistic applications might use Java Persistence to store results in a database. The listener instance has full access to Resin-IoC capabilities, including dependency injection, standard aspects like @TransactionAttribute, interceptors, and WebBeans event processing. package example; import java.util.logging.Logger; import java.util.logging.Level; import javax.jms.Message; import javax.jms.TextMessage; import javax.jms.MessageListener; public class MyListener implements MessageListener { private static final Logger log = Logger.getLogger(MyListener.class.getName()); public void onMessage(Message message) { try { ObjectMessage objMessage = (ObjectMessage) message; log.info("received: " + objMessage.getValue()); _lastMessage = textMessage.getValue(); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } } Since Resin is an inversion of control container (IoC), it can configure the JMS resources in the standard Resin configuration file. The Resin IoC documentation gives more information. The configuration is responsible for three things:
The Because the listener and sender need a reference to the queue, the jms-queue stores it in the "my_queue" variable. <web-app xmlns="http://caucho.com/ns/resin" xmlns:ee="urn:java:ee" xmlns:resin="urn:java:com.caucho.resin"> <resin:FileQueue ee:Named="my_queue"> <path>WEB-INF/db</path> </resin:FileQueue> </web-app> JMS also needs a configured ConnectionFactory, so the sender and listener can create JMS connections. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:JmsConnectionFactory/> </web-app> The MessageListener is configured as an EJB message bean. Resin's EJB support instantiates the listeners and receives messages from the queue. <web-app xmlns="http://caucho.com/ns/resin"> <resin:FileQueue ee:Named="my_queue"> <path>WEB-INF/db</path> </resin:FileQueue> <ejb-message-bean class="example.MyListener"> <destination>${my_queue}</destination> </ejb-message-bean> </web-app>
|