首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Spring不运行我的@Scheduled方法?

为什么Spring不运行我的@Scheduled方法?
EN

Stack Overflow用户
提问于 2011-05-27 23:54:23
回答 8查看 53.4K关注 0票数 31

我有点困惑,因为我正在尝试使用use Spring,但是@Scheduled似乎没有找到我的方法。最终的结果是,我使用@Scheduled注释的方法都没有被执行。

我使用以下声明调用了Spring的任务魔术:

代码语言:javascript
复制
<beans> <!-- XMLNS, XSD declarations omitted for brevity -->

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

  <task:executor id="executor" pool-size="5"/>
  <task:scheduler id="scheduler" pool-size="5"/>
  <task:annotation-driven scheduler="scheduler" executor="executor"/>

</beans>

我有一个界面,看起来像这样:

代码语言:javascript
复制
package com.mypackage;

public interface MyInterface {

    public void executePeriodically();
}

对应的impl如下:

代码语言:javascript
复制
package com.mypackage.impl;
// imports omitted for brevity

@Service
public class MyInterfaceImpl implements MyInterface {

    @Scheduled(cron = "0/5 * * * * ?")
    public void executePeriodically() {
        System.out.println("The time is now : " + new Date());
    }
}

现在预期的结果是,我有一个非常嘈杂的小家伙告诉我现在是什么时间,每隔5个seconds...but,实际上我什么也得不到。我已经尝试了在接口方法和impl方法上使用注释,但这似乎没有改变任何事情。

我确信executor和scheduler正在初始化,因为我的日志中有以下内容:

代码语言:javascript
复制
INFO  - ThreadPoolTaskExecutor     - Initializing ExecutorService 
INFO  - XmlWebApplicationContext   - Bean 'executor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO  - XmlWebApplicationContext   - Bean 'executor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO  - ThreadPoolTaskScheduler    - Initializing ExecutorService  'scheduler'
INFO  - XmlWebApplicationContext   - Bean 'scheduler' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

我不确定这句关于不符合条件的话是相关的还是无关紧要的。

目前,我正在通过如下方式声明我的计划任务来解决这个问题:

代码语言:javascript
复制
<task:scheduled-tasks>
  <task:scheduled ref="sourceDocumentManagerImpl" method="deleteOldDocuments" cron="0 0 * * * ?"/>
</task:scheduled-tasks>

虽然这可以很好地工作,但我更喜欢使用注释,因为在代码中直接看到对该方法的期望要方便得多。有人知道我会做错什么吗?根据记录,我使用的是Spring 3.0.4

非常感谢!

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-05-29 02:58:21

在Spring3.0.x(至少是3.0.4和3.0.5)中,在发现So...it代理上的@Scheduled注释时,似乎有一个问题。我有一个用事务性建议包装@Scheduled方法的切入点声明,这似乎是问题的根源。如果我删除通知,作业就会运行。如果我将其添加回去,Spring将无法为我的带注释的方法找到并创建任务。

所以,我要去向Spring的人提交一个bug,在此期间,我不得不手动声明我的任务。

票数 14
EN

Stack Overflow用户

发布于 2014-09-25 17:48:34

添加“任务:注释驱动”?

代码语言:javascript
复制
<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>

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

用于注释驱动任务的Spring @Configuration (非xml配置)

只需在WebMvcConfig类上添加@EnableScheduling

代码语言:javascript
复制
@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
   /** Annotations config Stuff ... **/
}

参考Spring Scheduler does not work

票数 21
EN

Stack Overflow用户

发布于 2013-03-18 02:52:02

我通过在我的applicationContext.xml中添加default-lazy-init="false“修复了这个问题。

代码语言:javascript
复制
<beans .....
    **default-lazy-init="false"**>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6154741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档