![]() Job.getJobDataMap().put(FileScanJob.FILE_SCAN_LISTENER_NAME, JcgFileScanListener.LISTENER_NAME) Ģ.3 Setup FileScanListener and Schedule Jobįinally, we need to setup the file scan listener, to schedule and execute the job. Job.getJobDataMap().put(FileScanJob.FILE_NAME, filename) JobDetail job = JobBuilder.newJob(FileScanJob.class).withIdentity(jobKey).build() JcgQuartzApp.java JobKey jobKey = new JobKey("jcgFileScanJobName", "group1") The listener is a key value pair in the global job data map with the name of the file scan listener as the key and the listener object as the value, as seen below. When the last modified date is greater than the last executed time, the job will execute the listener that is defined in the global job data map. This job provided by the Quartz library will run at the specified interval and check to see if the last modified date on the file is greater than the last time the job executed. Next, we are going to setup a Quartz library job, FileScanJob, that is designed to watch a specified file. Scheduler scheduler = new StdSchedulerFactory().getScheduler() scheduler.start() 2.2 Define Job and Job Data JcgQuartzApp.java Trigger trigger = TriggerBuilder.newTrigger().withIdentity("jcgFileScanTriggerName", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build() The cron trigger will be set to run every 5 seconds. 2.1 Define a Cron Trigger and Standard Schedulerįirst we will define a cron trigger and use the StdSchedulerFactory to initiate a scheduler. See the following link for cron expressions in Quartz. Cron triggers use the cron syntax to schedule and repeat jobs at regularly scheduled intervals. Simple triggers run at a given time and will repeat as many times as requested. There are a few common options for triggers that include simple triggers and cron triggers (as in this example). ![]() In this case if the file was edited since the last execution. The job will decide whether to execute the provided listener based on the criteria set forth in the job. The scheduler will execute the defined job based on the provided trigger. To setup a file watcher with Quartz we must define a scheduler, trigger, job and listener. Java Quartz Trigger, Scheduler, Job and Listener 1.1 Add Dependencies for QuartzĪdd Quartz dependencies in the pom.xml file. We will use Maven to setup a new project in Eclipse with the appropriate Quartz dependencies. We used Eclipse Oxygen, Java 8, Maven 3.3.9 and Quartz 2.2.1. This example monitors a file over a regularly scheduled interval and will respond to any change to the file by executing a listener. In this example we are going to demonstrate how to make use of a Quartz File Watcher. Quartz is an open source framework that specializes in scheduling and executing jobs within a Java application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |