org.springframework.scheduling.quartz.CronTriggerFactoryBean Java Examples

The following examples show how to use org.springframework.scheduling.quartz.CronTriggerFactoryBean. 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 File: JobScheduler.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void afterPropertiesSet () throws Exception
{
   Map<String, AbstractJob> webappBeanNames =
      applicationContext.getBeansOfType (AbstractJob.class);
   triggers = new HashMap<> ();
   for (String webappBeanName : webappBeanNames.keySet ())
   {
      AbstractJob cron = webappBeanNames.get (webappBeanName);
      CronTriggerFactoryBean trigger = new CronTriggerFactoryBean ();
      JobDetail job = JobBuilder.newJob (cron.getClass ()).
         storeDurably (true).build ();
      trigger.setJobDetail (job);
      trigger.setCronExpression (cron.getCronExpression ());
      trigger.setName (webappBeanName + "Trigger");
      trigger.afterPropertiesSet ();
      triggers.put (cron.getClass(), trigger.getObject ());
   }
   super.setTriggers (triggers.values ().toArray (
      new Trigger[triggers.size ()]));
   super.setJobFactory (autowiringJobFactory);
   super.afterPropertiesSet ();
}
 
Example #2
Source File: SchedulerFactoryBeanWrapper.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param acf
 * @param sch
 * @return
 */
private Trigger registerSchedulerTriggerBean(DefaultListableBeanFactory acf,AbstractJob sch) {
	//注册JobDetail
	String jobDetailBeanName = sch.getJobName() + "JobDetail";
	if(context.containsBean(jobDetailBeanName)){
		throw new RuntimeException("duplicate jobName["+sch.getJobName()+"] defined!!");
	}
	BeanDefinitionBuilder beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingJobDetailFactoryBean.class);
	beanDefBuilder.addPropertyValue("targetObject", sch);
	beanDefBuilder.addPropertyValue("targetMethod", "execute");
	beanDefBuilder.addPropertyValue("group", groupName);
	beanDefBuilder.addPropertyValue("concurrent", false);
	acf.registerBeanDefinition(jobDetailBeanName, beanDefBuilder.getRawBeanDefinition());
	
	//注册Trigger
	String triggerBeanName = sch.getJobName() + "Trigger";
	beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(CronTriggerFactoryBean.class);
	beanDefBuilder.addPropertyReference("jobDetail", jobDetailBeanName);
	beanDefBuilder.addPropertyValue("cronExpression", sch.getCronExpr());
	beanDefBuilder.addPropertyValue("group", groupName);
	acf.registerBeanDefinition(triggerBeanName, beanDefBuilder.getRawBeanDefinition());
	
	return (Trigger) context.getBean(triggerBeanName);
	
}
 
Example #3
Source File: ScheduleModule.java    From bulbasaur with Apache License 2.0 6 votes vote down vote up
private void registerTrigger(
    String ownSign,
    String name,
    String jobDetailBeanName,
    String cronExpression,
    String triggerName

) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
        CronTriggerFactoryBean.class);
    builder.addPropertyValue("name", name);
    builder.addPropertyValue("group", ownSign);
    builder.addPropertyValue("jobDetail", beanFactory.getBean(jobDetailBeanName));
    builder.addPropertyValue("cronExpression", cronExpression);
    registry.registerBeanDefinition(triggerName, builder.getRawBeanDefinition());

}
 
Example #4
Source File: SchedulerFactoryBeanWrapper.java    From azeroth with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param acf
 * @param sch
 * @return
 */
private Trigger registerSchedulerTriggerBean(DefaultListableBeanFactory acf, AbstractJob sch) {
    //注册JobDetail
    String jobDetailBeanName = sch.getJobName() + "JobDetail";
    if (context.containsBean(jobDetailBeanName)) {
        throw new RuntimeException("duplicate jobName[" + sch.getJobName() + "] defined!!");
    }
    BeanDefinitionBuilder beanDefBuilder = BeanDefinitionBuilder
        .genericBeanDefinition(MethodInvokingJobDetailFactoryBean.class);
    beanDefBuilder.addPropertyValue("targetObject", sch);
    beanDefBuilder.addPropertyValue("targetMethod", "execute");
    beanDefBuilder.addPropertyValue("concurrent", false);
    acf.registerBeanDefinition(jobDetailBeanName, beanDefBuilder.getRawBeanDefinition());

    //注册Trigger
    String triggerBeanName = sch.getJobName() + "Trigger";
    beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(CronTriggerFactoryBean.class);
    beanDefBuilder.addPropertyReference("jobDetail", jobDetailBeanName);
    beanDefBuilder.addPropertyValue("cronExpression", sch.getCronExpr());
    beanDefBuilder.addPropertyValue("group", groupName);
    acf.registerBeanDefinition(triggerBeanName, beanDefBuilder.getRawBeanDefinition());

    logger.info("register scheduler task [{}] ok!!", sch.getJobName());
    return (Trigger) context.getBean(triggerBeanName);

}
 
