Spring调度器不工作

发布于 2024-10-14 17:45:35 字数 608 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

断桥再见 2024-10-21 17:45:35

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

只需在 WebMvcConfig 类上添加 @EnableScheduling


    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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

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

Just add @EnableScheduling on your WebMvcConfig class


    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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

攀登最高峰 2024-10-21 17:45:35

如果您想使用task:annotation-driven方法并且您的@Scheduled注释不起作用,那么您很可能在上下文xml中错过了context:component-scan
如果没有这一行,spring 就无法猜测在哪里搜索你的注释。

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

If you want to use task:annotation-driven approach and your @Scheduled annotation is not working, then you most probably missed context:component-scan in your context xml.
Without this line, spring cannot guess where to search for your annotations.

<context:component-scan base-package="..." />
呆头 2024-10-21 17:45:35

发生这种情况是因为默认情况下 Spring 延迟初始化 bean。

来禁用 bean 的延迟初始化。

@Lazy(false)

通过将此注释放在@Component 顶部

This is happening because by default Spring lazy initializes the beans.

Disable lazy initialization for the bean by placing this annotation

@Lazy(false)

on top of your @Component.

Bonjour°[大白 2024-10-21 17:45:35

对我来说,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.

神爱温柔 2024-10-21 17:45:35

配置调度程序后,在主类中添加@EnableScheduling
**enter图片描述在这里**

After configuring the Schedulers, add @EnableScheduling in your main class.
**enter image description here**

月牙弯弯 2024-10-21 17:45:35

我终于找到了解决办法。

application-context.xml

<bean id="schedulingTest" class="...SchedulingTest" />

<task:scheduled-tasks>
    <task:scheduled ref="schedulingTest" method="test" cron="* * * * * ?"/>
</task:scheduled-tasks>

和不带注释的 test() 方法。该方法每秒运行一次并且运行良好。

I finally found a solution.

application-context.xml

<bean id="schedulingTest" class="...SchedulingTest" />

<task:scheduled-tasks>
    <task:scheduled ref="schedulingTest" method="test" cron="* * * * * ?"/>
</task:scheduled-tasks>

and the test() method without the annotation. This runs the method every second and works perfectly.

鱼忆七猫命九 2024-10-21 17:45:35

如果您有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

2024-10-21 17:45:35

我的解决方案是在 applicationContext.xml: 中添加

<task:annotation-driven/>

以下 schemaLocation:

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd

The solution for me was to add in the applicationContext.xml:

<task:annotation-driven/>

with the following schemaLocation:

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
风追烟花雨 2024-10-21 17:45:35

您还应该检查该 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.

你的心境我的脸 2024-10-21 17:45:35

我必须使用

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.3.xsd"></beans>

下面的 Bean 定义更新我的dispatcher-servlet.xml:

<bean id="scheduledTasks" class="com.vish.services.scheduler.ScheduledTasks"></bean>

I had to update my dispatcher-servlet.xml with

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.3.xsd"></beans>

Bean definition below:

<bean id="scheduledTasks" class="com.vish.services.scheduler.ScheduledTasks"></bean>
南薇 2024-10-21 17:45:35

我们有以下原因:
服务需要一个接口(由于 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!

清眉祭 2024-10-21 17:45:35

只需在任何使用@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.

东北女汉子 2024-10-21 17:45:35

也许这对某人有用。当我有一个在 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).

瞳孔里扚悲伤 2024-10-21 17:45:35

如果您将 Grails 与 Spring Scheduler 一起使用,您将需要添加到类的顶部。

static lazyInit = false

来源

If you are using Grails with Spring Scheduler you will need to add to the top of your class.

static lazyInit = false

Source

半边脸i 2024-10-21 17:45:35

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文