2014-12-02 116 views
2

我有一个像春天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()); 
     } 
} 

我设置为交易所骆驼对象从地图,如:

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; 
    } 
} 

我不明白为什么我的骆驼处理器不起作用(不触发automaticaly)。我希望得到我的对象,我用所有的领域填补交换骆驼交换。

请帮忙。

+1

你是如何将信息插入你的路线?此外,将'Message'扩展为'CamelMessage'看起来没有必要......如果您使用JMS进行消费,您应该可以将主体作为String对象进行获取。 – mdnghtblue 2014-12-02 14:26:57

回答

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(). 
0

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


<路线ID = “父” >
<从URI = “文件:位置”/ >
<到URI = “直接:开始”/ >
<路线/ >

<路线ID = “孩子” >
<从URI = “直接:开始”/ >
<到URI = “豆:textProcessor”/ >
<路线/ >

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