Spring 调度程序不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4817414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:17:51  来源:igfitidea点击:

Spring Scheduler does not work

springspring-mvcscheduling

提问by user219882

I have a problem with Spring's annotation based task scheduler - I can't get it working, I don't see any problem here...

我对 Spring 基于注释的任务调度程序有问题 - 我无法让它工作,我在这里看不到任何问题......

application-context.xml

应用程序上下文.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 <<<");
    }

}

回答by ahll

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

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

Just add @EnableScheduling on your WebMvcConfig class

只需在您的 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 ... **/
    }

回答by Serkan Ar?ku?u

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

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

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

回答by Babken Vardanyan

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

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

Disable lazy initialization for the bean by placing this annotation

通过放置此注释禁用 bean 的延迟初始化

@Lazy(false)

on top of your @Component.

在您的@Component.

回答by user219882

I finally found a solution.

我终于找到了解决方案。

application-context.xml

应用程序上下文.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.

test()没有注释的方法。这每秒运行该方法并且完美运行。

回答by Hazhir

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

如果您有 dispatcher-servlet.xml 将您的配置移到那里。它对我有用,我在这篇文章中发表了评论:https: //stackoverflow.com/a/11632536/546130

回答by nabeel

For me the solution that worked in Spring 5 was that I had to add @Componentto the class having @Scheduledannotated methods.

对我来说,在 Spring 5 中起作用的解决方案是我必须添加@Component到具有@Scheduled注释方法的类中。

回答by pasindupa

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

配置调度程序后,在主类中添加@EnableScheduling**enter image description here**

回答by Amir M

You should also check lazy-init to be false for that bean or use default-lazy-init="false"in beans.

您还应该检查lazy-init 是否为该bean 的false 或default-lazy-init="false"在bean 中使用。

That solved my problem.

那解决了我的问题。

回答by Joao Gavazzi

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

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

<task:annotation-driven/>

with the following schemaLocation:

具有以下 schemaLocation:

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

回答by vishsodhani

I had to update my dispatcher-servlet.xml with

我不得不更新我的 dispatcher-servlet.xml

<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定义如下:

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