2014-12-02 150 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」/ >
<路線/ >

這裏我們有父路由和子路由,一旦父路由獲得文件形式定義的位置,它將它發送到父路由中的直接端點。
小孩路線消費者終點是直接的,所以現在消息會來小孩路線。