我想实现以下功能:
阅读CSV文件线由行则每行:
- 基于该行包含的值构建请求
- 将请求发送给消息队列
- 其他组件需要拿起消息,处理该请求并发送另一个消息队列的响应(称为生产者,所以生产者可以拿起的响应)。
我相信, 请求-应答模式符合该法案。 我安装的ActiveMQ,下载骆驼并试图用自己的JMS项目。
配置组件,队列和测试连接后(工作),我试图找出如何真正实现请求 - 应答? 我没能找到任何好的例子
我有一个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"));
}
});
}
}