2012-09-01 3 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 큐에 일부 샘플 메시지를 보내면 소비됩니다.

관련 문제