camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > ActiveMQ
Date Fri, 12 Mar 2010 14:31:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/ActiveMQ">ActiveMQ</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~cmoulliard">charles
Moulliard</a>
    </h4>
     Add section : Advisory Messages
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Add section : Advisory Messages<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <h2><a name="ActiveMQ-ActiveMQComponent"></a>ActiveMQ Component</h2>

<p>The ActiveMQ component allows messages to be sent to a <a href="http://java.sun.com/products/jms/"
rel="nofollow">JMS</a> Queue or Topic; or messages to be consumed from a JMS Queue
or Topic using <a href="http://activemq.apache.org/" title="The most popular and powerful
open source message broker" rel="nofollow">Apache ActiveMQ</a>. </p>

<p>This component is based on the <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS
Component</a> and uses Spring's JMS support for declarative transactions, using Spring's
<tt>JmsTemplate</tt> for sending and a <tt>MessageListenerContainer</tt>
for consuming. All the options from the <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
component also applies for this component.</p>

<p>To use this component make sure you have the <tt>activemq.jar</tt> or
<tt>activemq-core.jar</tt> on your classpath along with any Camel dependencies
such as <tt>camel-core.jar</tt>, <tt>camel-spring.jar</tt> and <tt>camel-jms.jar</tt>.</p>

<h3><a name="ActiveMQ-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
activemq:[queue:|topic:]destinationName
</pre>
</div></div>

<p>Where <b>destinationName</b> is an ActiveMQ queue or topic name. By default,
the <b>destinationName</b> is interpreted as a queue name. For example, to connect
to the queue, <tt>FOO.BAR</tt>, use:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
activemq:FOO.BAR
</pre>
</div></div>

<p>You can include the optional <tt>queue:</tt> prefix, if you prefer:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
activemq:queue:FOO.BAR
</pre>
</div></div>

<p>To connect to a topic, you must include the <tt>topic:</tt> prefix. For
example, to connect to the topic, <tt>Stocks.Prices</tt>, use:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
activemq:topic:Stocks.Prices
</pre>
</div></div>

<h3><a name="ActiveMQ-Options"></a>Options</h3>
<p>See Options on the <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
component as all these options also apply for this component.</p>

<h3><a name="ActiveMQ-ConfiguringtheConnectionFactory"></a>Configuring the
Connection Factory</h3>

