Java Code Examples for org.springframework.scheduling.quartz.QuartzJobBean

The following examples show how to use org.springframework.scheduling.quartz.QuartzJobBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-boot-quartz-demo   Source File: JobUtil.java    License: MIT License 7 votes vote down vote up
/**
 * Create Quartz Job.
 * 
 * @param jobClass Class whose executeInternal() method needs to be called. 
 * @param isDurable Job needs to be persisted even after completion. if true, job will be persisted, not otherwise. 
 * @param context Spring application context.
 * @param jobName Job name.
 * @param jobGroup Job group.
 * 
 * @return JobDetail object
 */
protected static JobDetail createJob(Class<? extends QuartzJobBean> jobClass, boolean isDurable, 
		ApplicationContext context, String jobName, String jobGroup){
    JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
    factoryBean.setJobClass(jobClass);
    factoryBean.setDurability(isDurable);
    factoryBean.setApplicationContext(context);
    factoryBean.setName(jobName);
    factoryBean.setGroup(jobGroup);
       
    // set job data map
       JobDataMap jobDataMap = new JobDataMap();
       jobDataMap.put("myKey", "myValue");
       factoryBean.setJobDataMap(jobDataMap);
       
       factoryBean.afterPropertiesSet();
       
    return factoryBean.getObject();
}
 
Example 2
Source Project: springbootquartzs   Source File: QuartzService.java    License: MIT License 6 votes vote down vote up
/**
 * 增加一个job
 *
 * @param jobClass
 *            任务实现类
 * @param jobName
 *            任务名称(建议唯一)
 * @param jobGroupName
 *            任务组名
 * @param jobTime
 *            时间表达式 (如:0/5 * * * * ? )
 * @param jobData
 *            参数
 */
public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String jobTime, Map jobData) {
    try {
        // 创建jobDetail实例,绑定Job实现类
        // 指明job的名称,所在组的名称,以及绑定job类
        // 任务名称和组构成任务key
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)
                .build();
        // 设置job参数
        if(jobData!= null && jobData.size()>0){
            jobDetail.getJobDataMap().putAll(jobData);
        }
        // 定义调度触发规则
        // 使用cornTrigger规则
        // 触发器key
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
                .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
                .withSchedule(CronScheduleBuilder.cronSchedule(jobTime)).startNow().build();
        // 把作业和触发器注册到任务调度中
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source Project: springbootquartzs   Source File: QuartzService.java    License: MIT License 6 votes vote down vote up
/**
 * 增加一个job
 *
 * @param jobClass
 *            任务实现类
 * @param jobName
 *            任务名称(建议唯一)
 * @param jobGroupName
 *            任务组名
 * @param jobTime
 *            时间表达式 (如:0/5 * * * * ? )
 * @param jobData
 *            参数
 */
public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String jobTime, Map jobData) {
    try {
        // 创建jobDetail实例,绑定Job实现类
        // 指明job的名称,所在组的名称,以及绑定job类
        // 任务名称和组构成任务key
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)
                .build();
        // 设置job参数
        if(jobData!= null && jobData.size()>0){
            jobDetail.getJobDataMap().putAll(jobData);
        }
        // 定义调度触发规则
        // 使用cornTrigger规则
        // 触发器key
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
                .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
                .withSchedule(CronScheduleBuilder.cronSchedule(jobTime)).startNow().build();
        // 把作业和触发器注册到任务调度中
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
Source Project: spring-boot-quartz-demo   Source File: JobServiceImpl.java    License: MIT License 6 votes vote down vote up
/**
 * Schedule a job by jobName at given date.
 */
@Override
public boolean scheduleCronJob(String jobName, Class<? extends QuartzJobBean> jobClass, Date date, String cronExpression) {
	System.out.println("Request received to scheduleJob");

	String jobKey = jobName;
	String groupKey = "SampleGroup";	
	String triggerKey = jobName;		

	JobDetail jobDetail = JobUtil.createJob(jobClass, false, context, jobKey, groupKey);

	System.out.println("creating trigger for key :"+jobKey + " at date :"+date);
	Trigger cronTriggerBean = JobUtil.createCronTrigger(triggerKey, date, cronExpression, SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);

	try {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		Date dt = scheduler.scheduleJob(jobDetail, cronTriggerBean);
		System.out.println("Job with key jobKey :"+jobKey+ " and group :"+groupKey+ " scheduled successfully for date :"+dt);
		return true;
	} catch (SchedulerException e) {
		System.out.println("SchedulerException while scheduling job with key :"+jobKey + " message :"+e.getMessage());
		e.printStackTrace();
	}

	return false;
}
 
Example 5
Source Project: bamboobsc   Source File: BaseJob.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * check before job in working.
 * 
 * @param context
 * @param currentJob
 * @return
 */
protected boolean checkCurrentlyExecutingJobs(JobExecutionContext context, QuartzJobBean currentJob) {
	boolean beofreJobWork = false;
	try {
		List<JobExecutionContext> jobs = context.getScheduler().getCurrentlyExecutingJobs();
		for (int j = 0; (jobs != null && !jobs.isEmpty()) && j < jobs.size() && !beofreJobWork; j++) {
			JobExecutionContext job = jobs.get(j);
			if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(currentJob)) {
				beofreJobWork = true;
			}
		}
	} catch (SchedulerException e) {
		e.printStackTrace();
	}
	return beofreJobWork;
}
 
