首页 文章

在主类中调用Camel路由 endpoints

提问于
浏览
0

我使用 endpoints `direct:getRestFromExternalService创建了一个Camel路由,当我尝试在另一个类中的main方法中使用此 endpoints 时,我得到一个异常

在交易所执行期间发生异常:交易所[ID-WMLI118067-61025-1493883025815-0-2]

endpoints 上没有可用的消费者: endpoints [direct:// getRestFromExternalService] . 交换[ID-WMLI118067-61025-1493883025815-0-2]

这是路线类:

public class MyRoute extends RouteBuilder {

@Override
public void configure() throws Exception {

    // Create the camel context for the REST API routing in Fuse
            CamelContext contextFuseAPI = new DefaultCamelContext();

            // Start the route inside the context to listen to the ActiveMQ
            contextFuseAPI.addRoutes(new RouteBuilder() {

                @Override
                public void configure() {
                    from("direct:getRestFromExternalService")
                        .setHeader(Exchange.HTTP_METHOD, simple("GET"))
                        .to("<external API URI>");
                }
});
}
}

这是使用main方法调用此路由的类:

public class FuseApp {

public static void main(String[] args) throws Exception {
    CamelContext contextFuseAPI = new DefaultCamelContext();

    contextFuseAPI.addRoutes(new MyRoute());

    contextFuseAPI.start();

    Thread.sleep(3000);

    ProducerTemplate template = contextFuseAPI.createProducerTemplate();

    Object result = template.requestBody("direct:getRestFromExternalService", null, String.class);


    Exchange exchange = new DefaultExchange(contextFuseAPI);
    String response = ExchangeHelper.convertToType(exchange, String.class, result); 
    System.out.println("Response : "+ response);        

    contextFuseAPI.stop();


}

}

我在没有ProducerTemplate和Object行的情况下测试了main方法,并且它运行了 . 有没有办法使用来自不同类中实现的路由的 endpoints 调用requestBody?

1 回答

  • 0

    我解决了它,问题是上下文是在路由类和主方法类中创建的 .

相关问题