我正在尝试使用@Scheduled功能。我已经遵循了this和this教程,但我无法执行我的计划任务。
我已经创建了一个worker:
@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");
}
}并在我的应用程序上下文中尝试了不同的连接。最终版本如下所示:
<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>服务器启动时没有任何错误。
我是不是遗漏了什么?
发布于 2013-05-17 14:56:17
<context:annotation-config />不检测beans它只处理已声明bean上的注释。这意味着您的@Service实际上并未转换为bean。
请改用<context:component-scan base-package="com.yourcomany" />。
发布于 2015-01-25 18:13:40
我面临着同样的问题,但原因是不同的。我不得不在我的顶级项目中添加以下内容:
<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.xsdhttps://stackoverflow.com/questions/16602277
复制相似问题