@Scheduled任务无法启动

时间:2013-05-17 05:50:59

标签: spring spring-mvc annotations quartz-scheduler

我正在尝试使用@Scheduled功能。我遵循了thisthis教程,但我无法执行计划任务。

我创建了一个工人:

@Component("syncWorker")
public class SyncedEliWorker implements Worker {
    protected Logger logger = Logger.getLogger(this.getClass());

    public void work() {
        String threadName = Thread.currentThread().getName();
        logger.debug("   " + threadName + " has began to do scheduled scrap with id=marketwatch2");
    }
}

和SchedulingService:

@Service
public class SchedulingService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    @Qualifier("syncWorker")
    private Worker worker;

    @Scheduled(fixedDelay = 5000)
    public void doSchedule() {
        logger.debug("Start schedule");

        worker.work();
        logger.debug("End schedule");
    }
}

在我的applicationcontext中尝试了不同的连线。 最终版本如下:

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

    <context:annotation-config/>

    <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
    <task:scheduler id="taskScheduler" pool-size="3"/>
    <task:executor id="taskExecutor" pool-size="3"/>

    ... Other beans...
</beans>

服务器启动时没有任何错误。

我错过了什么吗?

2 个答案:

答案 0 :(得分:8)

<context:annotation-config />没有检测到bean - 它只是处理声明的bean上的注释。这意味着你的@Service实际上并没有变成一个bean。

改为使用<context:component-scan base-package="com.yourcomany" />

答案 1 :(得分:1)

我遇到了同样的问题,但原因不同。 我必须在我的顶级项目中添加以下内容:

<task:annotation-driven />

添加时,请不要忘记在应用程序上下文中添加正确的位置:

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
相关问题