ActiveMQ和Java路由

时间:2014-11-26 17:00:36

标签: java apache-camel activemq

我是ActiveMQ(版本5.9.1)和Apache Camel的新手。我试验了几条路线。我用Java编写它们,然后打包在.jar中并部署在%ACTIVEMQ_HOME%/ lib上。在我的自定义foo-activemq.xml中,我添加了一个带有包子标记的camelContext标记。这是我的相关.xml

...
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <package>edu.foo.amq.camel</package>
</camelContext>
...

以下是我的两条路线:

package edu.foo.amq.camel;

import org.apache.camel.builder.RouteBuilder;

public class NumberRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
    from("jms:queue:number.queue")
      .marshal("UTF-8")
      .choice()
      .when(header("readyToGo").isNotEqualTo(true))
        .to("jms:queue:big.number.queue");
    }
}

package edu.foo.amq.camel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

public class BigNumberRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
    from("jms:queue:big.number.queue")
    .marshal("UTF-8")
    .split(body().tokenize("\n")).streaming()
    .process(new Processor() {
        @Override
        public void process(Exchange arg0) throws Exception {
            arg0.getIn().setHeader("readyToGo", true);
            System.out.println(arg0.getIn().getBody(
                    String.class));
            }
        })
        .to("jms:queue:number.queue");
    }
}

当我启动ActiveMQ代理时,我可以看到... Total 0 routes, of wich 0 is started.。如果我查看ActiveMQ Web控制台,我可以启动并运行队列number.queuebig.number.queue,但我的java路由不是。我错过了什么?

更新 我从我的foo-activemq.xml配置文件中删除camelContext标记,然后添加import标记:

...
<import resource="jetty.xml"/>
<import resource="foo-camel.xml"/>
...

这是我的foo-camle.xml配置文件,我主要使用%ACTIVEMQ_HOME%/ examples / conf / camel.xml文件。我将此文件放入%ACTIVEMQ_HOME%/ conf:

<beans xmlns="http://www.springframework.org/schema/beans"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://camel.apache.org/schema/spring
    http://camel.apache.org/schema/spring/camel-spring.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <package>edu.foo.amq.camel</package>
</camelContext>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
    <property name="connectionFactory">
      <bean class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="vm://amq-broker?create=false"/>
        <property name="userName" value="${activemq.username}"/>
        <property name="password" value="${activemq.password}"/>
      </bean>
    </property>
</bean>

但结果是一样的:ActiveMQ说没有路由

1 个答案:

答案 0 :(得分:1)

必须将Camel配置XML文件添加到activemq.xml配置文件中:

<import resource="foo-activemq.xml" />

修改

还有什么?

  • foo-activemq.xml重命名为默认activemq.xml
  • 再次验证您的jar是否真的在lib目录中(我相信您已经这样做了)
  • foo-camel-xml添加一个简单的测试路由,并检查日志文件是否加载了此路由,例如

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring" >
        <package>edu.foo.amq.camel</package>
        <route>
            <from uri="direct:start" />
            <log message="${body}" />
        </route>
    </camelContext>