为什么不是Spring运行我

为什么不是Spring运行我

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

问题描述

我有点糊涂,因为我想用使用 @Scheduled 注释,但春天似乎并没有被找到我的方法。最终的结果是,正在执行的没有我的方法与 @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();
}

通过相应的IMPL是这样的:

With a corresponding impl like this:

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>

虽然这工作完全正常,我宁愿使用注解,因为它是如此方便多了,直接看到code什么期望该方法。任何人都知道我可以做错了什么?为了记录在案,我使用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

感谢一大堆!

推荐答案

所以...它看起来像有春天3.0.x的一个问题(至少是3.0.4和3.0.5)具有发现做在AOP代理 @Scheduled 注释。我有一个切入点声明包裹 @Scheduled 法事务的意见,这似乎是问题的根源。如果我删除的意见,作业运行。如果我早在添加它,春没有找到和我的批注的方法创建一个任务。

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.

所以,我要去一个bug与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的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:51