Apache Camel Cron 作业调度

问题描述 投票:0回答:3

对于 Apache Camel 来说,我有一个要求,我需要每五分钟查找一个文件夹并将文件从源移动到目标。我编写了以下代码,但是当我运行作业时,无论我将文件复制到源的时间如何,它都会将文件复制到目标,请帮助我理解这一点:

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.routepolicy.quartz.CronScheduledRoutePolicy;

public class App1 {

    public static void main(final String[] arguments) {
        final CamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {

                    CronScheduledRoutePolicy startPolicy = new CronScheduledRoutePolicy();
                    startPolicy.setRouteStartTime("0 0/5 * 1/1 * ? *");

                    from("file:E:\\TestingWatch1\\input")
                        .routeId("testRoute").routePolicy(startPolicy)
                        .to("file:E:\\TestingWatch1\\output");

                }
            });
            camelContext.start();
            Thread.sleep(10000);
            //camelContext.stop();
        } catch (Exception camelException) {
        }
    }

}
java apache-camel
3个回答
2
投票

为了安排路线,我一直在使用 CAMEL QUARTZ 组件。我相信克劳斯也提到过这个选项:

from("quartz://scheduler_name?cron={{cron.schedule}}")
    .routeId("cron-scheduler")

cron.schedule 值在参数中设置为:

00+00+*/2+1/1+*+?+*

即每两个小时运行一次。 因此,每 5 分钟运行一次,类似于:

00+*/5+*+1/1+*+?+*

1
投票

正如Claus Ibsen所说,你可以直接使用cron:

public class CopyTest {
    public static void main(final String[] arguments) {
        final CamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {

                    from("file://c:/test/input?scheduler=quartz2&scheduler.cron=00+*/5+*+1/1+*+?+*")
                        .routeId("testRoute")
                        .log(LoggingLevel.INFO, "File name : ${header.CamelFileName}")
                        .to("file://c:/test/output");
                }
            });
            camelContext.start();
            Thread.sleep(10*60*1000);
            //camelContext.stop();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

更多信息可以在这里找到: http://www.davsclaus.com/2013/08/apache-camel-212-even-easier-cron.html

或者在这里: http://camel.apache.org/file2.html URI 选项->消费者->调度器

或者在这里: http://camel.apache.org/quartz2.html 使用 QuartzScheduledPollConsumerScheduler

这是我的日志:

2016-12-23 21:52:24,440 [main           ] INFO  DefaultCamelContext            - Apache Camel 2.15.2 (CamelContext: camel-1) is starting
2016-12-23 21:52:24,440 [main           ] INFO  ManagedManagementStrategy      - JMX is enabled
2016-12-23 21:52:25,329 [main           ] INFO  DefaultTypeConverter           - Loaded 200 type converters
2016-12-23 21:52:25,985 [main           ] INFO  DefaultCamelContext            - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2016-12-23 21:52:25,985 [main           ] INFO  DefaultCamelContext            - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2016-12-23 21:52:26,187 [main           ] INFO  QuartzComponent                - Create and initializing scheduler.
2016-12-23 21:52:26,187 [main           ] INFO  QuartzComponent                - Setting org.quartz.scheduler.jmx.export=true to ensure QuartzScheduler(s) will be enlisted in JMX.
2016-12-23 21:52:26,312 [main           ] INFO  StdSchedulerFactory            - Using default implementation for ThreadExecutor
2016-12-23 21:52:26,312 [main           ] INFO  SimpleThreadPool               - Job execution threads will use class loader of thread: main
2016-12-23 21:52:26,359 [main           ] INFO  SchedulerSignalerImpl          - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2016-12-23 21:52:26,359 [main           ] INFO  QuartzScheduler                - Quartz Scheduler v.2.2.1 created.
2016-12-23 21:52:26,390 [main           ] INFO  RAMJobStore                    - RAMJobStore initialized.
2016-12-23 21:52:26,421 [main           ] INFO  QuartzScheduler                - Scheduler meta-data: Quartz Scheduler (v2.2.1) 'DefaultQuartzScheduler-camel-1' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

2016-12-23 21:52:26,421 [main           ] INFO  StdSchedulerFactory            - Quartz scheduler 'DefaultQuartzScheduler-camel-1' initialized from an externally provided properties instance.
2016-12-23 21:52:26,421 [main           ] INFO  StdSchedulerFactory            - Quartz scheduler version: 2.2.1
2016-12-23 21:52:26,999 [main           ] INFO  DefaultCamelContext            - Route: testRoute started and consuming from: Endpoint[file://c:/test/input?scheduler=quartz2&scheduler.cron=00+*%2F5+*+1%2F1+*+%3F+*]
2016-12-23 21:52:26,999 [main           ] INFO  QuartzComponent                - Starting scheduler.
2016-12-23 21:52:26,999 [main           ] INFO  QuartzScheduler                - Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED started.
2016-12-23 21:52:26,999 [main           ] INFO  DefaultCamelContext            - Total 1 routes, of which 1 is started.
2016-12-23 21:52:26,999 [main           ] INFO  DefaultCamelContext            - Apache Camel 2.15.2 (CamelContext: camel-1) started in 2.574 seconds
2016-12-23 21:55:00,052 [amel-1_Worker-1] INFO  testRoute                      - File name : client.log

您需要依赖项:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-quartz2</artifactId>
    <version>${camel.version}</version>
</dependency>

0
投票

我会成功的,但我了解它,告诉我更多关于 apache Camel 与 Quartz 的使用

© www.soinside.com 2019 - 2024. All rights reserved.