1

Ok, so I have two schedules as below. You can see that I have my executor service as well as a new instance of scheduler.

Seeing as I have a single instance of scheduler, and I have two tasks I want to run at different times. Does this mean that in the below configuration I'm merely rescheduling the existing instance of scheduler?

Do I need to have multiple scheduler instances?

Instantiate executor service and scheduler

    //Creates Executor Instance
    final ExecutorService es = Executors.newSingleThreadExecutor();

    // Creates a Scheduler instance.
    Scheduler scheduler = new Scheduler();

Create schedule for first recurring task

    // Schedule a once-a-week task at midday on Sunday.
    scheduler.schedule("* 12 * * 7", new Runnable() {
        public void run() {
            Log.i(CLASS_NAME, "ConstituentScraper Schedule");

            es.submit(new ConstituentScraper());
        }
    });

Create schedule for second recurring task

    // Schedule a once-a-day task.
    scheduler.schedule("* 7 * * 1-5 | * 18 * * 1-5 ", new Runnable() {
        public void run() {
            Log.i(CLASS_NAME, "SummaryScraper Schedule");

            es.submit(new SummaryScraper());
        }
    });
TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51

1 Answers1

0

The answer to the above is yes. You do need to have separate scheduler instances for each schedule.

So, as a result the code should look like this.

In the case that you have two schedules, an instance will be set up individually for each of them.

    // Creates a Constituent Scheduler instance.
    Scheduler constituentScheduler = new Scheduler();

    // Creates a Summary Scheduler instance.
    Scheduler summaryScheduler = new Scheduler();   

And each schedule can be set up individually

    // Schedule a once-a-week task at 8am on Sunday.        
    constituentScheduler.schedule("0 8 * * 7", new Runnable() {
        public void run() {
            Log.i(CLASS_NAME, "ConstituentScraper Schedule");

            es.submit(new ConstituentScraper());
        }
    });


    //scheduler.schedule("28 7 * * 1-5 | * 18 * * 1-5 ", new Runnable() {
    summaryScheduler.schedule("0 7 * * 1-5 |0 18 * * 1-5 ", new Runnable() {
        public void run() {
            Log.i(CLASS_NAME, "SummaryScraper Schedule");

            // TODO only put in queue if a working day
            es.submit(new SummaryScraper());
        }
    });

Each Schedule must also be started once it has been set up.

    // Starts the Scheduler
    constituentScheduler.start();

    // Starts the Scheduler
    summaryScheduler.start();
TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51
  • I disagree with that.. You can use one Scheduler instance to schedule multiple tasks with different SchedulingPatterns. – Zuko Sep 07 '17 at 11:39
  • -1. The same scheduler can be used. The scheduling duration is tied to the schedule() method not the instance of the scheduler. – talonx Jan 24 '18 at 17:43