1,以常用的订单管理为例,业务需要:为创建时间超过10天的订单记录添加自动清除功能。
2,实现思路:自定义MyServletContextListener 与MytimerTask 分别继承ServletContextListener 、TimeTask,然后在MytimeTask的run函数里添加要完成的业务逻辑。
3,实现代码:
package com.huiyou.utils;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener{
// 应用监听器的销毁方法
public void contextDestroyed(ServletContextEvent event) {
// ServletContext sc = event.getServletContext();
// 在整个web应用销毁之前调用,将所有应用空间所设置的内容清空
System.out.println("销毁工作完成...");
}
// 应用监听器的初始化方法
public void contextInitialized(ServletContextEvent event) {
// 通过这个事件可以获取整个应用的空间
// 在整个web应用下面启动的时候做一些初始化的内容添加工作
//timer.schedule(new MyTask(),long time1,long timer2);
Timer timer = new Timer();
timer.schedule(new StatisticsTask(event.getServletContext()), 0, 60*60*1000);//0表示没有延迟,每隔1小时
// 1.第一个参数为继承TimerTask类的实现类,并实现run()方法,因为TimerTask类也实现了Runnable接口;
// firstTime为Date类型,period为long
// 从firstTime时刻开始,每隔period毫秒执行一次。
// 3.第三个参数,意思是重复调用间隔,不设置的话,不会重复调用。
}
}
package com.huiyou.utils;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.huiyou.domain.Mall_order;
import com.huiyou.service.Mall_orderService;
import com.huiyou.vo.Mall_orderVo;
public class MytimerTask extends TimerTask
{
private static boolean isRunning = false;
private ServletContext context = null;
@Autowired//为了解决service没能实例问题
private Mall_orderService mall_orderService;
public StatisticsTask(ServletContext context)
{
this.context = context;
}
@Override
public void run()
{
if (!isRunning)
{
isRunning = true;
context.log("开始执行指定任务");
//TODO 添加自定义的详细任务
executeTask();
//指定任务执行结束
isRunning = false;
context.log("指定任务执行结束");
}
else
{
context.log("上一次任务执行还未结束");
}
}
public void executeTask() {
// TODO Auto-generated method stub
//订单超时清除管理
Thread t = new Thread(new Runnable(){
public void run(){
// run方法具体重写
Mall_orderService mall_orderService = (Mall_orderService)ApplicationContextUtil.getBean("select_order");//为了解决service没能实例问题
Mall_orderVo mall_orderVo = new Mall_orderVo();
mall_orderVo.setStatus(1);
List<Mall_order>list=mall_orderService.select_order(mall_orderVo);
for(Mall_order mall_order:list){
Date acceptdate = mall_order.getCreate_time();
long acceptdate1 = acceptdate.getTime();
Date now= new Date();
long now_time=now.getTime();
long diff= now_time-acceptdate1;
if(diff>86400000){ //订单超过一天后自动删除
String id=mall_order.getOrder_no();
mall_orderService.delete_order(id);
}
else{
}
}
//订单7天未确认后自动收货管理
mall_orderVo.setStatus(3);
list=mall_orderService.select_order(mall_orderVo);
for(Mall_order mall_order:list){
Date acceptdate = mall_order.getCreate_time();
long acceptdate1 = acceptdate.getTime();
Date now= new Date();
long now_time=now.getTime();
long diff= now_time-acceptdate1;
if(diff>604800000){ //订单超过七天后自动删除
Mall_order mall_order2= new Mall_order();
mall_order2.setOrder_no(mall_order.getOrder_no());
mall_order2.setStatus(4);//状态变为已收货
mall_orderService.update_order_status(mall_order2);
}
}
}});
t.start();
}
}
4,还有用quartz来实现定时的,现在用的挺多。。
补:1,在springmvc.xml中配置<bean id ="applicationContextUtil" class ="com.huiyou.utils.ApplicationContextUtil" ></bean>
2,引入工具类
package com.huiyou.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
3,service类实现类