骆驼:从直接到处理器的路线

时间:2014-12-02 07:50:06

标签: java spring jms apache-camel

我有一条Spring DSL路线,如:

<bean id="sendMsgProc" class="com.tc.infrastructure.utils.jms.SendMessageProcessor"/>   

   <camel:camelContext id="folder-jms" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">
    <camel:propertyPlaceholder id="jmsProps" location="classpath:jms-jndi.properties"/>
        <route id="folder-jms-route" autoStartup="true">
           <!-- <from uri="{{jms.output.folder}}"/> -->
           <from uri="direct:start"/>  
          <!--  <to uri="bean:camelMsgBean"/> -->
           <camel:process ref="sendMsgProc"/>
           <to uri="{{jms.in.send}}"/> 
        </route>
    </camel:camelContext> 

我的主要课程开始上下文,如:

SpringCamelContext conetx = (SpringCamelContext)camel.initContextCamel("camel-context.xml", "folder-jms");
            Exchange ex = new DefaultExchange(conetx);
            ex.getIn().setBody(executionTasks.entrySet().iterator().next().getValue(), CamelMessage.class);
            conetx.start();
            conetx.startRoute("folder-jms-route");

            Thread.sleep(10000);
            conetx.stopRoute("folder-jms-route");
            conetx.stop();

我有一个处理器可以让我的对象形式交换:

public class SendMessageProcessor implements Processor {


    //This processor exist for set headers into sending message
    public void process(Exchange exchange) throws Exception 
    {
        System.out.println("adasdasd");
        CamelMessage message = (CamelMessage)exchange.getIn().getBody(CamelMessage.class);
        System.out.println("Message with correlationId get for exchange " + message.getMsgCorrelationId());
        System.out.println("Body" + message.getBody());
        }
}

我确实在Camel中设置了来自Map的对象,如:

public class CamelMessage extends Message {


    private Map<String, Object> headersMap;
    private StringBuffer body;
    private String msgCorrelationId;

                public CamelMessage(File msgPath, String msgCorrelationId)
        {
            super.setMsgPath(msgPath);
            this.msgCorrelationId = msgCorrelationId;
        }

         public CamelMessage(String correlationID, Map<String, Object> headers, String body)
        {
            setMsgCorrelationId(correlationID);
            setHeadersMap(headers);
            setBody(body);
        }

    public Map<String, Object> getHeadersMap() {
        return headersMap;
    }
    protected void setHeadersMap(Map<String, Object> headersMap) {

        if(headersMap == null)
               headersMap = new HashMap<String, Object>();

        this.headersMap = headersMap;
    }


    public String getBody() {
        return body.toString();
    }
    protected void setBody(String body) {
        if(this.body == null)
            this.body = new StringBuffer();

        this.body.append(body);
    }



    public String getMsgCorrelationId() {
        return msgCorrelationId;
    }
    private void setMsgCorrelationId(String msgCorrelationId) {
        this.msgCorrelationId = msgCorrelationId;
    }
}

我无法理解为什么我的Camel处理器无法工作(不会自动触发)。而且我希望得到我所设置的对象,我将所有的字段填满了。

请帮忙。

2 个答案:

答案 0 :(得分:3)

我会在conetx.startRoute("folder-jms-route");

后添加
ProducerTemplate pt = conetx.createProducerTemplate();
pt.send("direct:start", ex);

- 查看http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultProducerTemplate.html了解更多信息

send(String endpointUri, Exchange exchange)
Sends the exchange to the given endpoint 

Notice: that if the processing of the exchange failed with an Exception it is not thrown from this method, but you can access it from the returned exchange using Exchange.getException().

答案 1 :(得分:0)

消费者终点总是期待一些输入数据。
例如,只要JMS队列接收到消息,您就已经声明了JMS消费者端点,然后路由被激活,并且它的下一个处理器被调用在消费者终点旁边。但是在你的情况下,你已经声明了直接的消费者端点,所以一些生产者端点应该向Direct端点发送消息。
所以我们在java DSL中使用ProducerTemplate将消息发送到Direct端点。
< strong>在Spring DSL示例中:

&lt; route id =&#34; parent&#34;&gt;
&lt; from uri =&#34 ; file:location&#34; /&gt;
&lt; to uri =&#34; direct:start&#34; /&gt;
&lt; route /&gt;

&lt; route id =&#34; child&#34;&gt;
&lt; from uri =&#34; direct:start&#34; /&gt;
&lt; to uri =&#34; bean:textProcessor&#34; /&gt;
&lt; route /&gt;

这里我们有父路由,一旦父路由获取文件格式定义的位置,它将它发送到直接端点在父路线。
孩子路由消费者终点是直接的,所以现在消息将来到子路径。