ActiveMQ+Camel+Spring+jms Demo(一)

俗话说的好:温故而知新。这就是我发布到这里的原因。

ActiveMQ+Camel+Spring 简单整合

官网我就不列了,我做的这个demo只是跑通了而已,还有很多需要完善的地方。

待续……

欢迎拍砖


简单描述下我的这个demo:web界面有2个请求,一个是向mq发送数字消息,一个是文本消息。
并将响应的结果回显到web。

不完美的地方:没有用2台电脑来测试mq请求和应答。测了回补上,待续……


pom文件就不贴了。


就贴比较关键的配置,其实这些都是从官方example中抄过来的。

camel-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:broker="http://activemq.apache.org/schema/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<context:component-scan base-package="com.activeMqCamelSpring.demo"/>

<context:property-placeholder location="classpath:camel.properties"
ignore-resource-not-found="true"/>

<camel:camelContext id="camel-server">
<camel:package>com.activeMqCamelSpring.demo.server</camel:package>
<camel:jmxAgent id="agent" createConnector="true"/>
</camel:camelContext>
<camel:camelContext id="camel-client">
<camel:template id="camelTemplate"/>
</camel:camelContext>

<broker:broker useJmx="true" persistent="false" brokerName="myBroker">
<broker:transportConnectors>
<broker:transportConnector name="vm" uri="vm://myBroker"/>
<broker:transportConnector name="tcp" uri="tcp://localhost:${tcp.port}"/>
</broker:transportConnectors>
</broker:broker>

<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="vm://myBroker"/>
</bean>

</beans>


camel.properties 就一句:
[code]
tcp.port=61610
[/code]

web.xml中

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:camel-server.xml,
classpath:spring-base.xml
</param-value>
</context-param>


ServerRoutes中也很简单

public class ServerRoutes extends RouteBuilder {

@Override
public void configure() throws Exception {
from("jms:queue:numbers").to("multiplier");
from("jms:queue:strings").to("textplier");
}

}


multiplier 类

@Service(value = "multiplier")
public class Treble {

public int multiply(final int originalNumber) {
return originalNumber * 3;
}

}

textplier类

@Service(value = "textplier")
public class TextPlier {

public String say(final String text){
return "activeMQ with camel for :"+text;
}

}


还有一个service


@Service
public class CamleService {

@Autowired
AbstractApplicationContext context;

public int sendHelloWorldNumToCamel(){
ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
int response = (Integer)camelTemplate.sendBody("jms:queue:numbers", ExchangePattern.InOut, 22);
return response;
}

public String sendHelloWorldTextToCamel(String text){
ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
String response = (String)camelTemplate.sendBody("jms:queue:strings", ExchangePattern.InOut, text);
return response;
}


}


运行效果截图

[img]http://dl.iteye.com/upload/picture/pic/131239/763239a3-9b58-39a1-8e18-a8e62fe409f7.png[/img]


附件是camel官方的example和我的demo打包
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值