Spring调度器不工作
我对 Spring 基于注释的任务调度程序有疑问 - 我无法让它工作,我在这里没有看到任何问题...
application-context.xml
<task:scheduler id="taskScheduler" />
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
bean
@Service
public final class SchedulingTest {
private static final Logger logger = Logger.getLogger(SchedulingTest.class);
@Scheduled(fixedRate = 1000)
public void test() {
logger.debug(">>> Scheduled test service <<<");
}
}
I have a problem with Spring's annotation based task scheduler - I can't get it working, I don't see any problem here...
application-context.xml
<task:scheduler id="taskScheduler" />
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
bean
@Service
public final class SchedulingTest {
private static final Logger logger = Logger.getLogger(SchedulingTest.class);
@Scheduled(fixedRate = 1000)
public void test() {
logger.debug(">>> Scheduled test service <<<");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
用于注释驱动任务的Spring @Configuration(非xml配置)
只需在 WebMvcConfig 类上添加 @EnableScheduling
Spring @Configuration (non-xml configuration) for annotation-driven tasks
Just add @EnableScheduling on your WebMvcConfig class
如果您想使用
task:annotation-driven
方法并且您的@Scheduled注释不起作用,那么您很可能在上下文xml中错过了context:component-scan
。如果没有这一行,spring 就无法猜测在哪里搜索你的注释。
If you want to use
task:annotation-driven
approach and your @Scheduled annotation is not working, then you most probably missedcontext:component-scan
in your context xml.Without this line, spring cannot guess where to search for your annotations.
发生这种情况是因为默认情况下 Spring 延迟初始化 bean。
来禁用 bean 的延迟初始化。
通过将此注释放在
@Component
顶部This is happening because by default Spring lazy initializes the beans.
Disable lazy initialization for the bean by placing this annotation
on top of your
@Component
.对我来说,Spring 5 中有效的解决方案是我必须将
@Component
添加到具有@Scheduled
注解方法的类中。For me the solution that worked in Spring 5 was that I had to add
@Component
to the class having@Scheduled
annotated methods.配置调度程序后,在主类中添加@EnableScheduling。
After configuring the Schedulers, add @EnableScheduling in your main class.
我终于找到了解决办法。
application-context.xml
和不带注释的
test()
方法。该方法每秒运行一次并且运行良好。I finally found a solution.
application-context.xml
and the
test()
method without the annotation. This runs the method every second and works perfectly.如果您有dispatcher-servlet.xml,请将您的配置移至此处。它对我有用,我在这篇文章中留下了评论:
https://stackoverflow.com/a/11632536/546130
if you have dispatcher-servlet.xml move your configuration there. it worked for me and i have left a comment in this article:
https://stackoverflow.com/a/11632536/546130
我的解决方案是在 applicationContext.xml: 中添加
以下 schemaLocation:
The solution for me was to add in the applicationContext.xml:
with the following schemaLocation:
您还应该检查该 bean 的lazy-init 是否为 false,或者在 bean 中使用
default-lazy-init="false"
。这解决了我的问题。
You should also check lazy-init to be false for that bean or use
default-lazy-init="false"
in beans.That solved my problem.
我必须使用
下面的 Bean 定义更新我的dispatcher-servlet.xml:
I had to update my dispatcher-servlet.xml with
Bean definition below:
我们有以下原因:
服务需要一个接口(由于 Transaction 注释)- IDE 也将此 tx 注释添加到接口中。但是 @Scheduled 是在实现服务类时 - Spring 忽略了它,因为它认为接口上只存在注释。所以要小心,只在实现类上有注释!
We had the following reason:
Service needed an interface (due to Transaction annotation) - IDE added this tx annotation also to interface. But @Scheduled was in implementing service class - and Spring ignored it since it thought that only annotations exist on the interface. So be careful to only have annotations on implementing classes!
只需在任何使用@Configuration注释的Spring Boot配置类中添加@EnableScheduling,并为运行计划作业的方法添加@Scheduled注释。
Just add @EnableScheduling at any spring boot configuration class annotated with @Configuration and for the method that run the schedule job add @Scheduled annotation.
也许这对某人有用。当我有一个在 PostConstruct 方法中执行长时间处理工作的 bean 时,我遇到了类似的问题。因此,Spring Boot 应用程序没有启动(因为 PostConstruct 方法正在进行中),这就是我的计划作业没有运行的原因(它们在应用程序启动后开始运行)。
Maybe it will be useful for someone. I ran into similar issue when I had a bean that did a long processing job in PostConstruct method. Thus, Spring Boot application didn't start (because PostConstruct method was in progress) and that's why my scheduled jobs didn't run (they start running after application startup).
如果您将 Grails 与 Spring Scheduler 一起使用,您将需要添加到类的顶部。
来源
If you are using Grails with Spring Scheduler you will need to add to the top of your class.
Source
在
@SpringBootApplication
之上添加@EnableScheduling
,并且您需要确保带有@Scheduled
的类标记有@Service 或
@Component
注释。Add
@EnableScheduling
on top of@SpringBootApplication
and you need to make sure your class with@Scheduled
is marked with@Service
or@Component
annotation.