JBoss Fuse - AMQ Avec Camel

Dans ce chapitre, nous allons apprendre les bases du fonctionnement d'ActiveMQ avec Camel.

Configuration du composant ActiveMQ

Avant de pouvoir utiliser la file d'attente ou la rubrique ActiveMQ dans notre code, nous devons configurer ActiveMQComponent. La configuration minimale d'ActiveMQComponent peut être effectuée comme indiqué dans le programme suivant -

<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 - Spécifie l'hôte et le port pour AMQ Broker.

  • username - Spécifie le nom d'utilisateur à utiliser pour se connecter à AMQ Broker.

  • password - spécifie le mot de passe pour se connecter à AMQ Broker.

Connexion à la file d'attente

Maintenant que nous avons configuré ActiveMQComponent, nous pouvons l'utiliser dans notre CamelContext comme point de terminaison.

Nous utiliserons le point de terminaison AMQ dans le format suivant -

Activemq:[queue|topic]:[queueName|topicName]

Écrire des messages sur 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">

Après avoir déployé ce bundle dans le conteneur Fuse, vous devriez pouvoir voir les messages publiés sur AMQ qui ont été placés sous forme de fichiers dans D:/src/data.

Input

D: /src/data/input.txt

Test me

Output

Lecture depuis 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

Après avoir déployé ce bundle, vous devriez voir un fichier généré dans D: / src et les messages sont consommés. Le consommateur doit également être affiché pour cette file d'attente.

Output

D: / src

Test me

Ecrire au sujet

<?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>

Lecture à partir du sujet

<?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>