ActiveMQ+Camel+Spring+jms Demo(二)

  • Post author:
  • Post category:其他


client端请求服务端消息,服务端将数字消息 number*2 返回。文本消息 添加 client+text 返回。

具体修改。

route类。

client端修改为:

from("jms:queue:client:numbers").to("multiplier");
from("jms:queue:client:strings").to("textplier");

server端修改为:

from("jms:queue:server:numbers").to("multiplier");
from("jms:queue:server:strings").to("textplier");

service对应的也要修改:

client service 修改为向server端发送消息:

   public int sendHelloWorldNumToCamel(){
ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
int response = (Integer)camelTemplate.sendBody("jms:queue:server: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:server:strings", ExchangePattern.InOut, text);
return response;
}

对应的server的service修改为:


public int sendHelloWorldNumToCamel(){
ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
int response = (Integer)camelTemplate.sendBody("jms:queue:client: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:client:strings", ExchangePattern.InOut, text);
return response;
}

添加了一个新的xml。camel-client.xml:


<camel:camelContext id="camel-client">
<camel:template id="camelTemplate"/>
</camel:camelContext>

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

<bean id="client-jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://${host}:${tcp.port}"/>
</bean>

camel.properties 的配置:

client端host应写为server的ip,server端host是client的ip。


# properties for the application
tcp.port=61610
host=server
#host=xkorey-pc

web.xml中添加


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

另外2个官方demo链接:

[url]http://xkorey.iteye.com/blog/2113910[/url]

应该还会有后续……



版权声明:本文为xkorey原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。