user7076183
user7076183

Reputation: 43

Apache Camel Cron Job scheduling

New to Apache camel, I have a requirement that I need to look for a folder and move the files from source to destination every five minutes. I wrote the below code, but when I run the Job it is copying the files to destination irrespective of time when I copy the files to source, Please help me in understanding this :

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) {
        }
    }

}

Upvotes: 2

Views: 16163

Answers (2)

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

As Claus Ibsen said, you can use cron directly:

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

More info can be found here: http://camel.apache.org/file2.html URI options->Consumer->scheduler

or here: http://camel.apache.org/quartz2.html Using QuartzScheduledPollConsumerScheduler

Here is my log:

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

You'll need the dependency:

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

Upvotes: 0

Evgeny Minkevich
Evgeny Minkevich

Reputation: 2429

In order to schedule a route I have been using the CAMEL QUARTZ component. I believe Claus has been alluding to this option as well:

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

cron.schedule value is set in params as:

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

I.e to run every two hours. So to run every 5 minutes that would be something like:

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

Upvotes: 2

Related Questions