camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Babak Vahdat (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > ActiveMQ
Date Mon, 04 Nov 2013 23:53:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/ActiveMQ">ActiveMQ</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bvahdat">Babak
Vahdat</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{info} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The {{PooledConnectionFactory}}
will then create a connection pool with up to 8 connections in use at the same time. Each
connection can be shared by many sessions. There is an option named <span class="diff-changed-words">{{max<span
class="diff-added-chars"style="background-color: #dfd;">imum</span>Active}}</span>
you can use to configure the maximum number of sessions per connection; the default value
is {{500}}. From *ActiveMQ 5.7* onwards the option has been renamed to better reflect its
purpose, being named as <span class="diff-changed-words">{{max<span class="diff-added-chars"style="background-color:
#dfd;">imum</span>ActiveSessionPerConnection}}.</span> Notice the {{concurrentConsumers}}
is set to a higher value than {{maxConnections}} is. This is okay, as each consumer is using
a session, and as a session can share the same connection, we are in the safe. In this example
we can have 8 * 500 = 4000 active sessions at the same time. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Invoking MessageListener
POJOs in a Camel route <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <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/"
class="external-link" 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" class="external-link" rel="nofollow">Apache ActiveMQ</a>.
</p>

<p>This component is based on <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>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Transacted and caching</b><br
/>See section <em>Transactions and Cache Levels</em> below on <a href="/confluence/display/CAMEL/JMS"
title="JMS">JMS</a> page if you are using transactions with <a href="/confluence/display/CAMEL/JMS"
title="JMS">JMS</a> as it can impact performance.</td></tr></table></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
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>This <a href="http://svn.apache.org/repos/asf/activemq/trunk/activemq-camel/src/test/java/org/apache/activemq/camel/component/ActiveMQRouteTest.java"
class="external-link" 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://activemq.apache.org/maven/5.5.0/activemq-camel/apidocs/org/apache/activemq/camel/component/ActiveMQComponent.html#activeMQComponent%28java.lang.String%29"
class="external-link" rel="nofollow"><tt>activeMQComponent()</tt> method</a>
while specifying the <a href="http://activemq.apache.org/configuring-transports.html" class="external-link"
rel="nofollow">brokerURL</a> used to connect to ActiveMQ. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
camelContext.addComponent("activemq", activeMQComponent("vm://localhost?broker.persistent=false"));
</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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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;

   &lt;camelContext xmlns="http://camel.apache.org/schema/spring"&gt;
   &lt;/camelContext&gt;

   &lt;bean id="activemq" 
      class="org.apache.activemq.camel.component.ActiveMQComponent"&gt;
      &lt;property name="brokerURL" value="tcp://somehost:61616"/&gt;
   &lt;/bean&gt;

&lt;/beans&gt;
</pre>
</div></div>

<h3><a name="ActiveMQ-Usingconnectionpooling"></a>Using connection pooling</h3>
<p>When sending to an ActiveMQ broker using Camel it's recommended to use a pooled connection
factory to efficiently handle  pooling of JMS connections, sessions and producers. This is
documented on the <a href="http://activemq.apache.org/spring-support.html" class="external-link"
rel="nofollow">ActiveMQ Spring Support </a> page.</p>

<p>You can grab ActiveMQ's <tt>org.apache.activemq.pool.PooledConnectionFactory</tt>
with Maven:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
   &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
   &lt;artifactId&gt;activemq-pool&lt;/artifactId&gt;
   &lt;version&gt;5.6.0&lt;/version&gt;
&lt;/dependency&gt;
</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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;bean id="jmsConnectionFactory" 
   class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
   &lt;property name="brokerURL" value="tcp://localhost:61616" /&gt;
&lt;/bean&gt;

&lt;bean id="pooledConnectionFactory" 
   class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop"&gt;
   &lt;property name="maxConnections" value="8" /&gt;
   &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt;
&lt;/bean&gt;

&lt;bean id="jmsConfig" 
   class="org.apache.camel.component.jms.JmsConfiguration"&gt;
   &lt;property name="connectionFactory" ref="pooledConnectionFactory"/&gt;
   &lt;property name="concurrentConsumers" value="10"/&gt;
&lt;/bean&gt;

&lt;bean id="activemq" 
    class="org.apache.activemq.camel.component.ActiveMQComponent"&gt;
    &lt;property name="configuration" ref="jmsConfig"/&gt;

    &lt;!-- if we are using transacted then enable CACHE_CONSUMER (if not using XA) to
run faster
         see more details at: http://camel.apache.org/jms
    &lt;property name="transacted" value="true"/&gt;
    &lt;property name="cacheLevelName" value="CACHE_CONSUMER" /&gt;
    --&gt;
&lt;/bean&gt;
</pre>
</div></div>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>Notice the <b>init</b>
and <b>destroy</b> methods on the pooled connection factory. This is important
to ensure the connection pool is properly started and shutdown.</td></tr></table></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Important
information about when using transactions</b><br />If you are using transactions
then see more details at <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>.
And remember to set <tt>cacheLevelName</tt> to <tt>CACHE_CONSUMER</tt>
if you are not using XA transactions. This can dramatically improve performance.</td></tr></table></div>

<p>The <tt>PooledConnectionFactory</tt> will then create a connection pool
with up to 8 connections in use at the same time. Each connection can be shared by many sessions.
There is an option named <tt>maximumActive</tt> you can use to configure the maximum
number of sessions per connection; the default value is <tt>500</tt>. From <b>ActiveMQ
5.7</b> onwards the option has been renamed to better reflect its purpose, being named
as <tt>maximumActiveSessionPerConnection</tt>. Notice the <tt>concurrentConsumers</tt>
is set to a higher value than <tt>maxConnections</tt> is. This is okay, as each
consumer is using a session, and as a session can share the same connection, we are in the
safe. In this example we can have 8 * 500 = 4000 active sessions at the same time.</p>

<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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
public class MyListener implements MessageListener {
   public void onMessage(Message jmsMessage) {
       // ...
   }
}
</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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("file://foo/bar").
  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-UsingActiveMQDestinationOptions"></a>Using ActiveMQ
Destination Options</h3>
<p><b>Available as of ActiveMQ 5.6</b></p>

<p>You can configure the <a href="http://activemq.apache.org/destination-options.html"
class="external-link" rel="nofollow">Destination Options</a> in the endpoint uri,
using the "destination." prefix. For example to mark a consumer as exclusive, and set its
prefetch size to 50, you can do as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;camelContext xmlns="http://camel.apache.org/schema/spring"&gt;
  &lt;route&gt;
    &lt;from uri="file://src/test/data?noop=true"/&gt;
    &lt;to uri="activemq:queue:foo"/&gt;
  &lt;/route&gt;
  &lt;route&gt;
    &lt;!-- use consumer.exclusive ActiveMQ destination option, notice we have to prefix
with destination. --&gt;
    &lt;from uri="activemq:foo?destination.consumer.exclusive=true&amp;amp;destination.consumer.prefetchSize=50"/&gt;
    &lt;to uri="mock:results"/&gt;
  &lt;/route&gt;
&lt;/camelContext&gt;
]]></script>
</div></div>


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

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

<p>The below route starts by reading the topic <em>ActiveMQ.Advisory.Connection</em>.
To watch another topic, simply change the name according to the name provided in ActiveMQ
Advisory Messages documentation. The parameter mapJmsMessage=false allows for converting the
org.apache.activemq.command.ActiveMqMessage object from the jms queue.  Next, the body received
is converted into a String for the purposes of this example 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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;route&gt;
   &lt;from uri="activemq:topic:ActiveMQ.Advisory.Connection?mapJmsMessage=false" /&gt;
   &lt;convertBodyTo type="java.lang.String"/&gt;
   &lt;transform&gt;
      &lt;simple&gt;${in.body}&amp;#13;&lt;/simple&gt;
   &lt;/transform&gt;
   &lt;to uri="file://data/activemq/?fileExist=Append&amp;amp;fileName=advisoryConnection-${date:now:yyyyMMdd}.txt"
/&gt;
&lt;/route&gt;
</pre>
</div></div>

<p>If you consume a message on a queue, you should see the following files under the
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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
ActiveMQMessage {commandId = 0, responseRequired = false, 
messageId = ID:dell-charles-3258-1268399815140
-1:0:0:0:221, originalDestination = null, originalTransactionId = null,
producerId = ID:dell-charles-3258-1268399815140-1:0:0:0, 
destination = topic://ActiveMQ.Advisory.Connection, transactionId = null, 
expiration = 0, timestamp = 0, arrival = 0, brokerInTime = 1268403383468, 
brokerOutTime = 1268403383468, correlationId = null, replyTo = null, 
persistent = false, type = Advisory, priority = 0, groupID = null, groupSequence = 0, 
targetConsumerId = null, compressed = false, userID = null, content = null, 
marshalledProperties = org.apache.activemq.util.ByteSequence@17e2705, 
dataStructure = ConnectionInfo {commandId = 1, responseRequired = true, 
connectionId = ID:dell-charles-3258-1268399815140-2:50, 
clientId = ID:dell-charles-3258-1268399815140-14:0, userName = , password = *****, 
brokerPath = null, brokerMasterConnector = false, manageable = true, 
clientMaster = true}, redeliveryCounter = 0, size = 0, properties = 
{originBrokerName=master, originBrokerId=ID:dell-charles-3258-1268399815140-0:0, 
originBrokerURL=vm://master}, readOnlyProperties = true, readOnlyBody = true, 
droppable = false}
</pre>
</div></div>

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

<p>You will need this dependency</p>
<ul class="alternate" type="square">
	<li><tt>activemq-camel</tt></li>
</ul>


<p><a href="/confluence/display/CAMEL/ActiveMQ" title="ActiveMQ">ActiveMQ</a>
is an extension of the <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
component released with the <a href="http://activemq.apache.org" class="external-link"
rel="nofollow">ActiveMQ project</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
  &lt;artifactId&gt;activemq-camel&lt;/artifactId&gt;
  &lt;version&gt;5.6.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>


<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;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/ActiveMQ">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=54646&revisedVersion=40&originalVersion=39">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message