Apache的骆驼发送消息JMS消费者接收消息

问题描述:

从文件夹

的Apache骆驼简单的消息路由ActiveMQ的话题:Apache的骆驼发送消息JMS消费者接收消息

//Create context to create endpoint, routes, processor within context scope 
     CamelContext context = new DefaultCamelContext(); 


     //Create endpoint route 
     context.addRoutes(new RouteBuilder() { 

      @Override 
      public void configure() throws Exception 
      { 
       from("file:data/outbox").to("activemq:topic:Vadim_Topic"); 
       //from("activemq:topic:TEST").to.to("file:data/outbox"); 
      } 

     }); 

     context.start(); 
      Thread.sleep(5000); 
     context.stop(); 
    } 

和JMS实现,如果消费者主题:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); 
     try { 

      Connection connection = connectionFactory.createConnection(); 
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

      //connection.setClientID("12345"); 

      connection.start(); 
      Topic topic = session.createTopic("Vadim_Topic"); 
      MessageConsumer messageConsumer = session.createConsumer(topic); 

      MessageListener messageListener = new MessageListener() { 

       public void onMessage(Message message) { 

        TextMessage textMessage = (TextMessage) message; 
        try { 
         System.out.println("Received message: " + textMessage.getText()); 
        } catch (JMSException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 

      messageConsumer.setMessageListener(messageListener); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

不明白为什么是我的消费者无法收到Camel路由发送的消息? 我想这个问题是我需要订阅我的JMS消费者在骆驼发送的消息? 如果出现这种情况,我该怎么办?

+0

您确定文件已成功读取并发送至主题吗? – 2014-11-04 10:34:46

骆驼不仅可以让你发送消息到一个主题,它还可以很容易地从一个主题读取消息,并将其发送到你的POJO之一。

,从你的主题读取,并将消息发送到一个POJO看起来像这样的路线:

from("activemq:topic:Vadim_Topic").bean(ExampleBean.class); 

骆驼会弄清楚这取决于它收到的消息的类型的POJO调用哪个方法,和可用的方法签名。请参阅此页面,了解有关在骆驼路线中使用POJO的详细信息:https://camel.apache.org/bean.html