Resin ActiveMQ How to

From Resin 4.0 Wiki

Revision as of 17:00, 6 February 2013 by Rick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Resin, ActiveMQ and CDI

ActiveMQ can be used from Resin using Resin's CDI support.


Then, in your resin-web.xml (under WEB-INF of your webapp), you'll need to configure the ActiveMA as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://caucho.com/ns/resin" xmlns:activemq="urn:java:org.apache.activemq"
	xmlns:queue="urn:java:org.apache.activemq.command"
	xmlns:ee="urn:java:ee">
	
	<activemq:ActiveMQConnectionFactory
		broker-url="vm://localhost">
		<ee:Named>activemq</ee:Named>
		<ee:ApplicationScoped />
	</activemq:ActiveMQConnectionFactory>
	<queue:ActiveMQQueue physical-name="queue.test">
		<ee:Named>test</ee:Named>
	</queue:ActiveMQQueue>
</web-app>
 

The Java code looks as follows:

package com.example;

import java.io.IOException;

import javax.inject.Inject;
import javax.inject.Named;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Sender
 */
@WebServlet("/Sender")
public class Sender extends HttpServlet {

	@Inject @Named("activemq")
	QueueConnectionFactory _factory;
	@Inject @Named("test")
	Queue _queue;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		QueueConnection conn = null;
		QueueSession session = null;
		QueueSender sender = null;

		try {

			 conn = _factory.createQueueConnection();
			 session = conn.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);
			sender = session.createSender(_queue);
			sender.send(_queue, session.createTextMessage("test"));
			response.getWriter().println("SENT");

		} catch (JMSException e) {
			throw new ServletException(e);
		} finally {
			try {
				conn.close();
				session.close();
				sender.close();
			} catch (JMSException e) {
				throw new ServletException(e);
			}
		}
	}

}

Personal tools
TOOLBOX
LANGUAGES