zoukankan      html  css  js  c++  java
  • httpServlet,java web后台服务

    1,定时执行的类

    package com.utils;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public
    class MyTimer extends Thread { // 间隔时间:小时 private int intervalHours; // 误差(操作所需时间可能导致误差):分钟 private int deviationMinute; // 执行时间 private String runTime; public MyTimer(){ // 参数取得 intervalHours = 24; deviationMinute = 5; runTime= "19:30"; } public void run() { while (!this.isInterrupted()) {// 线程未中断执行循环 // 获取当前时间:HH:mm Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm"); String hours = dateFormat.format(now); // 时间到达 if(hours.equals(runTime)){ doRun(); try { // 长时间休眠:间隔时间-误差 Thread.sleep(intervalHours * 3600 * 1000 - deviationMinute * 60 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } }else{ try { // 等待间隔:30s Thread.sleep(30000); System.out.println(hours); } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 处理 */ private void doRun() { } }

     

    2,servlet

    package com.utils;
    
    import javax.servlet.http.HttpServlet;
    
    public class MyServlet extends HttpServlet{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private MyTimer myTimer;  
        public MyServlet(){  
            }  
    
        public void init(){  
            String str = null;  
            if (str == null && myTimer== null) {  
                myTimer=  new MyTimer();
                
                myTimer.start(); 
            }
         super.init(); }
    public void destory(){ if (myTimer!= null && myTimer.isInterrupted()) { myTimer.interrupt(); }
         super.destroy(); } }


    3,web.xml

        <servlet>
            <servlet-name>MyServlet</servlet-name>
            <servlet-class>com.utils.MyServlet</servlet-class>
            <load-on-startup>9</load-on-startup
      </servlet
    >
  • 相关阅读:
    win7同时安装python2和python3
    Centos6.8安装python3.6
    Typescript的接口
    ES5中的类相关/Typescript的类相关
    Typescript介绍
    Global Interpreter Lock 全局解释器锁
    Go语言设计模式(五)
    Go语言设计模式(四)
    Go语言反射
    Go语言程序设计(三)
  • 原文地址:https://www.cnblogs.com/lckblog/p/4125678.html
Copyright © 2011-2022 走看看