Example #5
Source File: QuartzConfigration.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "secondTrigger")
public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) {
    CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
    trigger.setJobDetail(secondJobDetail);
    // cron表达式
    trigger.setCronExpression(Expression);
    //trigger.setCronExpression("0 0/1 * * * ?");
    return trigger;
}
 
Example #6
Source File: SlaCheckerCronJob.java    From mojito with Apache License 2.0 5 votes vote down vote up
@Bean
public CronTriggerFactoryBean triggerSlaCheckerCronJob(@Qualifier("slaCheckerCron") JobDetail job) {
    CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
    trigger.setJobDetail(job);
    trigger.setCronExpression(incidentCheckCron);
    return trigger;
}
 
Example #7
Source File: RepositoryStatisticsCronJob.java    From mojito with Apache License 2.0 5 votes vote down vote up
@Bean
public CronTriggerFactoryBean triggerRepositoryStatisticsCron(@Qualifier("repositoryStatisticsCron") JobDetail job) {
    CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
    trigger.setCronExpression(cron);
    trigger.setJobDetail(job);
    return trigger;
}
 
Example #8
Source File: QuartzConfig.java    From lemonaid with MIT License 5 votes vote down vote up
@Bean
public CronTriggerFactoryBean insideTrackTrigger() {
	CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
	cronTriggerFactoryBean.setJobDetail(insideTrackJob().getObject());
	cronTriggerFactoryBean.setCronExpression("0 0 * * * ?"); // Every hour
	cronTriggerFactoryBean.setGroup("lemonaid-quartz");
	return cronTriggerFactoryBean;
}
 
Example #9
Source File: QuartzConfig.java    From lemonaid with MIT License 5 votes vote down vote up
@Bean
public CronTriggerFactoryBean locationTrigger() {
	CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
	cronTriggerFactoryBean.setJobDetail(locationJob().getObject());
	cronTriggerFactoryBean.setCronExpression("0 15 0 * * ?"); // Every day
	cronTriggerFactoryBean.setGroup("lemonaid-quartz");
	return cronTriggerFactoryBean;
}
 
Example #10
Source File: QuartzConfig.java    From lemonaid with MIT License 5 votes vote down vote up
@Bean
public CronTriggerFactoryBean gravatarTrigger() {
	CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
	cronTriggerFactoryBean.setJobDetail(gravatarJob().getObject());
	cronTriggerFactoryBean.setCronExpression("0 30 * * * ?"); // Every hour
	cronTriggerFactoryBean.setGroup("lemonaid-quartz");
	return cronTriggerFactoryBean;
}
 
Example #11
Source File: TestContextConfiguration11.java    From spring-boot-starter-quartz with Apache License 2.0 5 votes vote down vote up
@Bean(name="cronTrigger2")
public CronTriggerFactoryBean createSimpleCronTrigger2(@Qualifier("cronJobDetail") JobDetail jobDetail) {
	Map<String, Object> map = new HashMap<>(1);
	map.put("myKey", "myValue");
    return QuartzUtils.createCronTrigger(jobDetail, "CTName2", "Cron", "CTDesc2", "0 0 0/1 1/1 * ? *", 
    		CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY, map, 10000L, new Date(), "Europe/Berlin", 1234);
}
 
Example #12
Source File: TestContextConfiguration4.java    From spring-boot-starter-quartz with Apache License 2.0 5 votes vote down vote up
@Bean(name="cronTrigger2")
public CronTriggerFactoryBean createSimpleCronTrigger2(@Qualifier("cronJobDetail") JobDetail jobDetail) {
	Map<String, Object> map = new HashMap<>(1);
	map.put("myKey", "myValue");
    return QuartzUtils.createCronTrigger(jobDetail, "CTName2", "Cron", "CTDesc2", "0 0 0/1 1/1 * ? *", 
    		CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY, map, 10000L, new Date(), "Europe/Berlin", 1234);
}
 
