最近在做springmvc的项目,其中用到了定时任务,发上来,大家一起学习下。
在spring-mvc.xml文件中加入
1.xmlns:task=”http://www.springframework.org/schema/task
2.xsi:schemaLocation中加入
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
3.添加任务扫描注解
4.添加注解包
提供一个测试类
package com.ivox.wsms.quartz;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.ivox.wsms.entity.MessageInfo;
import com.ivox.wsms.service.MessageInfoService;
@Component
public class MyTask {
@Autowired
private MessageInfoService messageService;
@Scheduled(cron="0/10 * * * * ? ") //间隔5秒执行
public void taskCycle(){
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
List infos = messageService.queryMessageInfoByStartDate(format.format(new Date()));
System.out.println(infos.size());
}
}