spring mvc 定时器

package com.project.mobile.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.admin.service.ServiceOrderService;
import com.commons.util.DateUtil;

@Component
public class Task {
/**
* 服务订单Service
*/
@Resource(name="serviceOrderServiceImpl")
private ServiceOrderService serviceOrderService;

private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
@Scheduled(cron = "0 1 0 * * ? ")
public void testTask() throws ParseException{
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date parse = FORMAT.parse(FORMAT.format(date));
System.out.println("打印时间"+FORMAT.format(calendar.getTime()));
List<Map<String,Object>> selectAllEndTime = serviceOrderService.selectAllEndTime(parse);
if(CollectionUtils.isNotEmpty(selectAllEndTime)){
for (Map<String, Object> map : selectAllEndTime) {
serviceOrderService.updateServiceOrderStatus((String)map.get("id"), "9", null);
System.out.println(122222);
}
}
}
}

 需要定时执行的方法

 

 

在springMVC.xml 中 添加配置文件

beans 中添加 依赖xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd  

 

增加配置

<!-- 定时任务 -->
<context:component-scan base-package="com.project.mobile.util" use-default-filters="true">
</context:component-scan>
<task:annotation-driven></task:annotation-driven>

 

posted @ 2019-01-10 13:58  小洺  阅读(147)  评论(0)    收藏  举报