blob: 42e61d5426c3906c4bf6fb86c6900b95ab477387 [file] [log] [blame]
<div class="wiki-content maincontent"><h2>Broker Camel Component</h2><p><strong>Available as of ActiveMQ 5.9</strong></p><p>Embedding Apache Camel inside the ActiveMQ broker provides great flexibility for extending the message broker with the integration power of Camel. Apache Camel routes also benefit in that you can avoid the serialization and network costs of connecting to ActiveMQ remotely - if you use the <a shape="rect" href="http://camel.apache.org/activemq.html">activemq component</a>.</p><p>If however, you want to change the behavior of messages flowing through the ActiveMQ message broker itself you will be limited to the shipped set of ActiveMQ broker <a shape="rect" href="http://activemq.apache.org/interceptors.html">Interceptors</a> - or develop your own <a shape="rect" href="http://activemq.apache.org/developing-plugins.html">Broker plugin</a> and then introduce that as a jar on to the class path for the ActiveMQ broker. The&#160;<strong><code>broker</code></strong> Camel component makes this even easier. It intercepts messages as they move through the broker itself, allowing them to be modified and manipulated before they are persisted to the message store or delivered to end consumers.</p><p>For example <a shape="rect" href="http://activemq.apache.org/how-should-i-package-applications-using-camel-and-activemq.html">by defining a CamelContext to run inside the broker's JVM </a>the&#160;<strong><code>broker</code></strong> component can intercept all messages published to a Topic, say, and publish them to a Queue instead, changing their priority along the way:</p><structured-macro ac:macro-id="46808b45-4f42-4c77-8d11-b064dc9fa99c" ac:name="code" ac:schema-version="1"><parameter ac:name="language">xml</parameter><plain-text-body>&lt;route id="setPriority"&gt;
&lt;from uri="broker:topic:test.broker.&gt;"/&gt;
&lt;setHeader headerName="JMSPriority"&gt;
&lt;constant&gt;9&lt;/constant&gt;
&lt;/setHeader&gt;
&lt;to uri="broker:queue:test.broker.component.queue"/&gt;
&lt;/route&gt;
</plain-text-body></structured-macro><p>Notes:</p><ul><li>A broker component only adds an intercept into the broker if its started - so the broker component will not add any overhead to the running broker until its used - and then the overhead will be trivial.</li><li>Messages are intercepted by the broker component when they have been received by the broker - but before they are processed (persisted or routed to a destination).</li><li>The&#160;<strong><code>IN</code></strong> message on the Exchange is a <strong><code>CamelMessage</code></strong>, but also a JMS Message (messages routed through ActiveMQ from STOMP/MQTT/AMQP etc. are always translated into JMS messages).</li><li>W<a shape="rect" href="http://activemq.apache.org/wildcards.html">ildcards</a> can be used on a destination to intercept messages from destinations matching the wildcard.</li><li>After the intercept, you have to explicitly send the message back to the broker component - this allows you to either drop select messages (by not sending) - or, like in the above case - re-route the message to a different destination.<br clear="none"><br clear="none"></li></ul><structured-macro ac:macro-id="7fd157e0-4ac6-4027-9539-96ed3ed4828e" ac:name="warning" ac:schema-version="1"><rich-text-body><p>There is one deliberate caveat though, only intercepted messages can be sent to a&#160;<strong><code>broker</code></strong> component. For example, routing a Camel message from another Component e.g. <strong><code>file</code></strong>, will result in an error.</p></rich-text-body></structured-macro><p>Extra classes that have been added to the&#160;<strong><code>activemq-broker</code></strong> package to support the&#160;<strong><code>broker</code></strong> component. They allow the state of the running broker to be interrogated without using JMX. These classes are:</p><ul><li><a shape="rect" href="http://activemq.apache.org/maven/5.9.0/apidocs/org/apache/activemq/broker/view/MessageBrokerView.html">org.apache.activemq.broker.view.MessageBrokerView</a> - provides methods to retrieve statistics on a the broker</li><li>From the&#160;<strong><code>org.apache.activemq.broker.view.MessageBrokerView</code></strong> - you can retrieve a <a shape="rect" href="http://activemq.apache.org/maven/5.9.0/apidocs/org/apache/activemq/broker/view/BrokerDestinationView.html">org.apache.activemq.broker.view.BrokerDestinationView</a> for a particular destination.</li></ul><h3>Example</h3><p>How to route messages when a destination's queue depth has reached a certain limit:</p><structured-macro ac:macro-id="15dd6e15-edd6-4a5e-9a16-c0faf86e2c2a" ac:name="code" ac:schema-version="1"><parameter ac:name="language">xml</parameter><plain-text-body>&lt;camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/spring"&gt;
&lt;route id="routeAboveQueueLimitTest"&gt;
&lt;from uri="broker:queue:test.broker.queue"/&gt;
&lt;choice&gt;
&lt;when&gt;
&lt;spel&gt;#{@destinationView.queueSize &gt;= 100}&lt;/spel&gt;
&lt;to uri="broker:queue:test.broker.processLater"/&gt;
&lt;/when&gt;
&lt;otherwise&gt;
&lt;to uri="broker:queue:test.broker.queue"/&gt;
&lt;/otherwise&gt;
&lt;/choice&gt;
&lt;/route&gt;
&lt;/camelContext&gt;
&lt;bean id="brokerView" class="org.apache.activemq.broker.view.MessageBrokerView"&gt;
&lt;constructor-arg value="testBroker"/&gt;
&lt;/bean&gt;
&lt;bean id="destinationView" factory-bean="brokerView" factory-method="getDestinationView"&gt;
&lt;constructor-arg value="test.broker.component.route"/&gt;
&lt;/bean&gt;
</plain-text-body></structured-macro><p>This is using the Camel Message Router pattern. Note the use of the Spring expression language&#160;<strong><code>spel</code></strong> in the&#160;<strong><code>when</code></strong> clause.</p><p>&#160;</p></div>