java web定时器Timer


public class AutoRun implements ServletContextListener {
protected final Log log = LogFactory.getLog(getClass());
private Timer timer = null;
private static TGroupBuyService service;
private static ApplicationContext context;

public void contextInitialized(ServletContextEvent arg0) {
context = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
service =(TGroupBuyService)context.getBean("groupBuyService");
// TODO Auto-generated method stub
timer = new Timer(true);
try {

// 一天的毫秒数
long daySpan = 24 * 60 * 60 * 1000;

// 规定的每天时间10:00:00运行
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd '10:00:00'");
// 首次运行时间
Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(new Date()));

// 如果今天的已经过了 首次运行时间就改为明天
if(System.currentTimeMillis() > startTime.getTime())
startTime = new Date(startTime.getTime() + daySpan);

// 以每24小时执行一次
timer.scheduleAtFixedRate(new MyTask(), startTime, daySpan);
} catch (Exception e) {
e.printStackTrace();
}
//timer .cancel();
}

public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub


}

class MyTask extends java.util.TimerTask {
public void run() {
log.info("--------------每天10点钟更新--------------");
String[] id_Array = {"1","2","3"};
String[] total_Array = {"10","6","6"};
boolean flag = service.updateRemainder(id_Array, total_Array);
if(flag){
log.info("--------------定时器Job更新成功--------------");
}else{
log.info("--------------定时器Job更新失败--------------");
}
}
}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值