<p>The following <a href="http://svn.apache.org/repos/asf/activemq/trunk/activemq-camel/src/test/java/org/apache/camel/component/ActiveMQRouteTest.java"
rel="nofollow">test case</a> shows how to add an ActiveMQComponent to the <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> using the <a href="http://camel.apache.org/maven/camel-activemq/apidocs/org/apache/camel/component/activemq/ActiveMQComponent.html#activeMQComponent(java.lang.String)"
rel="nofollow"><tt>activeMQComponent()</tt> method</a> while specifying
the <a href="http://activemq.apache.org/configuring-transports.html" rel="nofollow">brokerURL</a>
used to connect to ActiveMQ </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">camelContext.addComponent(<span class="code-quote">"activemq"</span>,
activeMQComponent(<span class="code-quote">"vm:<span class="code-comment">//localhost?broker.persistent=<span
class="code-keyword">false</span>"</span>));</span>
</pre>
</div></div>

<h3><a name="ActiveMQ-ConfiguringtheConnectionFactoryusingSpringXML"></a>Configuring
the Connection Factory using Spring XML</h3>

<p>You can configure the ActiveMQ broker URL on the ActiveMQComponent as follows</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"&gt;

  <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>


  <span class="code-tag">&lt;bean id=<span class="code-quote">"activemq"</span>
class=<span class="code-quote">"org.apache.activemq.camel.component.ActiveMQComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"brokerURL"</span>
value=<span class="code-quote">"tcp://somehost:61616"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<h3><a name="ActiveMQ-Usingconnectionpooling"></a>Using connection pooling</h3>
<p>When sending to an AcitveMQ broker using Camel its recommended to use a JMS connection
pooling such as Jencks. See more here <a href="http://fusesource.com/docs/esb/3.3/jms/ESBJMSConnectFactoryAMQ.html"
rel="nofollow">Jencks Connection Pooling</a></p>

<p>You can grab Jencks AMQ pool with maven:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;dependency&gt;</span>
      <span class="code-tag">&lt;groupId&gt;</span>org.jencks<span
class="code-tag">&lt;/groupId&gt;</span>
      <span class="code-tag">&lt;artifactId&gt;</span>jencks-amqpool<span
class="code-tag">&lt;/artifactId&gt;</span>
      <span class="code-tag">&lt;version&gt;</span>2.1<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<p>And then setup the <b>activemq</b> Camel component as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag"><span class="code-comment">&lt;!-- use jencks connection
pooling so its more effecient to send JMS messages --&gt;</span></span>
    &lt;amqpool:pool id=<span class="code-quote">"jmsConnectionFactory"</span>
<span class="code-keyword">xmlns:amqpool</span>=<span class="code-quote">"http://jencks.org/amqpool/2.0"</span>
                  brokerURL=<span class="code-quote">"tcp://localhost:61616"</span>
                  maxConnections=<span class="code-quote">"8"</span>/&gt;

    <span class="code-tag">&lt;bean id=<span class="code-quote">"jmsConfig"</span>
class=<span class="code-quote">"org.apache.camel.component.jms.JmsConfiguration"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"connectionFactory"</span>
ref=<span class="code-quote">"jmsConnectionFactory"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"transacted"</span>
value=<span class="code-quote">"false"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"concurrentConsumers"</span>
value=<span class="code-quote">"10"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag">&lt;bean id=<span class="code-quote">"activemq"</span>
class=<span class="code-quote">"org.apache.activemq.camel.component.ActiveMQComponent"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"configuration"</span>
ref=<span class="code-quote">"jmsConfig"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="ActiveMQ-InvokingMessageListenerPOJOsinaCamelroute"></a>Invoking
MessageListener POJOs in a Camel route</h3>

<p>The ActiveMQ component also provides a helper <a href="/confluence/display/CAMEL/Type+Converter"
title="Type Converter">Type Converter</a> from a JMS MessageListener to a <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a>. This means that the <a href="/confluence/display/CAMEL/Bean"
title="Bean">Bean</a> component is capable of invoking any JMS MessageListener bean
directly inside any route.</p>

<p>So for example you can create a MessageListener in JMS like this....</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class MyListener <span class="code-keyword">implements</span>
MessageListener {
   <span class="code-keyword">public</span> void onMessage(Message jmsMessage)
{
       <span class="code-comment">// ...
</span>   }
}
</pre>
</div></div>

<p>Then use it in your Camel route as follows</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//foo/bar"</span>).
</span>  bean(MyListener.class);
</pre>
</div></div>

<p>That is, you can reuse any of the Camel <a href="/confluence/display/CAMEL/Components"
title="Components">Components</a> and easily integrate them into your JMS <tt>MessageListener</tt>
POJO!</p>

<h3><a name="ActiveMQ-ConsumingAdvisoryMessages"></a>Consuming Advisory
Messages</h3>

<p>ActiveMQ can generates <a href="http://activemq.apache.org/advisory-message.html"
rel="nofollow">Advisory messages </a> which are put in topics that you can consume.
Such messages can help you to send alarm in case you detect slow consumers<br/>
or to build statistics (nber of messages/produced per day, ...). The following Spring DSL
example shows you how to read messages from Connection Topic :</p>

<p>The route start by reading the topic : ActiveMQ.Advisory.Connection. To watch another
topic, simply change the name according to the name provided in ActiveMQ Advisory Messages<br/>
documentation. The parameter mapJmsMessage=false allows to convert the org.apache.activemq.command.ActiveMqMessage
object from the jms queue.<br/>
Next, the body received is converted into a String for the example purpose and a carriage
return is added. Finally, the string is added to a file</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;route&gt;
	&lt;from uri=<span class="code-quote">"activemq:topic:ActiveMQ.Advisory.Connection?mapJmsMessage=<span
class="code-keyword">false</span>"</span> /&gt;
	&lt;convertBodyTo type=<span class="code-quote">"java.lang.<span class="code-object">String</span>"</span>/&gt;
	&lt;transform&gt;
	     &lt;simple&gt;${in.body}&amp;#13;&lt;/simple&gt;
	&lt;/transform&gt;
	&lt;to uri=<span class="code-quote">"file:<span class="code-comment">//data/activemq/?fileExist=Append&amp;amp;fileName=advisoryConnection-${date:now:yyyyMMdd}.txt"</span>
/&gt;
</span>&lt;/route&gt;
</pre>
</div></div>

<p>If you produce consume a message on a queue, you should see the following files under
data/activemq folder :</p>

<p>advisoryConnection-20100312.txt<br/>
advisoryProducer-20100312.txt</p>

<p>and containing string :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ActiveMQMessage {commandId = 0, responseRequired = <span class="code-keyword">false</span>,
messageId = ID:dell-charles-3258-1268399815140-1:0:0:0:221, originalDestination = <span
class="code-keyword">null</span>, 
originalTransactionId = <span class="code-keyword">null</span>, producerId = ID:dell-charles-3258-1268399815140-1:0:0:0,
destination = topic:<span class="code-comment">//ActiveMQ.Advisory.Connection, 
</span>transactionId = <span class="code-keyword">null</span>, expiration
= 0, timestamp = 0, arrival = 0, brokerInTime = 1268403383468, brokerOutTime = 1268403383468,
correlationId = <span class="code-keyword">null</span>,
 replyTo = <span class="code-keyword">null</span>, persistent = <span class="code-keyword">false</span>,
type = Advisory, priority = 0, groupID = <span class="code-keyword">null</span>,
groupSequence = 0, targetConsumerId = <span class="code-keyword">null</span>,
compressed = <span class="code-keyword">false</span>, 
 userID = <span class="code-keyword">null</span>, content = <span class="code-keyword">null</span>,
marshalledProperties = org.apache.activemq.util.ByteSequence@17e2705, dataStructure = 
 ConnectionInfo {commandId = 1, responseRequired = <span class="code-keyword">true</span>,
connectionId = ID:dell-charles-3258-1268399815140-2:50, clientId = 
 ID:dell-charles-3258-1268399815140-14:0, userName = , password = *****, brokerPath = <span
class="code-keyword">null</span>, brokerMasterConnector = <span class="code-keyword">false</span>,
manageable = <span class="code-keyword">true</span>,
 clientMaster = <span class="code-keyword">true</span>}, redeliveryCounter = 0,
size = 0, properties = {originBrokerName=master, originBrokerId=ID:dell-charles-3258-1268399815140-0:0,
  originBrokerURL=vm:<span class="code-comment">//master}, readOnlyProperties = <span
class="code-keyword">true</span>, readOnlyBody = <span class="code-keyword">true</span>,
droppable = <span class="code-keyword">false</span>}</span>
</pre>
</div></div>

<h3><a name="ActiveMQ-GettingComponentJAR"></a>Getting Component JAR</h3>

<p>You need these dependencies</p>
<ul class="alternate" type="square">
	<li><tt>camel-jms</tt></li>
	<li><tt>activemq-camel</tt></li>
</ul>


<h4><a name="ActiveMQ-cameljms"></a>camel-jms</h4>
<p>You <b>must</b> have the <tt>camel-jms</tt> as dependency
as <a href="/confluence/display/CAMEL/ActiveMQ" title="ActiveMQ">ActiveMQ</a>
is an extension to the <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
component.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-jms&lt;/artifactId&gt;
  &lt;version&gt;1.6.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>

<p>The ActiveMQ Camel component is released with the ActiveMQ project itself.<br/>
For Maven 2 users you simply just need to add the following dependency to your project.</p>

<h4><a name="ActiveMQ-ActiveMQ5.2orlater"></a>ActiveMQ 5.2 or later</h4>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
  &lt;artifactId&gt;activemq-camel&lt;/artifactId&gt;
  &lt;version&gt;5.2.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>

<h4><a name="ActiveMQ-ActiveMQ5.1.0"></a>ActiveMQ 5.1.0</h4>

<p>For 5.1.0 its in the activemq-core library</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
  &lt;artifactId&gt;activemq-core&lt;/artifactId&gt;
  &lt;version&gt;5.1.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>

<p>Alternatively you can download the component jar directly from the Maven repository:</p>

<ul>
	<li><a href="http://repo2.maven.org/maven2/org/apache/activemq/activemq-camel/5.2.0/activemq-camel-5.2.0.jar"
rel="nofollow">activemq-camel-5.2.0.jar</a></li>
	<li><a href="http://repo2.maven.org/maven2/org/apache/activemq/activemq-core/5.1.0/activemq-core-5.1.0.jar"
rel="nofollow">activemq-core-5.1.0.jar</a></li>
</ul>


<h4><a name="ActiveMQ-ActiveMQ4.x"></a>ActiveMQ 4.x</h4>

<p>For this version you must use the <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
component instead. Please be careful to use a pooling connection factory as described in the
<a href="http://activemq.apache.org/jmstemplate-gotchas.html" rel="nofollow">JmsTemplate
Gotchas</a></p>

<h3><a name="ActiveMQ-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/ActiveMQ">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=54646&revisedVersion=26&originalVersion=25">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/ActiveMQ?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message