org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean Java Examples

The following examples show how to use org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean. 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: QuartzConfigration.java    From springBoot-study with Apache License 2.0 6 votes vote down vote up
@Bean(name = "jobDetail")  
public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) {
    // ScheduleTask为需要执行的任务  
    MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();  
    /* 
     *  是否并发执行 
     *  例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了, 
     *  如果此处为true,则下一个任务会bing执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行 
     */  
    jobDetail.setConcurrent(true);  

    jobDetail.setName("scheduler");// 设置任务的名字  
    jobDetail.setGroup("scheduler_group");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用  

    /* 
     * 这两行代码表示执行task对象中的scheduleTest方法。定时执行的逻辑都在scheduleTest。
     */  
    jobDetail.setTargetObject(task);  

    jobDetail.setTargetMethod("scheduleTest");  
    return jobDetail;  
}
 
Example #2
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 #3
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 #4
Source File: JobAdder.java    From quartz-glass with Apache License 2.0 6 votes vote down vote up
private JobDetail methodExecuteJobDetail(Class<?> clazz, JobKey jobKey, JobDataMap jobDataMapping) throws Exception {
    MethodInvokingJobDetailFactoryBean factoryBean = new MethodInvokingJobDetailFactoryBean();
    factoryBean.setGroup(jobKey.getGroup());
    factoryBean.setName(jobKey.getName());

    factoryBean.setTargetClass(clazz);
    factoryBean.setTargetMethod(findExecuteMethod(clazz));
    boolean allowConcurrent = !clazz.isAnnotationPresent(DisallowConcurrentExecution.class);
    factoryBean.setConcurrent(allowConcurrent);

    factoryBean.afterPropertiesSet();

    JobDetail jobDetail = factoryBean.getObject();
    JobDataMap jobDataMap = jobDetail.getJobDataMap();
    jobDataMap.putAll(jobDataMapping);
    jobDataMap.remove("methodInvoker");

    PojoJobMeta pojoJobMeta = createPojoJobMeta(clazz, jobKey, jobDataMapping);
    jobDataMap.put(GlassConstants.POJO_JOB_META, pojoJobMeta);

    return jobDetail;
}
 
Example #5
Source File: GlassJobFactory.java    From quartz-glass with Apache License 2.0 6 votes vote down vote up
private void setTargetObject(Job job, TriggerFiredBundle bundle, boolean forceCreateObject) throws Exception {
    PojoJobMeta pojoJobMeta = getPojoJobMeta(bundle.getJobDetail());
    if (pojoJobMeta == null) return;
    if (pojoJobMeta.getTargetObject() != null && !forceCreateObject) return;

    Object targetObject;
    Class targetClass = pojoJobMeta.getTargetClass();
    try {
        targetObject = beanFactory.getBean(targetClass);
    } catch (NoSuchBeanDefinitionException e) {
        targetObject = targetClass.newInstance();
        beanFactory.autowireBean(targetObject);
    }

    populateJobDataMapTargetObject(bundle, targetObject);
    pojoJobMeta.setTargetObject(targetObject);

    MethodInvokingJobDetailFactoryBean methodInvoker = createMethodInvoker(pojoJobMeta);
    methodInvoker.setTargetObject(targetObject);

    MethodInvokingJob methodInvokingJob = (MethodInvokingJob) job;
    methodInvokingJob.setMethodInvoker(methodInvoker);
}
 
Example #6
Source File: QuartzConfigration.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "firstJobDetail")
public MethodInvokingJobDetailFactoryBean firstJobDetail() {
    FirstJob firstJob = (FirstJob) SpringUtil.getBean(FirstJob.class);
    MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
    // 是否并发执行
    jobDetail.setConcurrent(false);
    // 为需要执行的实体类对应的对象
    jobDetail.setTargetObject(firstJob);
    // 需要执行的方法
    jobDetail.setTargetMethod("task");
    return jobDetail;
}
 
Example #7
Source File: QuartzConfigration.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "secondJobDetail")
public MethodInvokingJobDetailFactoryBean secondJobDetail() {

    SecondJob secondJob = (SecondJob) SpringUtil.getBean(SecondJob.class);
    MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
    // 是否并发执行
    jobDetail.setConcurrent(false);
    // 为需要执行的实体类对应的对象
    jobDetail.setTargetObject(secondJob);
    // 需要执行的方法
    jobDetail.setTargetMethod("task");
    return jobDetail;
}
 
Example #8
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 #9
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 #10
Source File: GlassJobFactory.java    From quartz-glass with Apache License 2.0 5 votes vote down vote up
private MethodInvokingJobDetailFactoryBean createMethodInvoker(PojoJobMeta pojoJobMeta) throws Exception {
    MethodInvokingJobDetailFactoryBean factoryBean = new MethodInvokingJobDetailFactoryBean();
    factoryBean.setGroup(pojoJobMeta.getGroup());
    factoryBean.setName(pojoJobMeta.getName());

    factoryBean.setTargetClass(pojoJobMeta.getTargetClass());
    factoryBean.setTargetMethod(pojoJobMeta.getTargetMethod());
    factoryBean.setConcurrent(pojoJobMeta.isConcurrent());

    factoryBean.afterPropertiesSet();


    return factoryBean;
}