2014-11-22 61 views
0

我有很簡單的POJO類:Apache的駱駝:爲什麼我不能發送豆文字JMS

public class MessageBean { 

    String text; 

    public String getMessage() 
    { 
     return text; 
    } 

} 

和駱駝航線:

public static void main(String[] args) { 

     final MessageBean bean = new MessageBean(); 
     bean.text = "This is the text"; 

     CamelContext context = new DefaultCamelContext(); 
     ConnectionFactory conFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); 

     context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(conFactory)); 

     try { 
      context.addRoutes(new RouteBuilder() { 

       @Override 
       public void configure() throws Exception { 

        from("direct:hello").process(new Processor() { 

         public void process(Exchange exchange) throws Exception { 

          exchange.getIn().setBody(bean); 
         } 
        }).setBody(simple("${body.message}")).to("jms:queue:Test.Queue"); 
       } 
      }); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     try { 
      context.start(); 
      Thread.sleep(5000); 
      context.stop(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

我不明白爲什麼我不能從bean變量text發送文本到activemq隊列?

當我嘗試從文件夾發送txt文件時,它正確發送到隊列中的jms。

回答

1

要將消息插入駱駝路由,您需要將其發送到路由中的消費者端點,在此情況下爲direct:start。最簡單的方法是使用ProducerTemplate。在您的情況下已經啓動:

ProducerTemplate template = context.createProducerTemplate(); 
template.sendBody("direct:start", bean); 

但如果你最終只是想的bean.getMessage()內容發送到您的JMS隊列(這就是它看起來像你想在這裏做),你可能只是這樣做而不是從你的路線刪除setBody()電話:

template.sendBody("direct:start", bean.getMessage()); 

More information on ProducerTemplate

+0

你只需要使用ProducerTemplate發送消息踢的路線(它甚至可能是一個空的消息)。 – 2014-11-23 02:04:34