Spring cron job not working

13,275

Solution 1

You will need to add few things in your xml namespace and schema location. See this guide.

Solution 2

Did you @EnableScheduling (or the Xml equivalent)? See this guide for more detail.

Share:
13,275

Related videos on Youtube

xsiraul
Author by

xsiraul

Updated on September 15, 2022

Comments

  • xsiraul
    xsiraul over 1 year

    I am newbie with Spring framework. At the moment I try to integrate cron job service.

    I defined a service class as:

    package com.test.cron;
    @Service
    public class CronJob {
        protected static final Logger logger = Logger.getLogger(PasswordRemindFlusher.class);
    
        @Scheduled(cron="0 0/2 * * * ?")
        public void demoServiceMethod()
        {
    
            logger.debug("Cron job started.");
        }
    }
    

    Then I defined in servlet config:

    <context:component-scan base-package="com.test.cron" />
    

    spring-servlet.xml:

    <?xml  version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    <context:annotation-config />
    
    <context:component-scan base-package="com.test.cron" />
    <task:annotation-driven />
    </beans>
    

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet> 
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    Exception:

    SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
        at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
    

    All other controllers in the same package works. I use Spring 3.1.2 framework. May I missed something?

    • Dave Syer
      Dave Syer about 10 years
      You appear to be loading the same XML file twice. Once should do (if you remove the ContextLoaderListener).
  • xsiraul
    xsiraul about 10 years
    If I try to add: <task:annotation-driven /> , I receive an exception, because in XML is written <context:annotation-config /> . If I delete <task:annotation-drive />, I don't receive error.
  • xsiraul
    xsiraul about 10 years
    If I try to add: <task:annotation-driven /> , I receive an exception, because in XML is written <context:annotation-config /> . If I delete <task:annotation-drive />, I don't receive error.
  • Dave Syer
    Dave Syer about 10 years
    That doesn't make much sense. Can you show the exception. BTW, there's no need to reply the same way to more than one answer (even if they are the same) - we can all read all the comments.