Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 2. ActiveMQ
ActiveMQ Component 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The ActiveMQ component allows messages to be sent to a JMS Queue or Topic; or messages to be consumed from a JMS Queue or Topic using Apache ActiveMQ.
This component is based on the JMS Component and uses Spring's JMS support for declarative transactions, using Spring's
JmsTemplate
for sending and a MessageListenerContainer
for consuming. All the options from the JMS component also apply for this component.
To use this component, make sure you have the
activemq.jar
or activemq-core.jar
on your classpath along with any Apache Camel dependencies such as camel-core.jar
, camel-spring.jar
and camel-jms.jar
.
Transacted and caching
See section Transactions and Cache Levels below on JMS page if you are using transactions with JMS as it can impact performance.
URI format 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
activemq:[queue:|topic:]destinationName
activemq:[queue:|topic:]destinationName
Where destinationName is an ActiveMQ queue or topic name. By default, the destinationName is interpreted as a queue name. For example, to connect to the queue,
FOO.BAR
, use:
activemq:FOO.BAR
activemq:FOO.BAR
You can include the optional
queue:
prefix, if you prefer:
activemq:queue:FOO.BAR
activemq:queue:FOO.BAR
To connect to a topic, you must include the
topic:
prefix. For example, to connect to the topic, Stocks.Prices
, use:
activemq:topic:Stocks.Prices
activemq:topic:Stocks.Prices
Options 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
See Options on the JMS component as all these options also apply for this component.
Configuring the Connection Factory 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The following test case shows how to add an ActiveMQComponent to the CamelContext using the
activeMQComponent()
method while specifying the brokerURL used to connect to ActiveMQ.
camelContext.addComponent("activemq", activeMQComponent("vm://localhost?broker.persistent=false"));
camelContext.addComponent("activemq", activeMQComponent("vm://localhost?broker.persistent=false"));
Configuring the Connection Factory using Spring XML 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can configure the ActiveMQ broker URL on the ActiveMQComponent as follows
Using connection pooling 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
When sending to an ActiveMQ broker using Camel it's recommended to use a pooled connection factory to handle efficient pooling of JMS connections, sessions and producers. This is documented in the page ActiveMQ Spring Support.
You can grab Jencks AMQ pool with Maven:
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.3.2</version> </dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.3.2</version>
</dependency>
And then setup the activemq component as follows:
Note
Notice the init and destroy methods on the pooled connection factory. This is important to ensure the connection pool is properly started and shutdown.
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 maxActive
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 maxActiveSessionPerConnection
. 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.
Invoking MessageListener POJOs in a route 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The ActiveMQ component also provides a helper Type Converter from a JMS MessageListener to a Processor. This means that the Bean component is capable of invoking any JMS MessageListener bean directly inside any route.
So for example you can create a MessageListener in JMS as follows:
public class MyListener implements MessageListener { public void onMessage(Message jmsMessage) { // ... } }
public class MyListener implements MessageListener {
public void onMessage(Message jmsMessage) {
// ...
}
}
Then use it in your route as follows
from("file://foo/bar"). bean(MyListener.class);
from("file://foo/bar").
bean(MyListener.class);
That is, you can reuse any of the Apache Camel Components and easily integrate them into your JMS
MessageListener
POJO\!
Using ActiveMQ Destination Options 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
Available as of ActiveMQ 5.6
You can configure the Destination Options 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:
Consuming Advisory Messages 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
ActiveMQ can generate Advisory messages 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.
If you consume a message on a queue, you should see the following files under data/activemq folder :
advisoryConnection-20100312.txt advisoryProducer-20100312.txt
and containing string:
Getting Component JAR 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You need this dependency:
activemq-camel
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-camel</artifactId> <version>5.6.0</version> </dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.6.0</version>
</dependency>