Why isn't Spring running my @Scheduled method?

前端 未结 8 1617
天涯浪人
天涯浪人 2020-12-29 03:18

I\'m a bit befuddled because I\'m trying use use @Scheduled annotations, but Spring doesn\'t seem to be finding my methods. The end result is that, none of my m

相关标签:
8条回答
  • 2020-12-29 03:54

    In spite of that I am using Spring 3.1.2, I am running into the same problem in case i place my executor and scheduler tag in ApplicationContext.xml. i have two xml configuration file for spring in my project which are :

    1. applicationContext.xml
    2. dispatcher-servlet.xml

    So try to move your configuration to the last configuration file which spring reads. in my case it is start working by moving my configuration to dispatcher-servlet.xml

    here is my example:

    applicationContext.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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context" 
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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">
    
        <bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="location" value="/WEB-INF/configuration.properties" />
        </bean>
    
        <!-- To fix the problem with Unicode characters in ajax responses -->
        <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <array>
                    <bean class = "org.springframework.http.converter.StringHttpMessageConverter">
                        <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
                    </bean>
                    <bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                </array>
            </property>
        </bean>
    
        <!-- Hibernate Configuration
            p:driverClassName="com.mysql.jdbc.Driver"
            p:url="jdbc:mysql://localhost:3306/iss"
            p:username="root"
            p:password="root"
        -->
        <bean name="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
              p:driverClassName="org.postgresql.Driver"
              p:url="jdbc:postgresql://localhost:5432/iss"
              p:username="postgres"
              p:password=""
    
        />
    
        <!--<bean name="SessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean">-->
        <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="DataSource" />
            </property>
            <property name="hibernateProperties">
                <props>
                    <!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>-->
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <!--<prop key="hibernate.current_session_context_class">thread</prop>-->
                    <!--<prop key="hibernate.current_session_context_class">managed</prop>-->
                    <!--<prop key="hibernate.search.default.indexBase">/tmp/hibernate/indexes/</prop>-->
                    <!--<prop key="hibernate.flushMode">AUTO</prop>-->
                    <prop key="hibernate.connection.useUnicode">true</prop>
                    <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                    <prop key="hibernate.cache.use_second_level_cache">false</prop>
                    <prop key="hibernate.cache.use_query_cache">false</prop>
                    <prop key="hibernate.connection.autocommit">false</prop>
                </props>
            </property>
            <property name="packagesToScan" value="iss.DB" />
        </bean>
    
        <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <mvc:interceptors>
            <bean class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
                <property name="sessionFactory" ref="sessionFactory" />
            </bean>
        </mvc:interceptors>
    
        <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
    
    
    <!-- in this file it wont work
            <task:executor id="myExecutor" pool-size="1" />
        <task:scheduler id="myScheduler" pool-size="1" />
        <task:annotation-driven 
            executor="myExecutor"
            scheduler="myScheduler"/>    
    -->
    </beans>
    

    dispatcher-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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" 
           xmlns:context="http://www.springframework.org/schema/context">
    
        <bean class="org.springframework.web.servlet.mvc.support.AnnotationControllerTypePredicate"/>
    
        <context:component-scan base-package="iss"/>
        <context:component-scan base-package="com.hazhir"/>
    
        <!--Internationalization -->
        <bean id="messageSource"
              class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="iss.languages.text" />
        </bean>
    
        <bean id="localChangeInterseptor"
              class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="language" />
        </bean>
    
        <bean id="localeResolver"
              class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
            <property name="defaultLocale" value="en_US" />
            <property name="cookieName" value="clientLanguage" />
            <property name="cookieMaxAge" value="99999999"/>
        </bean>
    
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="alwaysUseFullPath" value="true" />
            <property name="interceptors">
                <list>
                    <ref bean="localChangeInterseptor" />
                </list>
            </property>
        </bean>
    
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp">
            <property name="exposeContextBeansAsAttributes" value="true" />
        </bean>
    
        <!-- Multipart form data -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="1000000000" />
        </bean>
    
        <context:annotation-config />
    
        <!-- here is a right place for such configuration
            -->
        <task:executor id="myExecutor" pool-size="1" />
        <task:scheduler id="myScheduler" pool-size="1" />
        <task:annotation-driven 
            executor="myExecutor"
            scheduler="myScheduler"/> 
    </beans>
    

    hope it helps.

    0 讨论(0)
  • 2020-12-29 03:56

    So...it looks like there's a problem in Spring 3.0.x (at the very least 3.0.4 and 3.0.5) having to do with discovering @Scheduled annotations on AOP proxies. I've got a pointcut declaration wrapping the @Scheduled method with transactional advice, and that seems to be the root of the problem. If I remove the advice, the job runs. If I add it back in, Spring fails to find and create a task for my annotated method.

    So, I'm going to go file a bug with the Spring folks, and in the meantime I'm stuck declaring my tasks manually.

    0 讨论(0)
  • 2020-12-29 03:58

    The only difference that I see from my setup (which works), is that my class is annotated with @Component rather than @Service. Other things to check:

    • whether you have the proper jars on the classpath (spring-context, I think)
    • put a breakpoint and check whether it really isn't executing
    • double-check the cron expression (I admit I always consult the docs for that). Or use a fixed delay, just to check if it works
    • try upgrading to 3.0.5 or the latest 3.1 snapshot.
    0 讨论(0)
  • 2020-12-29 03:59

    add "task:annotation-driven" ?

    <beans> <!-- XMLNS, XSD declarations omitted for brevity -->
    
      <context:component-scan base-package="com.mypackage"/>
      <task:annotation-driven/>
      <task:executor id="executor" pool-size="5"/>
      <task:scheduler id="scheduler" pool-size="5"/>
      <task:annotation-driven scheduler="scheduler" executor="executor"/>
    
    </beans>
    

    reference http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/

    or

    Spring @Configuration (non-xml configuration) for annotation-driven tasks

    Just add @EnableScheduling on you WebMvcConfig class

    @Configuration
    @EnableWebMvc
    @EnableAsync
    @EnableScheduling
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
       /** Annotations config Stuff ... **/
    }
    

    reference Spring Scheduler does not work

    0 讨论(0)
  • 2020-12-29 03:59

    I fixed it by adding default-lazy-init="false" to my applicationContext.xml.

    <beans .....
        **default-lazy-init="false"**>
    
    0 讨论(0)
  • 2020-12-29 04:02

    This is pretty old question but since this keeps up coming in google search results let me add the solution that worked for me in Spring 5. For me it didn't work until I added @Component to the class having @Scheduled annotated methods.

    0 讨论(0)
提交回复
热议问题