Example 6
Source Project: springbootquartzs   Source File: QuartzService.java    License: MIT License 5 votes vote down vote up
/**
 * 增加一个job
 *
 * @param jobClass
 *            任务实现类
 * @param jobName
 *            任务名称
 * @param jobGroupName
 *            任务组名
 * @param jobTime
 *            时间表达式 (这是每隔多少秒为一次任务)
 * @param jobTimes
 *            运行的次数 (<0:表示不限次数)
 * @param jobData
 *            参数
 */
public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobTime,
                   int jobTimes, Map jobData) {
    try {
        // 任务名称和组构成任务key
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)
                .build();
        // 设置job参数
        if(jobData!= null && jobData.size()>0){
            jobDetail.getJobDataMap().putAll(jobData);
        }
        // 使用simpleTrigger规则
        Trigger trigger = null;
        if (jobTimes < 0) {
            trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
                    .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1).withIntervalInSeconds(jobTime))
                    .startNow().build();
        } else {
            trigger = TriggerBuilder
                    .newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder
                            .repeatSecondlyForever(1).withIntervalInSeconds(jobTime).withRepeatCount(jobTimes))
                    .startNow().build();
        }
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source Project: springbootquartzs   Source File: QuartzService.java    License: MIT License 5 votes vote down vote up
/**
 * 增加一个job
 *
 * @param jobClass
 *            任务实现类
 * @param jobName
 *            任务名称
 * @param jobGroupName
 *            任务组名
 * @param jobTime
 *            时间表达式 (这是每隔多少秒为一次任务)
 * @param jobTimes
 *            运行的次数 (<0:表示不限次数)
 * @param jobData
 *            参数
 */
public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobTime,
                   int jobTimes, Map jobData) {
    try {
        // 任务名称和组构成任务key
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)
                .build();
        // 设置job参数
        if(jobData!= null && jobData.size()>0){
            jobDetail.getJobDataMap().putAll(jobData);
        }
        // 使用simpleTrigger规则
        Trigger trigger = null;
        if (jobTimes < 0) {
            trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
                    .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1).withIntervalInSeconds(jobTime))
                    .startNow().build();
        } else {
            trigger = TriggerBuilder
                    .newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder
                            .repeatSecondlyForever(1).withIntervalInSeconds(jobTime).withRepeatCount(jobTimes))
                    .startNow().build();
        }
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source Project: spring-boot-quartz-demo   Source File: JobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * Schedule a job by jobName at given date.
 */
