立即运行Quartz调度程序Java

时间:2017-05-16 14:20:52

标签: java quartz-scheduler

我正在尝试使用Quartz调度程序立即启动新作业,该作业只运行一次,作业未被触发且没有任何异常。

以下是代码:

public void startRecipeUpdateJob(Ingredients ingredient) {
        try {

            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put(RecipeMessages.INGREDIENT_TEXT, ingredient);

            // specify the job' s details..
            JobDetail job = JobBuilder.newJob(RecipesUpdateJob.class).setJobData(jobDataMap).build();

            // specify the running period of the job
            Trigger trigger = TriggerBuilder.newTrigger().startNow().build();
            SchedulerFactory schFactory = new StdSchedulerFactory();
            Scheduler sch = schFactory.getScheduler();
            sch.start();
            sch.scheduleJob(job, trigger);

            logger.info("Scheduler scheduled job: " + RecipesUpdateJob.class.getClass());

            Thread.sleep(1000L);

            sch.shutdown(false);

        } catch (SchedulerException | InterruptedException e) {
            logger.info("Scheduler Stopped running: " + e.getMessage());
        }
    }

这是工作班:

public class RecipesUpdateJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap map = context.getJobDetail().getJobDataMap();

        Ingredients ingredients = (Ingredients) map.get(RecipeMessages.INGREDIENT_TEXT);

        RecipeHelperTwo recipeHelper = new RecipeHelperTwo();
        recipeHelper.updateRecipesCaloriesByIngredient(ingredients);
    }

}

请帮忙, 感谢。

0 个答案:

没有答案