Example usage for org.springframework.scheduling.quartz JobDetailFactoryBean setJobDataAsMap

List of usage examples for org.springframework.scheduling.quartz JobDetailFactoryBean setJobDataAsMap

Introduction

In this page you can find the example usage for org.springframework.scheduling.quartz JobDetailFactoryBean setJobDataAsMap.

Prototype

public void setJobDataAsMap(Map<String, ?> jobDataAsMap) 

Source Link

Document

Register objects in the JobDataMap via a given Map.

Usage

From source file:eu.trentorise.game.managers.QuartzTaskManager.java

public void createTask(GameTask task, String gameId) {
    try {/*from ww  w.ja  va 2 s . co  m*/

        // start the scheduler
        // init in postcontruct not possible cause circolar reference of
        // gameCtx
        if (!scheduler.isStarted()) {
            init();
        }

        GameContext ctx = createGameCtx(gameId, task);
        // check scheduler context data
        if (!scheduler.getContext().containsKey(ctx.getGameRefId() + ":" + task.getName())) {
            scheduler.getContext().put(ctx.getGameRefId() + ":" + task.getName(), ctx);
            logger.debug("Added gameCtx {} to scheduler ctx", ctx.getGameRefId() + ":" + task.getName());
        }
        if (!scheduler.getContext().containsKey(task.getName())) {
            scheduler.getContext().put(task.getName(), task);
            logger.debug("Added {} task to scheduler ctx", task.getName());
        }

        // schedule task
        if (!scheduler.checkExists(new JobKey(task.getName(), ctx.getGameRefId()))
                && !scheduler.checkExists(new TriggerKey(task.getName(), ctx.getGameRefId()))) {
            JobDetailFactoryBean jobFactory = new JobDetailFactoryBean();
            jobFactory.setJobClass(GameJobQuartz.class);
            Map<String, Object> jobdata = new HashMap<String, Object>();
            jobdata.put("taskName", task.getName());
            jobdata.put("gameId", ctx.getGameRefId());
            jobFactory.setName(task.getName());
            jobFactory.setGroup(ctx.getGameRefId());
            jobFactory.setJobDataAsMap(jobdata);
            jobFactory.afterPropertiesSet();
            JobDetail job = jobFactory.getObject();

            CronTriggerFactoryBean triggerFactory = new CronTriggerFactoryBean();
            String cronExpression = task.getSchedule().getCronExpression();
            // fix for version 2.2.1 of CronTrigger
            triggerFactory.setCronExpression(fixCronExpression(cronExpression));
            triggerFactory.setName(task.getName());
            triggerFactory.setGroup(ctx.getGameRefId());
            triggerFactory.setJobDetail(job);
            triggerFactory.afterPropertiesSet();
            Trigger trigger = triggerFactory.getObject();
            scheduler.scheduleJob(job, trigger);
            logger.info("Created and started job task {} in group {}", task.getName(), ctx.getGameRefId());
        } else {
            logger.info("Job task {} in group {} already exists", task.getName(), ctx.getGameRefId());
        }

    } catch (Exception e) {
        logger.error(e.getMessage());
    }
}

From source file:org.libreplan.importers.SchedulerManager.java

/**
 * Creates {@link JobDetailFactoryBean} from the specified
 * <code>{@link JobSchedulerConfiguration}</code>
 *
 * @param jobSchedulerConfiguration// w ww. ja  va  2 s  .c  om
 *            configuration to create <code>JobDetailFactoryBean</>
 * @return the created <code>JobDetailFactoryBean</code> or null if unable to it
 */
private JobDetailFactoryBean createJobDetailBean(JobSchedulerConfiguration jobSchedulerConfiguration) {
    JobDetailFactoryBean jobDetailBean = new JobDetailFactoryBean();

    Class<?> jobClass = getJobClass(jobSchedulerConfiguration.getJobClassName());
    if (jobClass == null) {
        return null;
    }

    jobDetailBean.setName(jobSchedulerConfiguration.getJobName());
    jobDetailBean.setGroup(jobSchedulerConfiguration.getJobGroup());
    jobDetailBean.setJobClass(jobClass);

    Map<String, Object> jobDataAsMap = new HashMap<>();
    jobDataAsMap.put("applicationContext", applicationContext);
    jobDetailBean.setJobDataAsMap(jobDataAsMap);

    return jobDetailBean;
}