新的apache骆驼,我需要查找文件夹并每五分钟将文件从源移动到目标。我编写了以下代码,但是当我运行工作时,它将文件复制到目的地,无论我将文件复制为源以源为源时,请帮助我理解这一点:
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) {
}
}
}
为了安排我一直在使用骆驼石英组件的路由。我相信克劳斯(Claus)也一直在暗示:
from("quartz://scheduler_name?cron={{cron.schedule}}")
.routeId("cron-scheduler")
cron.schedule值以:
00+00+*/2+1/1+*+?+*
即每两个小时运行一次。因此,每5分钟运行一次,就像:
00+*/5+*+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-camel-212-even-easier-cron.html
或这里:http://camel.apache.org/file2.html uri options->"消费者 ->调度程序"
或这里: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>