JAVASpring

Apache Camel 和Active MQ的Tips

Apache Camel 可以很方便的和Active MQ进行通信,下面是一些Tips。

1.Active MQ 安装

Docker安装ActiveMQ

#下载 镜像
docker pull rmohr/activemq

#运行容器
docker run --name my-activemq -d -p 61616:61616 -p 8161:8161 rmohr/activemq

运行成功以后,访问 http://127.0.0.1:8161/admin(用户名和密码默认为admin),则启动成功。

2.Camel Maven工程 ActiveMQ Pom导入

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-activemq</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

但是运行的时候,有可能会报下面的错误。

java.lang.ClassNotFoundException: javax.jms.JMSContext

可以添加一下依赖解决,JMS 2.0 依赖如下

    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>javax.jms-api</artifactId>
        <version>2.0.1</version>
    </dependency>

3.Camel 工程中Spring XML Sample

<?xml version="1.0" encoding="UTF-8"?>
<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">
        
        <bean id="activemq" class="org.apache.camel.component.activemq.ActiveMQComponent">
 			<property name="connectionFactory">
    			<bean class="org.apache.activemq.ActiveMQConnectionFactory">
      				<property name="brokerURL" value="tcp://127.0.0.1:61616"/>
   		 		</bean>
  			</property>
		</bean>

    <!-- コンテキスト定義 -->
    <routeContext id="route-1" xmlns="http://camel.apache.org/schema/spring">
        <!-- メイン -->
        <route id="mainroute-1">
        	 <!-- 从testQueue受信 -->
             <from uri="activemq:testQueue"/>
             <log loggingLevel="DEBUG" message=">>>${body}"/>
             <!-- 往testQueue送信 -->
             <to uri="activemq:queue:testQueue"/>
        </route>
    </routeContext>
</beans>

有用的网站:

https://camel.apache.org/components/next/activemq-component.html

https://people.apache.org/~dkulp/camel/activemq.html