Example #13
Source File: QuartzConfiguration.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@Inject
@Bean
public CronTriggerFactoryBean cleanUpCronTriggerFactoryBean(ApplicationConfiguration configuration) {
    CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
    factory.setJobDetail(cleanUpJobDetail().getObject());
    factory.setCronExpression(configuration.getCronExpressionForOldData());
    factory.setMisfireInstruction(org.quartz.CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW);
    factory.setGroup(TRIGGER_GROUP);
    factory.setName(CLEAN_UP_TRIGGER_NAME);
    return factory;
}
 
Example #14
Source File: SpringBootAsyncProducerApplication.java    From Spring-Boot-2.0-Projects with MIT License 5 votes vote down vote up
@Bean
public CronTriggerFactoryBean trigger(JobDetail job, @Value("${images.cron}") String imagesCron) {
    CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
    cronTriggerFactoryBean.setCronExpression(imagesCron);
    cronTriggerFactoryBean.setJobDetail(job);
    return cronTriggerFactoryBean;
}
 
Example #15
Source File: Neutron.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * cron表达式触发器
 * @param triggerName 触发器名称
 * @param triggerGroup 触发器所在组
 * @param cronExpression cron表达式
 * @return CronTrigger
 * @throws ParseException cron解析异常
 */
public static CronTrigger createCronTrigger(String triggerName, String triggerGroup, String cronExpression) throws ParseException {
    CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
    factoryBean.setName(triggerName);
    factoryBean.setGroup(triggerGroup);
    factoryBean.setCronExpression(cronExpression);
    // 由于是手动创建的Bean,需要调用afterPropertiesSet()属性初始化方法
    factoryBean.afterPropertiesSet();
    return factoryBean.getObject();
}
 
Example #16
Source File: PacmanQuartzConfiguration.java    From pacbot with Apache License 2.0 5 votes vote down vote up
public CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
	CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
	factoryBean.setJobDetail(jobDetail);
	factoryBean.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
	factoryBean.setCronExpression(cronExpression);
	factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
	return factoryBean;
}
 
Example #17
Source File: SchedulerConfig.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 创建cron触发器
 *
 * @param jobDetail      job信息
 * @param cronExpression cron表达式
 * @param triggerName    触发器名称
 * @param triggerGroup   触发器组
 * @return
 */
private CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression,
                                                 String triggerName, String triggerGroup) {
    CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
    factoryBean.setJobDetail(jobDetail);
    factoryBean.setName(triggerName);
    factoryBean.setGroup(triggerGroup);
    factoryBean.setCronExpression(cronExpression);
    factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
    return factoryBean;
}
 
Example #18
Source File: QuartzConfigration.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@Bean(name = "jobTrigger")  
    public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {  
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();  
        tigger.setJobDetail(jobDetail.getObject());  
//        tigger.setCronExpression("0/30 * * * * ?");// 表示每隔30秒钟执行一次
        tigger.setCronExpression("1/30 * * * * ?");// 表示每隔30秒钟执行一次
        //tigger.set
        tigger.setName("myTigger");// trigger的name  
        return tigger;  

    }
 
Example #19
Source File: RecoverScheduledJob.java    From distributed-transaction-process with MIT License 5 votes vote down vote up
/**
 * 初始化方法,Spring启动时执行.
 */
