简体   繁体   中英

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. Note that the job makes an explicit call to applicationContext.getBean(...) in its execution.

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

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:

//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:

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

Hope this helps

I find out what the problem was, in Spring config, several beans implementing FactoryBean were declared without using generics. Adding the generics solved the issue.

This was most likely non related to Quartz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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