Java定时任务–定时调用方法(servlet)

Java 发表评论
package sdcncsi.ict.site.cms.ymjth;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import sdcncsi.ict.site.cms.template.CmsHtml;
/**
 * 网站首页定时生成
 * @author Administrator
 *
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public IndexServlet() {
		super();
	}
    
	public void destroy() {
		super.destroy(); 
	}
   
	public void init() throws ServletException {
		System.out.println("加载网站首页生成任务");
		this.timerTask();
	}
	
	public static void timerTask() {
		new Timer().schedule(new TimerTask(){
			@Override
			public void run() {
				CmsHtml cmsHtml = new CmsHtml();
				Ymjth ymjth = new Ymjth();
				ymjth.syjthHtml("D:\\tomcat_dzzw\\webapps\\dzzw","/dzzw",cmsHtml,"0", "1");
				System.out.println("新方式生成网站首页成功-----------------------------");
			}
		},new Date(),60000);
   }
}

 

请在web.xml配置下面参数

<servlet>
        <servlet-name>IndexServlet</servlet-name>
        <servlet-class>sdcncsi.ict.site.cms.ymjth.IndexServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
</servlet>

<!– 网站首页定时生成 –>
<servlet-mapping>
     <servlet-name>IndexServlet</servlet-name>
     <url-pattern>/servlet/IndexServlet</url-pattern>
</servlet-mapping>

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *