使用spring mvc安排任务

时间:2014-05-31 21:23:14

标签: java spring spring-mvc scheduled-tasks quartz-scheduler

我想在spring mvc项目中的每个特定时间运行以下方法它工作正常并打印第一个输出 但它没有访问数据库,所以它不显示列表

方法

 public class ScheduleService {

@Autowired
private UserDetailService userDetailService;

public void performService() throws IOException {
    System.out.println("first output");
    List<UserDetail> list=userDetailService.getAll();
    System.out.println(list);

}

配置文件

 <!-- Spring's scheduling support -->
<task:scheduled-tasks scheduler="taskScheduler">
   <task:scheduled ref="ScheduleService" method="performService" fixed-delay="2000"/>
</task:scheduled-tasks>

<!-- The bean that does the actual work -->
<bean id="ScheduleService" class="com.ctbllc.ctb.scheduling.ScheduleService" />

<!-- Defines a ThreadPoolTaskScheduler instance with configurable pool size. -->
<task:scheduler id="taskScheduler" pool-size="1"/>      

2 个答案:

答案 0 :(得分:1)

尝试这个(并从xml文件中删除bean定义):

@Component
public class ScheduleService {

    @Autowired
    private UserDetailService userDetailService;

    @Scheduled(fixedDelay = 2000L) // in msec
    public void performService() throws IOException {
        System.out.println("first output");
        List<UserDetail> list=userDetailService.getAll();
        System.out.println(list);

    }

}

答案 1 :(得分:0)

为该特定服务编写集成测试,并查看服务方法调用是否返回任何内容。手动测试总会导致这些问题。如有必要,请从测试和调试开始。