我如何每隔几分钟与非轮询消费者一起运行Apache Camel路由

问题描述 投票:1回答:1

我有一个自定义的Apache Camel组件,该组件由从DefaultConsumer扩展而没有Producer的Consumer组成。

我有一个Spring Boot应用程序,还有其他几条骆驼路线,都可以正常工作。

我想修改现有路由,以使其每X分钟调用一次自定义组件。该路线运行一次后运行良好。看起来像这样:

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);

我正在努力执行时间表。我试过定义SimpleScheduledRoutePolicy,然后在路由上使用setPolicy()。我也用setPolicy()尝试了CronScheduledRoutePolicy。如果您需要按计划启动或停止路线,这些路线似乎很有用。他们只运行我的路线一次,但不会重复执行。只要应用运行,我需要它每X分钟运行一次路由。

接下来,我尝试添加一个调用现有路由的石英路由,如下所示:

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

这是第一次,但是当下一次触发作业时,它将尝试启动已经运行的路径。

我希望避免修改自定义组件以从DefaultScheduledPollConsumer扩展,因为它不会总是按计划使用。但是,如果这是解决此问题的最佳方法,那么我会持开放态度。

java apache-camel quartz-scheduler alfresco
1个回答
0
投票

恐怕我对您的自定义组件一无所知。但是,如果可以将其用作生产者和消费者,则可以尝试这样的方法。请注意,作为制作人,您可以根据需要决定忽略整个Message

//trigger every 60 seconds
from("timer:timerName?period=60000")
  .routeId("alfrescoAuditLogToElastic")
  .to("alfaudit://http://acs.local:8080?username=" + user + "&password=" + password)
  .bean("alfAuditLogEntryEnricher")
  ...
© www.soinside.com 2019 - 2024. All rights reserved.