为什么 Spring 不运行我的 @Scheduled 方法? [英] Why isn't Spring running my @Scheduled method?

查看:25
本文介绍了为什么 Spring 不运行我的 @Scheduled 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 methods annotated with @Scheduled are being executed.

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

I have invoked Spring's task magic with the following declarations:

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

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

And I have an interface that looks something like this:

package com.mypackage;

public interface MyInterface {

    public void executePeriodically();
}

对应的实现如下:

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 秒告诉我几点……但实际上我什么也没得到.我已经尝试在接口方法和 impl 方法上使用注释,但这似乎并没有改变任何东西.

Now the expected result is that I have a very noisy little guy telling me what time it is every 5 seconds...but in reality I get absolutely nothing. I've tried with the annotation on the interface method and on the impl method, but that doesn't seem to change anything.

我确定正在初始化执行程序和调度程序,因为我的日志中有以下内容:

I know for sure that the executor and scheduler are being initialized because I have the following in my log:

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)

我不确定关于不符合条件的那句话是相关的还是红鲱鱼.

I'm not sure if that line about not being eligible is relevant or a red herring.

目前,我正在通过像这样声明我的计划任务来解决这个问题:

At the moment, I'm working around it by declaring my scheduled tasks like so:

<task:scheduled-tasks>
  <task:scheduled ref="sourceDocumentManagerImpl" method="deleteOldDocuments" cron="0 0 * * * ?"/>
</task:scheduled-tasks>

虽然这工作得很好,但我更愿意使用注释,因为直接在代码中查看该方法的期望值要方便得多.有谁知道我可能做错了什么?为了记录,我使用的是 Spring 3.0.4

While this works perfectly fine, I'd much rather use the annotations because it's so much more convenient to see directly in the code what the expectations are for that method. Anyone know what I could be doing wrong? For the record, I'm using Spring 3.0.4

非常感谢!

推荐答案

所以...看起来 Spring 3.0.x(至少 3.0.4 和 3.0.5)中存在一个问题,与发现有关AOP 代理上的 @Scheduled 注释.我有一个切入点声明用事务建议包装 @Scheduled 方法,这似乎是问题的根源.如果我删除建议,作业就会运行.如果我重新添加它,Spring 将无法为我的带注释的方法找到并创建任务.

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.

所以,我要去向 Spring 人员提交一个错误,同时我被困在手动声明我的任务中.

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

这篇关于为什么 Spring 不运行我的 @Scheduled 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