ActiveMQ

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
Line 7: Line 7:
 
== JCA ==
 
== JCA ==
  
ActiveMQ can be configured in Resin using ActiveMQ's JCA adapter.  The .rar file is in apache-activemq-5.0.0/lib/options/activemq-rar-5.0.0.rar
+
ActiveMQ can be configured in Resin using ActiveMQ's JCA adapter.   
  
In the resin.conf, you'll need to add a <resource-deploy> tag to tell Resin where to look for resources:
+
You can get the ActiveMQ RAR file at:
 +
 
 +
http://repo1.maven.org/maven2/org/apache/activemq/activemq-rar/5.7.0/activemq-rar-5.7.0.rar
 +
 
 +
In the resin.xml, you'll need to add a <resource-deploy> tag to tell Resin where to look for resources:
 
<code><pre>
 
<code><pre>
 
   <resin xmlns="http://caucho.com/ns/resin">
 
   <resin xmlns="http://caucho.com/ns/resin">
 
     <cluster id="">
 
     <cluster id="">
 
         <host id="">
 
         <host id="">
           <resource-deploy path="deploy"/>
+
           <resource-deploy path="/somedir/that/has/jca-rar/files/deploy"/>
 
             ...
 
             ...
 
         </host>
 
         </host>
Line 21: Line 25:
 
</pre></code>
 
</pre></code>
  
Then, in your resin-web.xml, you'll need to configure the connector.  It will look like:
+
Then, in your resin-web.xml (under WEB-INF of your webapp), you'll need to configure the connector.  It will look like:
 
<code><pre>
 
<code><pre>
  <web-app xmlns="http://caucho.com/ns/resin">
+
<?xml version="1.0" encoding="UTF-8" ?>
   
+
<web-app xmlns="http://caucho.com/ns/resin">
    <resource-adapter uri="activemq:">
+
        <init server-url="vm://localhost"/>
+
    </resource-adapter>
+
  
    <connection-factory uri="activemq:" name="activemq"/>
+
<resource-adapter class="org.apache.activemq.ra.ActiveMQResourceAdapter">
+
<init server-url="vm://localhost" />
    <jms-queue uri="activemq:" name="test">
+
</resource-adapter>
        <init physical-name="queue.test"/>
+
 
    </jms-queue>
+
<connection-factory class="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" name="activemq" />
  </web-app>
+
<jms-queue class="org.apache.activemq.command.ActiveMQQueue" name="test">
</pre></code>
+
<init physical-name="queue.test" />
 +
</jms-queue>
 +
</web-app>
 +
</pre></code>
  
 
* The <resource-adapter> configures a JCA resource-adapter.   
 
* The <resource-adapter> configures a JCA resource-adapter.   
* The uri="activemq:" is an alias for class="org.apache.activemq.ra.ActiveMQResourceAdapter" (contained in resin-support.jar) 
+
* The <init> block configures the resource adapter parameters. Refer to ActiveMQ or the ra.xml file of the expanded rar file to see what parameters you can configure.
* The <init> block configures the resource adapter parameters.
+
 
* The <connection-factory> configures the outbound factory
 
* The <connection-factory> configures the outbound factory
* The <bean> configures the Queue (i.e. the adminobject)
+
* The <jms-queue> configures the Queue (i.e. the adminobject)
  
 
<code><pre>
 
<code><pre>
package demo;
+
package com.example;
  
import javax.jms.*;
+
import java.io.IOException;
import javax.servlet.*;
+
import javax.webbeans.*;
+
  
public class DemoServlet extends GenericServlet {
+
import javax.inject.Inject;
  @In ConnectionFactory _factory;
+
import javax.inject.Named;
  @Named("test") Queue _queue;
+
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);
 +
}
 +
}
 +
}
  
  public void service(...)
 
  {
 
    ...
 
    Connection conn = _factory.createConnection();
 
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
    session.send(_queue, session.createTextMessage("test"));
 
    ...
 
  }
 
 
}
 
}
 +
 
</pre></code>
 
</pre></code>

Revision as of 00:00, 6 February 2013

40px-Edit-clear.png This article requires cleanup and may refer to a legacy version of Resin.

Please visit http://www.caucho.com/documentation/ for the most up-to-date documentation.

See Also

General documentation is available about Resin Messaging.

JCA

ActiveMQ can be configured in Resin using ActiveMQ's JCA adapter.

You can get the ActiveMQ RAR file at:

http://repo1.maven.org/maven2/org/apache/activemq/activemq-rar/5.7.0/activemq-rar-5.7.0.rar

In the resin.xml, you'll need to add a <resource-deploy> tag to tell Resin where to look for resources:

  <resin xmlns="http://caucho.com/ns/resin">
    <cluster id="">
        <host id="">
           <resource-deploy path="/somedir/that/has/jca-rar/files/deploy"/>
            ...
         </host>
     </cluster>
  </resin>

Then, in your resin-web.xml (under WEB-INF of your webapp), you'll need to configure the connector. It will look like:

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://caucho.com/ns/resin">

	<resource-adapter class="org.apache.activemq.ra.ActiveMQResourceAdapter">
		<init server-url="vm://localhost" />
	</resource-adapter>

	<connection-factory class="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" name="activemq" />
	<jms-queue class="org.apache.activemq.command.ActiveMQQueue" name="test">
		<init physical-name="queue.test" />
	</jms-queue>
</web-app>
 
  • The <resource-adapter> configures a JCA resource-adapter.
  • The <init> block configures the resource adapter parameters. Refer to ActiveMQ or the ra.xml file of the expanded rar file to see what parameters you can configure.
  • The <connection-factory> configures the outbound factory
  • The <jms-queue> configures the Queue (i.e. the adminobject)
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