码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2 设置global timer

时间:2015-04-14 19:17:05      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

设置全局的timer需要在web.xml中添加servlet, 并设置load-on-startup 为 1, 然后在servlet的init()中开启timer, 具体代码如下:

1. web.xml

<servlet>
	<servlet-name>backup</servlet-name>
	<servlet-class>locationService.backup.Backup</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

2. Backup.java

public class Backup extends HttpServlet {
	  private static final long serialVersionUID = 1L;
	  private static final Logger logger = Logger.getLogger(Backup.class);
	  public void init() throws ServletException
	    {

			TimerTask bakcupTask = new TimerTask(){
		        public void run(){
		        	try {
						backup();
					} catch (Exception e) {
						
						e.printStackTrace();
					}
		        }
		    };
		    
		    /* Schedule the task to be run every 24 hours */
		    Timer time = new Timer();
		    time.scheduleAtFixedRate(bakcupTask, new Date(),1000*60*60*24);

	    }
   ...

  

Struts2 设置global timer

标签:

原文地址:http://www.cnblogs.com/codingforum/p/4425610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!