0

Scheduling jobs using code is easy, but I would like to schedule jobs based on contents of a folder.

For example:

I want the folder in "\MyApp\Jobs\" to contain some XML files that will have the information about the IJob to be scheduled.

The thing is that I want this folder to be watched for changes (for XML files) and when a new file is found, a new IJob will be schedule using the information contained in the XML.

What should I do to implement a mechanism like this?

Thanks

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • Can you pick either the java or the .net version? While they're pretty similar, if you want specific information to one or the other platform it might help if your tags are specific as well – jvilalta Oct 15 '14 at 16:02

2 Answers2

0

The class java.io.File has a few listFiles() methods which will list the contents of your directory. Use a FileFilter or FilenameFilter if you want to limit the filenames returned in some way. Do this in a loop with a "sleep", something like 60 seconds, to avoid chewing up all your CPU.

Hope this helps.

Ed Randall
  • 6,887
  • 2
  • 50
  • 45
  • 1
    I was talking about .NET :) But I have just discovered that it can be made with a FileSystemWatcher. Thanks! – SuperJMN Oct 15 '14 at 17:42
  • There are some issues with that .Net class. – granadaCoder Oct 16 '14 at 19:32
  • http://stackoverflow.com/questions/3779143/c-sharp-filesystemwatcher-serious-problem http://stackoverflow.com/questions/6000856/filesystemwatcher-issues http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes Just be aware. – granadaCoder Oct 16 '14 at 19:33
0

you dont need to watch for changes in file using file watcher. while creating quartz property file it have option as org.quartz.plugin.jobInitializer.scanInterval = 5

which scans for xml file changes.so in above case it scans every 5 seconds

my complete quartz.property file as follow

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames =C:/Users/Admin/Documents/NetBeansProjects/QXmlTest/src/java/quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound =true
org.quartz.plugin.jobInitializer.scanInterval = 5
org.quartz.plugin.jobInitializer.wrapInUserTransaction= true

and i define jobs and trigger inside this quartz-config.xml file as follows :

<?xml version="1.0" encoding="UTF-8"?>

<job-scheduling-data
    xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData 
    http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
    version="1.8">

    <schedule>
        <job>
            <name>AJob</name>
            <group>AGroup</group>
            <description>Print a welcome message</description>
            <job-class>mypackage.SchedulerJob</job-class>
       </job>
       <trigger>
           <cron>
               <name>dummyTriggerName</name>
               <job-name>AJob</job-name>
               <job-group>AGroup</job-group>
               <!-- It will run every 5 seconds -->
               <cron-expression>0/5 * * * * ?</cron-expression>
           </cron>
       </trigger>
    </schedule>
</job-scheduling-data>

Please note that i am using quartz java api.

Atul Nar
  • 695
  • 6
  • 10