В этой главе мы изучим основы того, как ActiveMQ работает с Camel.
Настройка для компонента ActiveMQ
Прежде чем мы сможем использовать ActiveMQ-очередь или тему в нашем коде, мы должны настроить ActiveMQComponent. Минимальная конфигурация ActiveMQComponent может быть выполнена, как показано в следующей программе —
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent"> <property name = "brokerURL" value = "tcp://localhost:61616"/> <property name = "userName" value = "admin"/> <property name = "password" value = "admin"/> </bean>
-
brokerURL — указывает хост и порт для AMQ Broker.
-
username — указывает имя пользователя для подключения к AMQ Broker.
-
пароль — указывает пароль для подключения к AMQ Broker.
brokerURL — указывает хост и порт для AMQ Broker.
username — указывает имя пользователя для подключения к AMQ Broker.
пароль — указывает пароль для подключения к AMQ Broker.
Подключение к очереди
Теперь, когда мы настроили ActiveMQComponent, мы можем использовать его в нашем CamelContext в качестве конечной точки.
Мы будем использовать конечную точку AMQ в следующем формате —
Activemq:[queue|topic]:[queueName|topicName]
Написание сообщений в AMQ
<?xml version = "1.0" encoding="UTF-8"?> <!-- Configures the Camel Context--> <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.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
После развертывания этого пакета в контейнере Fuse вы сможете увидеть сообщения, опубликованные в AMQ, которые были размещены в виде файлов в D: / src / data .
вход
D: /src/data/input.txt
Test me
Выход
Чтение из AMQ
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<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.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files
(leaving them in place - see the 'noop' flag)
then performs content based routing on the message using XPath -->
<route>
<from uri = "activemq:queue:TestQ"/>
<to uri = "file:///d:/src"/>
</route>
</camelContext>
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value = "tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
</beans>
вход
После развертывания этого пакета вы должны увидеть файл, сгенерированный в D: / src, и сообщения будут использованы. Также Потребитель должен быть показан для этой очереди.
Выход
D: / SRC
Test me
Запись в тему
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<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.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files
(leaving them in place - see the 'noop' flag)
then performs content based routing on the message using XPath -->
<route>
<from uri = "file:///d:/src"/>
<to uri = "activemq:topic:TestTopic” />
</route>
</camelContext>
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value = "tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
</beans>
Чтение из темы
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<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.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files
(leaving them in place - see the 'noop' flag)
then performs content based routing on the message using XPath -->
<route>
<from uri = "activemq:topic:TestTopic"/>
<to uri = "file:///d:/src2"/>
</route>
</camelContext>
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value="tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
</beans>
вход
D: /src/file1.xml
<order>
<data>
<value>value1</value>
</data>
</order>
<order>
<data>
<value>value2</value>
</data>
</order>
<order>
<data>
<value>value3</value>
</data>
</order>
Выход
D: / SRC /

