简体   繁体   English

Spring在第一个Quartz作业执行期间抛出IllegalStateException

[英]IllegalStateException thrown by Spring during first Quartz job execution

During the first execution of the first job in Quartz scheduler, the following exception is thrown by Spring. 在Quartz Scheduler中第一次执行第一个作业的过程中,Spring抛出以下异常。 Note that the job makes an explicit call to applicationContext.getBean(...) in its execution. 请注意,作业在执行过程中对applicationContext.getBean(...)进行了显式调用。

Can someone explain the cause of this exception, and, maybe, the way to avoid it ? 有人可以解释这种异常的原因,以及避免这种异常的方法吗?

Spring version : 4.1.5.RELEASE Quartz version : 2.1.6 春季版本:4.1.5.RELEASE石英版本:2.1.6

Thanks in advance 提前致谢

2015-07-24 09:20:27,416 ERROR be.citobi.mediquality.schedulers.A4MCubeJob - a4MCubeJob in error
java.lang.IllegalStateException: About-to-be-created singleton instance implicitly appeared through the creation of the factory bean that its bean definition points to
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:860)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:790)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:542)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:436)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:412)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:398)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:337)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
    at be.citobi.mediquality.schedulers.A4MCubeJob.execute(A4MCubeJob.java:26)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

I'm not sure if my solution is correct, but here is the way how I'm autowiring spring beans to my jobs. 我不确定我的解决方案是否正确,但是这是我将春豆自动布线到我的工作的方式。 SchedulerFactoryBean creation: SchedulerFactoryBean的创建:

//Note: MySpringBean will be automatically autowired here. 
//Another possible approach is to use @Autowired and inject necessary bean one level higher
@Bean
public SchedulerFactoryBean scheduler(MySpringBean mySpringBean) {
    SchedulerFactoryBean sfb = new SchedulerFactoryBean();
    //default configuration goes here
    Map<String, Object> schedulerContext = new HashMap<>();
    schedulerContext.put("mySpringBean", mySpringBean);
    schedulerContext.setSchedulerContextAsMap(schedulerContext);
    return sfb;
}

And here is my Job code which suppose to use this spring bean: 这是我的Job代码,假设要使用此spring bean:

public class MyJob extends QuartzJobBean {
    private MySpringBean mySpringBean;

    public void setMySpringBean(MySpringBean mySpringBean) {
        this.mySpringBean = mySpringBean;
    }

    @Override
    public void executeInternal(JobExecutionContext jobContext) throws JobsExecutionException {
        //Job logic with usage of mySpringBean
    }
}

In my application Quartz jobs didn't require the whole application context - just 3 beans, so I decided to autowire certain beans instead of autowiring whole context 在我的应用程序中,Quartz作业不需要整个应用程序上下文-仅需要3个bean,因此我决定自动装配某些bean,而不是自动装配整个上下文

Hope this helps 希望这可以帮助

I find out what the problem was, in Spring config, several beans implementing FactoryBean were declared without using generics. 我发现了问题所在,在Spring config中,声明了几个实现FactoryBean的bean,而没有使用泛型。 Adding the generics solved the issue. 添加泛型即可解决此问题。

This was most likely non related to Quartz. 这很可能与Quartz无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM