在主类中调用Camel路由终结点

ryevplcw  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(176)

我创建了一个Camel路由,它的端点'direct:getRestFromExternalService,当我试图在另一个类的main方法中使用这个端点时,我得到了一个异常
在Exchange上执行期间出现异常错误:交易所[ID-WMLI 118067 -61025-1493883025815-0-2]

终结点上没有可用的使用者:端点[直接://从外部服务获取REST]。Exchange[ID-WMLI 118067 -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方法,它运行了。有没有办法从不同类中实现的路由中调用带有端点的requestBody?

sqyvllje

sqyvllje1#

我解决了这个问题,问题是在路由类和主方法类中都创建了上下文。

相关问题