标签:
1.在web.xml中添加servlet
<servlet> <servlet-name>StatisticInitServlet</servlet-name> <servlet-class>com.jovision.servlet.StatisticInitServlet</servlet- class> <load-on-startup>1</load-on-startup> 该servlet加载顺序,设为1,数字越小,代表加载时间越早 </servlet>
2.在servlet的init()方法中添加定时任务
public class StatisticInitServlet extends HttpServlet { private static Logger logger = Logger.getLogger(StatisticInitServlet.class); /*每日23点40分执行定时任务*/ public void init() throws ServletException { final Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 40); calendar.set(Calendar.SECOND, 00); Date time = calendar.getTime(); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { //统计当天新增设备数和新增用户数 daydeviceNum(); dayuserNum(); deleteRedisStatistic(); } //1000 * 60 * 60 * 24 }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行 } }
标签:
原文地址:http://www.cnblogs.com/fengyefeiluo/p/5035911.html