Spring Boot + Quartz:“请求的bean目前正在创建:是否存在无法解析的循环引用?”

时间:2017-01-13 22:55:22

标签: java spring spring-boot quartz-scheduler

我在Spring Boot应用程序中使用Quartz Scheduler在本教程(spring-boot-quartz-demo)之后实现了一个调度程序例程,并为我的目的做了一些修改。

例如,我的工作服务必须能够列出数据库及其子项中的所有对象,设置新值并最终更新。所有这一切都必须是交易性的。

由于某种原因,MyJob类不允许在其中声明事务方法,所以我通过使用transacitional方法注入一个新的服务类来解决这个问题。

这项工作完美无缺,但每次运行应用程序时,它都会给我一些警告信息:

[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBeanTrigger': Requested bean is currently in creation: Is there an unresolvable circular reference?


我减少了警告信息: @DependsOn("testJobBean")

我注意到作业类中的自动装配服务正在引起它,但为什么呢?我必须做些什么才能摆脱这些警告信息?

欢迎使用如何在作业类中声明@Transacional方法的解决方案!


QuartzConfig.java

@Configuration
@ConditionalOnProperty(name = "quartz.enabled")
public class QuartzConfig {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    List<Trigger> triggers;

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

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setOverwriteExistingJobs(true);
        factory.setDataSource(dataSource);
        factory.setTransactionManager(transactionManager);
        factory.setJobFactory(jobFactory);
        factory.setQuartzProperties(quartzProperties());

        if (triggers != null && !triggers.isEmpty()) {
            factory.setTriggers(((Trigger[]) 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 SimpleTriggerFactoryBean createTrigger(JobDetail jobDetail, long pollFrequencyMs) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(jobDetail);
        factoryBean.setRepeatInterval(pollFrequencyMs);
        factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
        return factoryBean;
    }

    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;
    }

}



AutowiringSpringBeanJobFactory.java

public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

    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);
        beanFactory.autowireBean(job);
        return job;
    }
}



MyJob.java

@Component
@DisallowConcurrentExecution
public class TestJob implements Job {

    @Autowired
    private TestService service;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        service.execute();
    }

    @Bean(name = "testJobBean")
    public JobDetailFactoryBean createJobDetail() {
        return QuartzConfig.createJobDetail(this.getClass());
    }

    @DependsOn("testJobBean")
    @Bean(name = "testJobBeanTrigger")
    public CronTriggerFactoryBean createTrigger(@Qualifier("testJobBean") JobDetail jobDetail,
            @Value("${quartz.job.test.cronExpression}") String cronExpression) {
        return QuartzConfig.createCronTrigger(jobDetail, cronExpression);
    }

}



MyService.java

@Service
@Slf4j
public class MyService {

    private TestRepository testRepository;

    @Autowired
    public MyService(TestRepository testRepository) {
        this.testRepository = testRepository;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
    public void execute() {
        log.debug("Job has been started");
        List<TestObject> list = testRepository.findAll();
        if (list != null && !list.isEmpty()) {

            System.out.println("\n");
            list.forEach(obj -> {
                System.out.println(
                        "TestObject ID: " + obj.getId() + "\n" + 
                        "SubTestObjects IDs: " + (obj.getSubTestObjects() == null ? "null" : obj.getSubTestObjects().toString()) + 
                        "AnotherSubTestObjects IDs: " + (obj.getAnotherSubTestObjects() == null ? "null" : obj.getAnotherSubTestObjects().toString())
                        );
            });
            System.out.println();

            log.info(list.size() + " test objects found");
        } else {
            log.info("No test objects were found");
        }
        log.debug("Job has been finished");
    }

}



quartz.properties

#============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.makeSchedulerThreadDaemon = true

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.makeThreadsDaemons = true

#============================================================================
# Configure JobStore
#============================================================================

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties = true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.clusterCheckinInterval 3600000

1 个答案:

答案 0 :(得分:1)

看起来问题可能出在createJobDetail方法中,请考虑以下代码段:

    @Bean(name = "testJobBean")
    public JobDetailFactoryBean createJobDetail() {
        return QuartzConfig.createJobDetail(this.getClass());
    }

    @DependsOn("testJobBean")
    @Bean(name = "testJobBeanTrigger")
    public CronTriggerFactoryBean createTrigger(@Qualifier("testJobBean") JobDetail jobDetail,
            @Value("${quartz.job.test.cronExpression}") String cronExpression) {
        return QuartzConfig.createCronTrigger(jobDetail, cronExpression);
    }

它在其实现中使用this来保存对当前对象的引用。但是,当前对象也可能在此时创建,从而导致此错误。我建议将这2个移到Config类。

此外,使用@DependsOn不是一个好习惯。