Passing array of arguments to Quartz Scheduler

1.2k views Asked by At

I am trying to add a Quartz Scheduler to an existing Java class.

The class uses String[] args as input from the main function.

public static void main(String[] args) {
   //validate(args);
   summarizeData(args);
}

But then the Java class implements Job, and it has to use the execute method which only takes one argument.

public void execute(JobExecutionContext arg0) throws JobExecutionException {
   // How to get the String[] args and pass it to this execute method?
   // Then I can pass it to the next helper functions, etc.
   // summarizeData(args);
}

Any suggestions?

Edit 1: The args should be command-line arguments in String

3

There are 3 answers

0
gTang On BEST ANSWER

Problem solved.

I had to map the command-line arguments to JobDataMap in the scheduler class. Then use getJobDataMap() in the .execute method to retrieve the arguments and putting them back into a String[].

1
Avantol13 On

In this situation, depending on what kind of arguments are being passed in (and the class you're creating) it may be advantageous to just create new static class variables and set them within main. If you need non-static variables, you may need to modify your class constructor and accessor/mutator (getter/setter) methods to set those.

Option I - Static Variables?

This will only work if the variables will be the same across all instances of the class!!

Also, I don't know exactly how Quartz works, so heed my warning in the code comments.

public class YourClass extends Job
{
    // Your other class variables

    // New static variables
    private static String someVariable;
    private static String someOtherVariable;

    public static void main(String[] args) {
        someVariable = args[0]; //<---- Set class variables
        someOtherVariable = args[1];

        // Do other stuff
    }

    // WARNING: If main isn't called (from any instance) before execute
    // the class variables will still be null
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
       // Do stuff with someVariable and someOtherVariable 
       System.out.println(someVariable + " " + someOtherVariable);
   }
}

Option II - Non-static Variables

Depending on your implementation, you may want to set the variables outside of main so they're not static. In that case, just create non-static class variables and modify your constructor to set these when the object is created (and/or add accessor/mutator (getter/setter) methods to modify the variables after initialization).

Since I don't know how you're using this class, I don't know which of the above two options will work best.

0
Thermech On

You can add object into the scheduler context

scheduler.getContext().put("arg1", "value1"); //or loop through your args

And then retrieve it:

public void execute(JobExecutionContext arg0) throws JobExecutionException {
    String arg1 = null;
    try {
        arg1 = (String)jobExecutionContext.getScheduler().getContext().get("arg1");
        //Do something
    } catch (SchedulerException e) {
        //Error management
    }
}