Yes the exchange has fromRouteId and other information you can use in your
filter.
There is also an isEnabled method you can implement for the filtering.

On Tue, Jan 31, 2023 at 8:54 AM ski n <raymondmees...@gmail.com> wrote:

> I have a question about event notifiers.
>
> Currently, I collect events using EventNotifierSupport
>
>
> https://javadoc.io/doc/org.apache.camel/camel-support/latest/org/apache/camel/support/EventNotifierSupport.html
>
> There I get a "CamelEvent" as listed in this class.
>
>
> https://www.javadoc.io/doc/org.apache.camel/camel-api/latest/org/apache/camel/spi/CamelEvent.html
>
> Question:
>
> It's already possible to ignore certain type of events. However is it also
> possible to restrict the collection of events to specific routes (for
> example based on the route id)?
>
> In Camel2/ServiceMix this could be achieved to add one or more routes to a
> CamelContext and then foreach CamelContext you could collect events.
>
> Is there still something similar possible?
>
> Or should I pass a filter to a constructor in the collector like this:
>
> public class MyCollector extends EventNotifierSupport {
>
>     private String routeId;
>
>     public MyCollector(String routeId){
>         this.routeId = routeId;
>     }
>
>     @Override
>     public void notify(CamelEvent event) throws Exception {
>
>         //filter by instance of CamelEvent
>         if(event instanceof CamelEvent.ExchangeCreatedEvent) {
>
>             //Cast event to specific event
>             CamelEvent.ExchangeCreatedEvent exchangeCreatedEvent =
> (CamelEvent.ExchangeCreatedEvent) event;
>
>             //Get the message exchange from event
>             Exchange exchange = exchangeCreatedEvent.getExchange();
>
>             //Get the route id from exchange
>             String fromRouteId = exchange.getFromRouteId();
>
>             //filter by routeId
>             if(fromRouteId!=null && fromRouteId.equals(routeId)){
>
>                 //Get message body from exchange
>                 String body = exchange.getMessage().getBody(String.class);
>
>                 System.out.println("Exchange " +
> exchange.getExchangeId() + " with routeid " + routeId + " has been
> created.")
>                 System.out.println("Message body=" + body);
>
>
>             }
>
>         }
>
>     }
>
> }
>
>
> And then add the event notifier like this:
>
> MyCollector myCollector = new MyCollector("myRouteId");
> myCollector.setIgnoreCamelContextEvents(true);
> myCollector.setIgnoreCamelContextInitEvents(true);
> myCollector.setIgnoreRouteEvents(true);
> myCollector.setIgnoreServiceEvents(true);
> myCollector.setIgnoreStepEvents(true);
>
> context.getManagementStrategy().addEventNotifier(myCollector);
>
>
> Add event notifiers for each route (or a group route) like the above code.
> Or is there a smarter / better way? And are there performance requirements
> to take into account when using this in production?
>
> BTW I couldn't find a guide about the usage of event notifiers, so I only
> had the Javadoc's available, but then you need to know where to look
> at. Maybe it's good to add a guide about event notifiers to:
>
> https://camel.apache.org/camel-core/advanced-camel-core-guides/index.html
>
> Regards,
>
> Raymond
>


-- 
Claus Ibsen
-----------------
@davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Reply via email to