java定时器

系统 1897 0

以前项目中写过类似的定时器,今天复习1下,自己建了个工程.

实现原理:创建servlet,应用服务器自动加载此servlet,在web.xml设置定时器的各个参数

开发工具:myeclipse6.0

应用服务器:tomcat6.0

1、创建web工程TestTimer

2、创建servlet=>com.billy.servlet.TestServlet.java

 

package com.billy.servlet;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.billy.Task;


public class TestServlet extends HttpServlet {
 
 private Timer timer1 = null;
 private Task task1;
 /**
  * Constructor of the object.
  */
 public TestServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
        if(timer1!=null){   
            timer1.cancel();   
        }  
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  System.out.println("doGet");
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  System.out.println("doPost");
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
  System.out.println("init");
  ServletContext context = getServletContext();
       
        // 定时器开关
        String startTask = getInitParameter("startTask");
        System.out.println(startTask);
       
        // 开始运行时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(getInitParameter("startTime")));
        Date time = calendar.getTime();

        // 缓冲时间(分钟)
        Long intervalTime = Long.parseLong(getInitParameter("intervalTime"));
        System.out.println(intervalTime);
       
       
        // 启动定时器
        if(startTask.equals("true")){
            timer1 = new Timer(true);
            task1 = new Task(context);
            timer1.schedule(task1, time, intervalTime * 1000 * 60);   
        }
 }

}

 

//Task为任务类

package com.billy;

import java.util.TimerTask;

import javax.servlet.ServletContext;

public class Task extends TimerTask{
 
 private ServletContext context;   
    static int i = 1;
 
    private static boolean isRunning = true;   
       
    public Task(ServletContext context){   
        this.context = context;
    }   
       
           
    @Override  
    public void run() {   
        if(isRunning){   

             //此处写自己需要循环的业务逻辑
              System.out.println("上传文件" + i);
              i++;
        }   
    }   

}

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns=" http://java.sun.com/xml/ns/j2ee "
 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
 xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.billy.servlet.TestServlet</servlet-class>
    <init-param>
    <!-- // 定时器开关  -->
     <param-name>startTask</param-name>
     <param-value>true</param-value>
    </init-param>
    <init-param>
    <!-- // 开始运行时间  HH-->
     <param-name>startTime</param-name>
     <param-value>12</param-value>
    </init-param>
    <init-param>
    <!-- // 缓冲时间  MM-->
     <param-name>intervalTime</param-name>
     <param-value>1</param-value>
    </init-param>
    <load-on-startup>300</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/wangweiTest</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

目录结构:


java定时器
 

java定时器


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论