How to find all Endpoints of route (Apache Camel, Java)

那年仲夏 提交于 2020-02-05 01:10:27

问题


I have several routes and many endpoints in Camel context. So need to get all endpoints created by one Route:

    CamelContext context = new DefaultCamelContext();
    RouteBuilder route1 = new RouteBuilder() {
        public void configure() {
            from("file:data/inbox?noop=true")
                    .routeId("myRoute1")
                    .enrich("http://website.com/file.txt")
                    .to("file:data/outbox")
                    .to("mock:someway");
        }
    };

    RouteBuilder route2 = new RouteBuilder() {
        public void configure() {
            from("file:data/outbox?noop=true")
                    .routeId("myRoute2")
                    .to("mock:myMom");
        }
    };

    context.addRoutes(route1);
    context.addRoutes(route2);

    context.start();

    // TODO 

    context.stop();

And before stop I need get all endpoints which created by myRoute1 ??? for example:

1.file://data/outbox 2.mock://someway 3.http://website.com/file.txt 4.file://data/inbox?noop=true

I can get only all Endpoints of Camel context as: context.getEndpoints()


回答1:


You could try the following:

  1. Give your route an routeId to be able to identify it later.
  2. Get the RouteDefinition from the Route from the CamelContext and filter the List of Outputs for the ToDefinition objects.

     List<ProcessorDefinition> outputProcessorDefs = exchange.getContext().getRouteDefinition("[routeId]").getOutputs();
     // Iterate and get all ProcessorDefinition objects which inherit from the ToDefinition
    



回答2:


An endpoint is not associated to a single route, as it can be reused among multiple routes. So you cannot really find out from the endpoint itself. But what you can do is to store all the endpoints in a local list, before you add the route, and then afterwards get all the endpoints again, and then diff these 2 lists of endpoints. all the new endpoints was then added by the new route.



来源:https://stackoverflow.com/questions/52006053/how-to-find-all-endpoints-of-route-apache-camel-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!