@Override
public boolean scheduleOneTimeJob(String jobName, Class<? extends QuartzJobBean> jobClass, Date date) {
	System.out.println("Request received to scheduleJob");

	String jobKey = jobName;
	String groupKey = "SampleGroup";	
	String triggerKey = jobName;		

	JobDetail jobDetail = JobUtil.createJob(jobClass, false, context, jobKey, groupKey);

	System.out.println("creating trigger for key :"+jobKey + " at date :"+date);
	Trigger cronTriggerBean = JobUtil.createSingleTrigger(triggerKey, date, SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
	//Trigger cronTriggerBean = JobUtil.createSingleTrigger(triggerKey, date, SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);

	try {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		Date dt = scheduler.scheduleJob(jobDetail, cronTriggerBean);
		System.out.println("Job with key jobKey :"+jobKey+ " and group :"+groupKey+ " scheduled successfully for date :"+dt);
		return true;
	} catch (SchedulerException e) {
		System.out.println("SchedulerException while scheduling job with key :"+jobKey + " message :"+e.getMessage());
		e.printStackTrace();
	}

	return false;
}
 
Example 9
Source Project: kvf-admin   Source File: ScheduleKit.java    License: MIT License 4 votes vote down vote up
public static QuartzJobBean getJobClass(String classname) {
    return (QuartzJobBean) SpringContextKit.getBean(classname);
}
 
Example 10
Source Project: api-boot   Source File: ApiBootJobWrapper.java    License: Apache License 2.0 4 votes vote down vote up
public Class<? extends QuartzJobBean> getJobClass() {
    return jobClass;
}
 
Example 11
Source Project: beihu-boot   Source File: ApiBootLoopJobWrapper.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 构造函数初始化父类的相关字段
 *
 * @param jobKey           任务key
 * @param loopIntervalTime 循环间隔次数
 * @param repeatTimes      重复次数
 * @param startAtTime      开始时间
 * @param param            参数
 * @param jobClass         任务执行类
 */
@Builder(builderMethodName = "Context", buildMethodName = "wrapper")
public ApiBootLoopJobWrapper(String jobKey, Class<? extends QuartzJobBean> jobClass, int repeatTimes, int loopIntervalTime, Date startAtTime, ApiBootJobParamWrapper param) {
    super(jobKey, jobClass, startAtTime, param);
    this.repeatTimes = repeatTimes;
    this.loopIntervalTime = loopIntervalTime;
}
 
Example 12
Source Project: api-boot   Source File: ApiBootLoopJobWrapper.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Constructor initialization {@link ApiBootLoopJobWrapper}
 *
 * @param jobKey           {@link ApiBootJobWrapper#getJobKey()}
 * @param jobClass         {@link ApiBootJobWrapper#getJobClass()}
 * @param repeatTimes      {@link ApiBootLoopJobWrapper#getRepeatTimes()}
 * @param loopIntervalTime {@link ApiBootLoopJobWrapper#getLoopIntervalTime()}
 * @param startAtTime      {@link ApiBootJobWrapper#getStartAtTime()}
 * @param param            {@link ApiBootJobWrapper#getParam()}
 */
@Builder(builderMethodName = "Context", buildMethodName = "wrapper")
public ApiBootLoopJobWrapper(String jobKey, Class<? extends QuartzJobBean> jobClass, int repeatTimes, Long loopIntervalTime, Date startAtTime, ApiBootJobParamWrapper param) {
    super(jobKey, jobClass, startAtTime, param);
    this.repeatTimes = repeatTimes;
    this.loopIntervalTime = loopIntervalTime;
}
 
Example 13
Source Project: beihu-boot   Source File: ApiBootOnceJobWrapper.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 构造函数初始化父类的相关字段
 *
 * @param jobKey      任务key
 * @param jobClass    任务执行类
 * @param param       参数集合
 * @param startAtTime 开始时间
 */
@Builder(builderMethodName = "Context", buildMethodName = "wrapper")
public ApiBootOnceJobWrapper(String jobKey, Class<? extends QuartzJobBean> jobClass, Date startAtTime, ApiBootJobParamWrapper param) {
    super(jobKey, jobClass, startAtTime, param);
}
 
Example 14
Source Project: beihu-boot   Source File: ApiBootCronJobWrapper.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 构造函数初始化父类的相关字段
 *
 * @param jobKey       任务key
 * @param cron         cron表达式
 * @param jobClass     执行任务类
 * @param param 参数封装对象
 */
@Builder(builderMethodName = "Context", buildMethodName = "wrapper")
public ApiBootCronJobWrapper(String jobKey, Class<? extends QuartzJobBean> jobClass, String cron, ApiBootJobParamWrapper param) {
    super(jobKey, jobClass, null, param);
    this.cron = cron;
}
 
Example 15
Source Project: api-boot   Source File: ApiBootOnceJobWrapper.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Constructor initialization {@link ApiBootOnceJobWrapper}
 *
 * @param jobKey      {@link ApiBootJobWrapper#getJobKey()}
 * @param jobClass    {@link ApiBootJobWrapper#getJobClass()}
 * @param startAtTime {@link ApiBootJobWrapper#getStartAtTime()}
 * @param param       {@link ApiBootJobWrapper#getParam()}
 */
@Builder(builderMethodName = "Context", buildMethodName = "wrapper")
public ApiBootOnceJobWrapper(String jobKey, Class<? extends QuartzJobBean> jobClass, Date startAtTime, ApiBootJobParamWrapper param) {
    super(jobKey, jobClass, startAtTime, param);
}
 
Example 16
Source Project: api-boot   Source File: ApiBootCronJobWrapper.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Constructor initialization {@link ApiBootCronJobWrapper}
 *
 * @param jobKey   {@link ApiBootJobWrapper#getJobKey()}
 * @param jobClass {@link ApiBootJobWrapper#getJobClass()}
 * @param cron     {@link ApiBootCronJobWrapper#getCron()}
 * @param param    {@link ApiBootJobWrapper#getParam()}
 */
@Builder(builderMethodName = "Context", buildMethodName = "wrapper")
public ApiBootCronJobWrapper(String jobKey, Class<? extends QuartzJobBean> jobClass, String cron, ApiBootJobParamWrapper param) {
    super(jobKey, jobClass, null, param);
    this.cron = cron;
}
 
Example 17
Source Project: spring-boot-quartz-demo   Source File: JobService.java    License: MIT License votes vote down vote up
boolean scheduleOneTimeJob(String jobName, Class<? extends QuartzJobBean> jobClass, Date date); 
Example 18
Source Project: spring-boot-quartz-demo   Source File: JobService.java    License: MIT License votes vote down vote up
boolean scheduleCronJob(String jobName, Class<? extends QuartzJobBean> jobClass, Date date, String cronExpression);