Sunday, April 17, 2016

Add scripts dynamically as jobs using property/xml file in Quartz

Leave a Comment

Scenario : I want to create a scheduler application which should run shell scripts as per the defined schedule. To keep it simple, I want the user to add script name and execution timings in some external file (properties/xml) which will be used by my application. For now, I am planning to run this application as a background process on Linux server. In future may be we'll make it as a web-app.

What I've tried till now:

  1. I came across xmlschedulingdataprocessorplugin for this purpose but it requires user to write jobs as Java code and then add it in XML file.
  2. I found some examples for scheduling which presently isn't working.

Please suggest some helpful quartz API which can help me in fulfilling this purpose.

3 Answers

Answers 1

The below tutorial help you to schedule shell script.

http://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/

By using

Runtime.getRuntime().exec("sh shellscript.sh"); 

You can run shell script.

Answers 2

I would take the following approach :

  • You create a class JobShellRunner which will implement Job interface from quartz :

    public class JobShellRunner implements Job {  @Override public void execute(JobExecutionContext context)     throws JobExecutionException {     // here you take need information about the shell script you need to run, from the context and run the shell script  } 

    }

  • read properties files (i suppose here it will be available information about which shell script to run and it's schedule information). For each information needed you will create it's context and it's trigger:

    JobKey jobKey = new JobKey("jobShellRunner", "group1"); // put in the job key need information about shell script (path, etc) JobDetail jobA = JobBuilder.newJob(JobShellRunner.class) .withIdentity(jobKey).build(); 
  • Then the trigger (note that on the cron expression you should complete with the one that you read from properties file):

    Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName1", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build();

  • Then schedule the job

    scheduler.scheduleJob(jobA, trigger);

Answers 3

you can do something like as following way,

First of need to go likewise,

  1. create your java application which has one scheduled-job which will read at some time interval one propery/xml file which will provide an information for which shell_file needs to execute and at what time.
  2. While your program's scheduled-job read that property/xml file and getting information as following,

     2.1. Shell-Script-File-Name  2.2. Timing at what time that script needs to be execute. 
  3. This information which is read by above(step-2), with help of it, this job will create newer independent-job which is fully responsible for execute shell script at particular time.(that time will be your job-time which is read from your propery/xml file). also take care of it to it should be one time only(as per your requirement).

  4. this above step repeatedly does untill whole information read by this job and every time will generate one newer job.

  5. in case after some time user edit/updated/added new line into property/xml file this java program's scheduled job will read only that newer changes and accordingly does as like above explained.

you can see below image for better understanding purpose,

enter image description here

For scheduling purpose you can set-up spring-quartz API for schedule job.

here, I am going to give you little bit pseudo code,

public class JobA implements Job {      @Override     public void execute(JobExecutionContext context)             throws JobExecutionException {          // continues read property/xml file untill while file not read         // based upon above read info. generate new job(one time only) at runtime which has capability to execute shell script // shell script can be execute by java program by this , //  Runtime.getRuntime().exec("sh /full-path/shell_script_name.sh");       }  } ............ public class CronTriggerExample {     public static void main( String[] args ) throws Exception     {          JobKey jobKeyA = new JobKey("jobA", "group1");         JobDetail jobA = JobBuilder.newJob(JobA.class)                 .withIdentity(jobKeyA).build();          Trigger trigger1 = TriggerBuilder                 .newTrigger()                 .withIdentity("dummyTriggerName1", "group1")                 .withSchedule(                         CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // you can set here your comfortable job time...                 .build();          Scheduler scheduler = new StdSchedulerFactory().getScheduler();          scheduler.start();         scheduler.scheduleJob(jobA, trigger1);     } } 

So, this is an idea what I believe and represent over here, which is top-most suitable as per your requirement.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment