当前位置: 首页 > 知识库问答 >
问题:

在Spring引导不工作的情况下实现Quartz多作业的自动连接

谭宜
2023-03-14

我想在Spring用石英调度器。配置多个作业时,我得到以下异常

Job2中方法jobTrigger的参数0需要类型为“org.quartz.jobDetail”的bean,但找不到该bean。

石英-V2.3,Spring-V4.2.x

配置类

@Configuration
 public class SchedulerConfig {

private static final Logger LOG = LoggerFactory.getLogger(SchedulerConfig.class);

@Autowired
List<Trigger> triggers;

@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    return jobFactory;
}

@Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory)  throws IOException {
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
          factory.setAutoStartup(true);
    factory.setJobFactory(jobFactory);
         factory.setQuartzProperties(quartzProperties());
    if (triggers != null && !triggers.isEmpty()) {
         LOG.info("starting jobs... Total Triggers - " + triggers.size());
        factory.setTriggers(triggers.toArray(new Trigger[triggers.size()]));
    }

    return factory;
}

@Bean
public Properties quartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
}


public static CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
    CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
    factoryBean.setJobDetail(jobDetail);
    factoryBean.setCronExpression(cronExpression);
    factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
    return factoryBean;
}

public static JobDetailFactoryBean createJobDetail(Class jobClass) {
    JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
    factoryBean.setJobClass(jobClass);
    factoryBean.setDurability(true);
    return factoryBean;
}

春装工厂

public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

private static final Logger LOG = LoggerFactory.getLogger(AutowiringSpringBeanJobFactory.class);

private transient AutowireCapableBeanFactory beanFactory;

@Override
public void setApplicationContext(final ApplicationContext context) {
    beanFactory = context.getAutowireCapableBeanFactory();
}

@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
    final Object job = super.createJobInstance(bundle);
    LOG.info("create job instance");
    beanFactory.autowireBean(job);
    return job;
}
@Component
@DisallowConcurrentExecution
public class Job1 implements Job {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Value("${schedule}")
private String frequency;

@Autowired
private Service service;

@Override
public void execute(JobExecutionContext jobExecutionContext) {
    log.info("execute");
}

@Bean(name = "jobBean1")
public JobDetailFactoryBean job() {
    return SchedulerConfig.createJobDetail(this.getClass());
}

@Bean(name = "jobBean1Trigger")
public CronTriggerFactoryBean jobTrigger(@Qualifier("jobBean1")JobDetail jobDetail) {
    return SchedulerConfig.createCronTrigger(jobDetail, frequency);
}
@Component
@DisallowConcurrentExecution
public class Job2 implements Job {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Value("${schedule}")
private String frequency;

@Autowired
private Service service;

@Override
public void execute(JobExecutionContext jobExecutionContext) {
    log.info("execute");
}

@Bean(name = "jobBean2")
public JobDetailFactoryBean job() {
    return SchedulerConfig.createJobDetail(this.getClass());
}

@Bean(name = "jobBean2Trigger")
public CronTriggerFactoryBean jobTrigger(@Qualifier("jobBean2")JobDetail jobDetail) {
    return SchedulerConfig.createCronTrigger(jobDetail, frequency);
}

@AutoWired私服服务;

如果只有一个作业使用这个autowired bean,那么也没有例外。如何使用相同的自动连线依赖关系配置多个作业?是什么引起了这个问题?

共有1个答案

邓阳伯
2023-03-14

这是http://www.baeldung.com/spring-quartz-schedule的修改版本,您引用该版本用于在一个配置文件中处理多个Quartz作业。为了简洁起见,我不包括整个QrtzSheduler类,只是替换了调度器方法,并在触发器中使用了@Qualifier引用:

...
@Bean
public Scheduler scheduler(Map<String, JobDetail> jobMap, Set<? extends Trigger> triggers) throws SchedulerException, IOException {

    StdSchedulerFactory factory = new StdSchedulerFactory();
    factory.initialize(new ClassPathResource("quartz.properties").getInputStream());

    logger.debug("Getting a handle to the Scheduler");
    Scheduler scheduler = factory.getScheduler();
    scheduler.setJobFactory(springBeanJobFactory());
    Map<JobDetail,Set<? extends Trigger>> triggersAndJobs = new HashMap<JobDetail,Set<? extends Trigger>>;
    for(JobDetail jobDetail : jobMap.getValues()){
        for(Trigger trigger : triggers){
            if(trigger.getJobKey().equals(jobDetail.getKey())){
               Set<Trigger> set = new HashSet<>();
               set.add(trigger);
               triggerAndJobs.put(jobDetail,set);
            }
        }
    }
    scheduler.scheduleJobs(triggersAndJobs, false);

    logger.debug("Starting Scheduler threads");
    scheduler.start();
    return scheduler;
}

@Bean(name="jobOne")
public JobDetail jobDetailOne() {

    ...
}

@Bean(name="jobTwo")
public JobDetail jobDetailTwo() {

   ...
}

@Bean
public Trigger triggerOne(@Qualifier("jobOne")JobDetail jobDetail) {

   ...
}

@Bean
public Trigger triggerTwo(@Qualifier("jobTwo")JobDetail jobDetail) {

   ...
}
 类似资料:
  • 我已经将Jenkins build配置为构建gradle项目并在其上运行测试(使用connectedInstrumentTest),但构建在启动几秒后失败。 com.android.ddmlib.installexception在com.android.ddmlib.device.installpackage(device.java:884)在com.android.builder.testing

  • 我想使用Quartz、Spring和Hibernate创建动态作业。用户与web服务交互以创建此类作业: NoaJobInstancesDAO是一个简单的DAO类,它利用了Hibernate的EntityManager: 问题是,当这个作业触发时,会抛出一个异常: 我不明白为什么!在Manager类中,我以这种方式安排作业 其中排定程序作为 Edit1:应用程序上下文的实例似乎正确。问题不可能在那

  • 我有一个执行一些活动的类,我想创建一个自动处理这个操作的作业,例如每x分钟调度一次。我使用的是Quartz,这个类实现了Job,在我的driver类中,我创建了jobdetail、scheduler和触发器,然后启动它。但是,作业未执行,日志信息: 我的驱动程序类中的排定程序代码: 其中“testmkfpmjob”是处理我的操作的作业类,并且strTimeSched已经被提取并设置为120从 我一

  • 问题内容: 我找不到一个提交不使用不推荐使用的类的Hadoop作业的示例。 尚未弃用的,仍然仅支持带有参数的方法。 有人可以给我指出一个Java代码示例,该示例仅使用类(而不是)提交Hadoop map / reduce作业,而不是使用包吗? 问题答案: 希望对您有所帮助

  • 我正在建立一个应用程序,并使用MariaDB作为我的数据库。我有一张桌子“kick_votes”。其主键包含三个字段: null

  • 我有HTML+CSS页面,其中有两个按钮: 我想让“顶级评论”默认可见。所以在本页的最后,我将简单的JavaScript 自动点击“顶部评论”按钮。 在我的开发环境中,一切都运行良好。但当将代码上传到启用CloudFlare的生产服务器时。JavaScript click()函数不起作用。 我已经为这个问题给CloudFlare的支持写了信,但两天后仍然没有任何回应。 对于我如何调试为什么这个Ja