0

I am now writing a quartz job to start at 3:00 am every day to save data in database and redis. But now I got a problem and can't get it solved even though I tried a lot of times.

The job:

public class QuartzScheduler implements ServletContextListener {

private static Logger logger = Logger.getLogger(QuartzScheduler.class);

@Autowired
private ExchangeRateConfigBean exchangeRateConfigBean;

private  Scheduler scheduler = null;

@Override
public void contextInitialized(ServletContextEvent servletContext) {

    try {
        ExchangeRateConfig exchangeRateConfig = exchangeRateConfigBean.setUpConfigurationFromConfigFile();

        if(!exchangeRateConfig.getJob_fire()) {
            logger.info("ExchangeRate Job Info: Current Exchange Rate Job flag is not open. Wont start the job!");
            return;
        }
    } catch (Exception e) {
        logger.error("ExchangeRate Job Info: Something wrong when analyzing config.properties file!");
        logger.error(e.getMessage());
        logger.error(e.getStackTrace());
        logger.error(e.getCause());
        return;
    }


    try {

        scheduler = StdSchedulerFactory.getDefaultScheduler();

        scheduler.start();

        logger.info("ExchangeRate Job Info: Exchange Rate job starting......");
    } catch (SchedulerException ex) {
        logger.error(ex);
    }
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    try {
        scheduler.shutdown();
    } catch (SchedulerException e) {
        logger.error(e);
    }
}

In this job , I autowired the component ExchangeRateConfigBean, but when I debugged in, I saw that the instance is null.

the spring config below:

  <bean id="exchangeRateConfigBean" class="com.letv.exchangerate.bean.impl.ExchangeRateConfigBeanImpl">
    <constructor-arg ref="exchangeRateErrorBean" />
</bean>
<bean id="exchangeRateErrorBean" class="com.letv.exchangerate.bean.impl.ExchangeRateErrorBeanImpl">
</bean>

The config Bean below:

public class ExchangeRateConfigBeanImpl implements ExchangeRateConfigBean {

public ExchangeRateConfigBeanImpl(){}

public ExchangeRateConfigBeanImpl(ExchangeRateErrorBean exchangeRateErrorBean)
{
    this.exchangeRateErrorBean = exchangeRateErrorBean;
}

private static Logger logger = Logger.getLogger(ExchangeRateConfigBeanImpl.class.getClass());

private ExchangeRateErrorBean exchangeRateErrorBean;

@Override
public ExchangeRateConfig setUpConfigurationFromConfigFile() throws IOException {

    InputStream inputStream = null;
    ExchangeRateConfig exchangeRateConfig = null;

    try {

        Properties prop = new Properties();
        String propFileName = "config.properties";

        inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
        exchangeRateConfig = new ExchangeRateConfig();

        if (inputStream != null) {
            prop.load(inputStream);
        } else {
            logger.error("property file '" + propFileName + "' not found in the classpath");
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }

        String job_fire_tmp = prop.getProperty("job_fire");
        exchangeRateConfig.setJob_fire(job_fire_tmp.isEmpty() ? false : Boolean.parseBoolean(job_fire_tmp));

        return exchangeRateConfig;

    } catch (IOException e) {
        logger.error(e.getStackTrace());
        return exchangeRateConfig;
    } finally {
        inputStream.close();
    }
}

In the config bean, I used the constructor injection to inject the error bean above. the error bean's code lists below:

public class ExchangeRateErrorBeanImpl implements ExchangeRateErrorBean{

public ExchangeRateErrorBeanImpl(){}

@Override
public ExchangeRateError getExchangeRateError(String content) {

    if (content.isEmpty())
        return null;
    if (!content.contains(":"))
        return null;

    String[] strArray = content.split(":");

    return new ExchangeRateError(strArray[0], strArray[1]);
}

}

Anyone can help? why I autowired the Config bean in the job class, it will create null instance? thx.

And before I post this, I have read this in detail, Why is my Spring @Autowired field null? but it didn't save my time.

Community
  • 1
  • 1
CharlieShi
  • 888
  • 3
  • 17
  • 43

1 Answers1

0

Your code shows QuartzScheduler as an instance of ServletContextListener. This will only create a Java bean. Your Quartz Scheduler class must be a bean in a Spring applicationContext. Only then will the other dependencies be autowired. Refer this for example - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-quartz

Shankar
  • 2,625
  • 3
  • 25
  • 49