我正在尝试实现以下功能:
逐行读取CSV文件,然后每行:
我相信request-reply pattern符合要求。 我安装了ActiveMQ,下载了camel并尝试使用他们的jms项目。
在配置组件,队列和测试连接(工作)之后,我试图弄清楚实际上如何实现请求 - 回复?我找不到任何好的examples
我有一个RouteBuilder
RouteBuilder
public class MyRouteBuilder extends RouteBuilder {
public static void main(String[] args) throws Exception {
new Main().run(args);
}
public void configure() {
from("file:src/data?noop=true")
.to("activemq:RequestQ");
from("activemq:RequestQ?exchangePattern=InOut&timeToLive=5000")
.inOut("activemq:RequestQ", "bean:myBean?method=someMethod");
}
}
驼context.xml中
<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 id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>org.apache.camel.example.spring</package>
</camelContext>
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="pooledConnectionFactory"
class="org.apache.activemq.pool.PooledConnectionFactory"
init-method="start" destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory"/>
<property name="concurrentConsumers" value="10"/>
</bean>
<bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>
</bean>
<bean id="myBean" class="org.apache.camel.example.spring.MyBean"/>
</beans>
问题:
修改
我得到了以下代码。 现在让我们说在处理器中我创建响应。 我该怎么发回来?我该如何使用响应?
public class MyRouteBuilder extends RouteBuilder {
public static void main(String[] args) throws Exception {
new Main().run(args);
}
public void configure() {
from("file:/Users/aviad/ws/integ/src/data?fileName=lines.txt&noop=true&idempotent=true")
.split()
.tokenize("\\n")
.inOut("activemq:req");
from("activemq:req")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println(exchange.getIn().getBody(String.class));
System.out.println("jmscorrelationid=" + exchange.getIn().getHeader("jmscorrelationid"));
System.out.println("jmsdestination=" + exchange.getIn().getHeader("jmsdestination"));
}
});
}
}
答案 0 :(得分:10)
我刚才有类似的东西,所以我改了它,就在这里。请注意,第二条路径不需要明确地知道请求/回复消息,只有生产者需要知道。如果对目的地集的回复(由驼峰自动处理),第二条路线将回复。
我不知道有什么好的例子,但this doc page对于小例子来说非常全面。
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file://c:/apps/in"/>
<split>
<tokenize token="\n"/>
<to uri="activemq:req" pattern="InOut"/>
<to uri="stream:out"/><!-- print Hello to console -->
</split>
</route>
<route>
<from uri="activemq:req"/>
<transform>
<simple>Hello ${in.body}</simple>
</transform>
</route>
</camelContext>