connorp
connorp

Reputation: 244

Execute jobs after JVM termination

Is it possible to schedule jobs inside the JVM for execution after the JVM has been terminated?

In my application, the user can opt to receive notifications on any new emails in their inbox. I accomplished this using Quartz, with the EmailChecker job scheduled to execute every 45 seconds.

public void checkInbox() throws SchedulerException
{
    JobDetail job = JobBuilder.newJob(EmailChecker.class)
            .withIdentity("emailJob", "jobGroup").build();

    Trigger trigger = TriggerBuilder.newTrigger()
            .withIdentity("emailTrigger", "jobGroup")
            .withSchedule(CronScheduleBuilder.cronSchedule("0/45 * * * * ?"))
            .build();

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);

}

This all works fine, but only while the JVM is running. Once it exits, no more notifications will be sent.

The application is a desktop app, and therefore will not be running all the time. And this feature would be majorly useless if it only worked inside the JVM since the user will also be able to view their inbox in real time, so notifications would be redundant.

Upvotes: 3

Views: 148

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Not internally to the JVM, because once the JVM terminates it isn't running to execute jobs. You could use a tool like cron or at to schedule a new JVM. If you can leave the JVM executing, then you can use the JVM to schedule the jobs (you could possibly use something like quartz-scheduler).

Upvotes: 3

Related Questions