Fusibile JBoss - AMQ con cammello
In questo capitolo impareremo le basi di come ActiveMQ funziona con Camel.
Configurazione in ActiveMQ Component
Prima di poter utilizzare la coda o l'argomento ActiveMQ nel nostro codice, dobbiamo configurare ActiveMQComponent. La configurazione minima di ActiveMQComponent può essere eseguita come mostrato nel seguente programma:
<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 - Specifica l'host e la porta per AMQ Broker.
username - Specifica il nome utente da utilizzare per la connessione a AMQ Broker.
password - specifica la password per la connessione a AMQ Broker.
Connessione alla coda
Ora che abbiamo configurato ActiveMQComponent, possiamo usarlo nel nostro CamelContext come endpoint.
Useremo l'endpoint AMQ nel seguente formato:
Activemq:[queue|topic]:[queueName|topicName]
Scrittura di messaggi ad 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">
Dopo aver distribuito questo bundle nel contenitore Fuse, dovresti essere in grado di vedere i messaggi pubblicati su AMQ che sono stati inseriti come file in D:/src/data.
Input
D: /src/data/input.txt
Test me
Output
Lettura da 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>
Input
Dopo aver distribuito questo pacchetto, dovresti vedere un file generato in D: / src e i messaggi vengono consumati. Anche il consumatore dovrebbe essere mostrato per quella coda.
Output
D: / src
Test me
Scrivendo all'argomento
<?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>
Lettura dall'argomento
<?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>
Input
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>
Output
D: / src /
<order>
<data>
<value>value1</value>
</data>
</order>
<order>
<data>
<value>value2</value>
</data>
</order>
<order>
<data>
<value>value3</value>
</data>
</order>