public void init() {

    try {
    	// MethodInvokingJobDetailFactoryBean 负责生成具体的任务,只需要指定某个对象的某个方法,在触发器触发时,即调用指定对象的指定方法。
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 指定该任务对应的调用对象,这个对象所属的类无需实现任何接口
        jobDetail.setTargetObject(transactionRecovery);
        // 指定在targetObject对象中某个的方法(此处调用TransactionRecovery中的startRecover方法)
        jobDetail.setTargetMethod("startRecover");
        // 设置任务名称
        jobDetail.setName("transactionRecoveryJob");
        // 是否允许任务并发执行,类默认是并发执行的,这时候如果不设置“concurrent”为false,很可能带来并发或者死锁的问题,而且几率较小,不容易复现,
        // 设置为false表示等上一个任务执行完后再开启新的任务
        jobDetail.setConcurrent(false);
        jobDetail.afterPropertiesSet();

        // 触发器生成器类,用被指定的调度器调度生成指定规则的触发器对象
        // 该类负责在spring容器中创建一个触发器,该类的ID应该在SchedulerFactoryBean属性的List中被引用,这样这个触发器才能保证被某个指定调度器调度
        CronTriggerFactoryBean cronTrigger = new CronTriggerFactoryBean();
        // 设置触发器名称
        cronTrigger.setBeanName("transactionRecoveryCronTrigger");
        // 触发规则(这里通过事务配置器获取事务恢复定时任务规则)
        cronTrigger.setCronExpression(transactionConfigurator.getRecoverConfig().getCronExpression());
        cronTrigger.afterPropertiesSet();

        // 设置调度任务
        scheduler.scheduleJob(jobDetail.getObject(), cronTrigger.getObject());
        // 启动任务调度器
        scheduler.start();

    } catch (Exception e) {
        throw new SystemException(e);
    }
}
 
Example #20
Source File: TestContextConfiguration4.java    From spring-boot-starter-quartz with Apache License 2.0 4 votes vote down vote up
@Bean(name="cronTrigger")
public CronTriggerFactoryBean createSimpleCronTrigger(@Qualifier("cronJobDetail") JobDetail jobDetail) {
    return QuartzUtils.createCronTrigger(jobDetail, null, "Cron", null, "0 0 0/1 1/1 * ? *", 5000L, null);
}
 
Example #21
Source File: TestContextConfiguration11.java    From spring-boot-starter-quartz with Apache License 2.0 4 votes vote down vote up
@Bean(name="cronTrigger")
public CronTriggerFactoryBean createSimpleCronTrigger(@Qualifier("cronJobDetail") JobDetail jobDetail) {
    return QuartzUtils.createCronTrigger(jobDetail, null, "Cron", null, "0 0 0/1 1/1 * ? *", 5000L, null);
}
 
Example #22
Source File: TaskTrigger.java    From document-management-software with GNU Lesser General Public License v3.0 4 votes vote down vote up
public CronTriggerFactoryBean getCronTrigger() {
	return cronTrigger;
}
 
Example #23
Source File: PacmanQuartzConfiguration.java    From pacbot with Apache License 2.0 4 votes vote down vote up
@Bean(name = "weeklyReportSyncJobTrigger")
public CronTriggerFactoryBean weeklyReportSyncJobTrigger(@Qualifier("weeklyScheduleJobDetail") JobDetail jobDetail, @Value("${cron.frequency.weekly-report-sync-trigger}") String frequency) {
	return createCronTrigger(jobDetail, frequency);
}
 
Example #24
Source File: QuartzUtils.java    From spring-boot-starter-quartz with Apache License 2.0 4 votes vote down vote up
/**
 * creates a cron trigger builder. you have to call {@link #build()} afterwards to get the object
 */
public QuartzCronTriggerBuilder() {
	triggerFactoryBean = new CronTriggerFactoryBean();
}
 
Example #25
Source File: QuartzUtils.java    From spring-boot-starter-quartz with Apache License 2.0 3 votes vote down vote up
/**
 * returns a cron trigger factory bean to put into spring context
 *
 * @param jobDetail
 * @param triggerName (optional)
 * @param triggerGroup (optional)
 * @param triggerDescription (optional)
 * @param cronExpression
 * @param misfireInstruction see {@link CronTrigger}
 * @param jobData
 * @param startDelay if start delay is set, startTime will be ignored
 * @param startTime
 * @param timeZone
 * @param priority
 * @return
 */
public static CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String triggerName, String triggerGroup,
		String triggerDescription, String cronExpression, int misfireInstruction, Map<String, ?> jobData,
		long startDelay, Date startTime, String timeZone, int priority) {

	return QuartzUtils.cronTriggerBuilder().jobDetail(jobDetail).name(triggerName).group(triggerGroup)
			.description(triggerDescription).cronExpression(cronExpression).startDelay(startDelay)
			.misfireInstruction(misfireInstruction).putJobData(jobData).timeZone(timeZone).startTime(startTime)
			.priority(priority).getTriggerFactoryBean();
}
 
