[英]Calling a Camel route endpoint in the main class
我創建了一個具有端點`direct:getRestFromExternalService的駱駝路線,當我嘗試在另一個類內的main方法中使用此端點時,出現異常
在交易所執行期間發生異常:Exchange [ID-WMLI118067-61025-1493883025815-0-2]
和
終結點上沒有可用的使用者:Endpoint [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方法的Class:
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?
我解決了問題,問題是在路由類和主方法類中都創建了上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.