2013-05-17 35 views
0

我正在尝试使用@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> 

的服务器出任何错误启动。

我错过了什么吗?

回答

8

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

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

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