Example #26
Source File: QuartzUtils.java    From spring-boot-starter-quartz with Apache License 2.0 3 votes vote down vote up
/**
 * creates a CronTriggerFactoryBean regarding the parameters with {@link CronTrigger#MISFIRE_INSTRUCTION_DO_NOTHING}
 * @see QuartzUtils#createCronTrigger(JobDetail, String, String, String, String, int, Map, long, Date, String, int)
 * 
 * @param jobDetail
 * @param triggerName (optional)
 * @param triggerGroup (optional)
 * @param triggerDescription (optional)
 * @param cronExpression
 * @param startDelay
 * @param jobData (optional)
 * @return
 */
public static CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String triggerName, String triggerGroup,
		String triggerDescription, String cronExpression, long startDelay, Map<String, ?> jobData) {

	return QuartzUtils.cronTriggerBuilder().jobDetail(jobDetail).name(triggerName).group(triggerGroup)
			.description(triggerDescription).cronExpression(cronExpression).startDelay(startDelay)
			.misfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING).putJobData(jobData)
			.getTriggerFactoryBean();
}
 
Example #27
Source File: SchedulerConfig.java    From zkdoctor with Apache License 2.0 3 votes vote down vote up
/**
 * 实例连接信息收集定时任务触发器信息
 *
 * @param jobDetail      job信息
 * @param cronExpression cron表达式
 * @return
 */
@Bean(name = "instanceConnectionCollectJobTrigger")
public CronTriggerFactoryBean instanceConnectionCollectJobTrigger(@Qualifier("instanceConnCollectJobDetail") JobDetail jobDetail,
                                                                  @Value(SchedulerConstant.INSTANCE_CONN_COLLECT_JOB_CRON) String cronExpression) {
    return createCronTrigger(jobDetail, cronExpression,
            SchedulerConstant.INSTANCE_CONN_COLLECT_TRIGGER_NAME, SchedulerConstant.INSTANCE_CONN_COLLECT_TRIGGER_GROUP);
}
 
Example #28
Source File: SchedulerConfig.java    From zkdoctor with Apache License 2.0 3 votes vote down vote up
/**
 * 监控报警定时任务触发器信息
 *
 * @param jobDetail      job信息
 * @param cronExpression cron表达式
 * @return
 */
@Bean(name = "machineJobTrigger")
public CronTriggerFactoryBean machineJobTrigger(@Qualifier("machineJobDetail") JobDetail jobDetail,
                                                @Value(SchedulerConstant.MACHINE_COLLECT_JOB_CRON) String cronExpression) {
    return createCronTrigger(jobDetail, cronExpression,
            SchedulerConstant.MACHINE_COLLECT_TRIGGER_NAME, SchedulerConstant.MACHINE_COLLECT_TRIGGER_GROUP);
}
 
Example #29
Source File: SchedulerConfig.java    From zkdoctor with Apache License 2.0 3 votes vote down vote up
/**
 * 监控报警定时任务触发器信息
 *
 * @param jobDetail      job信息
 * @param cronExpression cron表达式
 * @return
 */
@Bean(name = "monitorJobTrigger")
public CronTriggerFactoryBean monitorJobTrigger(@Qualifier("monitorJobDetail") JobDetail jobDetail,
                                                @Value(SchedulerConstant.ZK_MONITOR_JOB_CRON) String cronExpression) {
    return createCronTrigger(jobDetail, cronExpression,
            SchedulerConstant.ZK_MONITOR_TRIGGER_NAME, SchedulerConstant.ZK_MONITOR_TRIGGER_GROUP);
}
 
Example #30
Source File: SchedulerConfig.java    From zkdoctor with Apache License 2.0 3 votes vote down vote up
/**
 * 历史数据清除定时任务触发器信息
 *
 * @param jobDetail      job信息
 * @param cronExpression cron表达式
 * @return
 */
@Bean(name = "cleanCollectDataJobTrigger")
public CronTriggerFactoryBean cleanCollectDataJobTrigger(@Qualifier("cleanCollectDataJobDetail") JobDetail jobDetail,
                                                         @Value(SchedulerConstant.ZK_CLEAN_JOB_CRON) String cronExpression) {
    return createCronTrigger(jobDetail, cronExpression,
            SchedulerConstant.ZK_CLEAN_TRIGGER_NAME, SchedulerConstant.ZK_CLEAN_TRIGGER_GROUP);
}