2012-09-01 86 views
-1

我必须在activemq的消费者端集成骆驼。我已经设置了activemq并尝试配置骆驼(使用java DSL)的消费语言,但它不适合我。以下是代码:通过Apache Camel获取消息?

public class TestConsumer { 
    static String url = ActiveMQConnection.DEFAULT_BROKER_URL; 
    static String subject = "Test-AMQ"; 

    public static void main(String[] args) throws Exception { 
     CamelContext context = new DefaultCamelContext(); 
     BrokerService broker = new BrokerService(); 
     //broker.addConnector(url); 
     //broker.setBrokerName("localhost"); 
     broker.start(); 

     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?create=false&waitForStart=10000"); 
     context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); 
     context.addRoutes(new Routes()); 
     context.start(); 
    } 
} 

class Routes extends RouteBuilder { 
    @Override 
    public void configure() throws Exception { 
     from("jms:"+new TestConsumer().subject).process(new Processor() { 
      @Override 
      public void process(Exchange arg0) throws Exception { 
       System.out.println("Camel Test Message: " + arg0.toString()); 
      } 
     }); 
    } 
} 
+0

“Test-AMQ”是AMQ队列名称 –

+0

“不起作用”是什么意思?你收到一个例外吗? –

+0

另外'vm:// localhost'使用嵌入的代理。 –

回答

4

一个与您的工作类似的示例。

import org.apache.activemq.ActiveMQConnection; 
//import org.apache.activemq.broker.BrokerService; 
import org.apache.activemq.camel.component.ActiveMQComponent; 
import org.apache.camel.CamelContext; 
import org.apache.camel.Exchange; 
import org.apache.camel.Processor; 
import org.apache.camel.builder.RouteBuilder; 
import org.apache.camel.impl.DefaultCamelContext; 

public class TestConsumer { 
    static String url = ActiveMQConnection.DEFAULT_BROKER_URL; 
    static String subject = "Test-AMQ"; 

public static void main(String[] args) throws Exception { 
    CamelContext context = new DefaultCamelContext(); 
    // BrokerService broker = new BrokerService(); 

    // broker.start(); 

    ActiveMQComponent comp = ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false"); 
    context.addComponent("jms",comp); 
    context.addRoutes(new Routes()); 
    context.start();   
} 
} 

class Routes extends RouteBuilder { 
@Override 
public void configure() throws Exception { 
    from("jms:"+TestConsumer.subject).process(new Processor() { 
     @Override 
     public void process(Exchange arg0) throws Exception { 
      System.out.println("Camel Test Message: " + arg0.toString()); 
     } 
    }); 

    from("timer://foo?fixedRate=true&period=2000").setBody(simple("Hello, World")).to("jms:"+TestConsumer.subject); 
} 
} 

不知道你希望安装程序如何在最后工作。 使用VM传输实际上并不需要启动专用代理,而是使用VM实例中的一个代理。我只是放了一个定时器路由来触发一些示例消息到该ActiveMQ队列中,这些消息将被消耗。

相关问题