Jeff Potts
Jeff Potts

Reputation: 10538

How can I run an Apache Camel route with a non-polling Consumer every few minutes

I have a custom Apache Camel component which consists of a Consumer which extends from DefaultConsumer and no Producer.

I have a Spring Boot app with several other Camel routes which all work fine.

I would like to modify an existing route so that it invokes the custom component every X minutes. The route works fine when it runs once. It looks like this:

JacksonDataFormat enrichedAuditLogEntryFormat = new JacksonDataFormat(EnrichedAuditLogEntry.class);

from("alfaudit://http://acs.local:8080?username=" + user + "&password=" + password)
        .routeId("alfrescoAuditLogToElastic")
        .bean("alfAuditLogEntryEnricher")
        .marshal(enrichedAuditLogEntryFormat)
        .setHeader("indexId", header(AlfAuditConsumer.AUDIT_LOG_ENTRY_ID))
        .to("elasticsearch-rest://elasticsearch?operation=Index&indexName=" + AUDIT_LOG_INDEX + "&indexType=" + AUDIT_LOG_TYPE);

I am struggling with how to implement the schedule. I've tried defining a SimpleScheduledRoutePolicy and then using setPolicy() on the route. I've also tried CronScheduledRoutePolicy with setPolicy(). Those appear to be useful if you need to start or stop the route on a schedule. They run my route once, but don't do it repeatedly. I need it to run the route every X minutes for as long as the app is running.

Next, I tried adding a quartz route that invokes the existing route, like this:

from("quartz2://fetchaudit?cron=0+0/5+*+*+*+?")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                CamelContext context = exchange.getContext();
                context.startRoute("alfrescoAuditLogToElastic");
            }
        });

That works the first time, but when the job is triggered next, it attempts to start the already-running route.

I was hoping to avoid modifying my custom Component to extend from DefaultScheduledPollConsumer because it won't always be used on a schedule. But, if that's the best way to address this, I am open to it.

Upvotes: 4

Views: 833

Answers (1)

anon
anon

Reputation:

I'm afraid I don't know anything about your custom component. But if it were able to be used as a producer and a consumer, you might try something like this. Note that as a producer, you may decide to ignore the entire Message if desired.

//trigger every 60 seconds
from("timer:timerName?period=60000")
  .routeId("alfrescoAuditLogToElastic")
  .to("alfaudit://http://acs.local:8080?username=" + user + "&password=" + password)
  .bean("alfAuditLogEntryEnricher")
  ...

Upvotes: 1

Related Questions