JBoss Fuse - AMQ Dengan Unta
Dalam bab ini, kita akan mempelajari dasar-dasar bagaimana ActiveMQ bekerja dengan Camel.
Konfigurasi ke Komponen ActiveMQ
Sebelum kita dapat menggunakan antrian atau topik ActiveMQ dalam kode kita, kita harus mengkonfigurasi ActiveMQComponent. Konfigurasi minimal ActiveMQComponent dapat dilakukan seperti yang ditunjukkan pada program berikut -
<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 - Menentukan host dan port untuk AMQ Broker.
username - Menentukan nama pengguna yang akan digunakan untuk menghubungkan ke AMQ Broker.
password - menentukan kata sandi untuk menghubungkan ke AMQ Broker.
Menghubungkan ke Antrian
Sekarang setelah kita mengkonfigurasi ActiveMQComponent, kita dapat menggunakannya di CamelContext sebagai titik akhir.
Kami akan menggunakan titik akhir AMQ dalam format berikut -
Activemq:[queue|topic]:[queueName|topicName]
Menulis Pesan ke 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">
Setelah menerapkan bundel ini dalam wadah Fuse, Anda seharusnya dapat melihat pesan yang diposting ke AMQ yang ditempatkan sebagai file di D:/src/data.
Input
D: /src/data/input.txt
Test me
Output
Membaca dari 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
Setelah menerapkan bundel ini, Anda akan melihat file dibuat di D: / src dan pesan dikonsumsi. Juga Konsumen harus ditunjukkan untuk Antrian itu.
Output
D: / src
Test me
Menulis ke Topik
<?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>
Membaca dari Topik
<?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>