定时执行JAVA

来源:互联网 发布:空间数据分析方法 编辑:程序博客网 时间:2024/05/11 17:25

SyncSendToCP.java

package com.gdcn.xlt.sms.sync.cp;

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

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.amonsoft.commons.utils.*;

/**
 * 把数据同步到CP端口类
 * @copyright Amonsoft.com
 */
public class SyncSendToCP extends HttpServlet {

    // 是否已经完成了数据读入
    private static boolean loadedProperty = true;

    // 发送检索时间间隔,单位:秒。
    private static int sendTimer = 8;

    /**
     * 系列编号
     */
    private static final long serialVersionUID = -8977802003747335270L;

   

    /**
     * 取得初始化配置参数
     *
     */
    private static void getProperties() {
        sendTimer = MyString.toInt(SyncProperty.getProtity("check_send_timer",
                "8"), 8);
        if (sendTimer < 1) {
            loadedProperty = false;
            System.out
                    .println("Load sync property /"check_send_timer/" failed.");
        }
    }

    /**
     * 数据同步到CP端口构造函数
     */
    public SyncSendToCP() {
        super();
    }

    /**
     * 覆盖init方法,实现初始化类时执行程序
     *
     * @param config
     *            web.xml配置信息对象
     * @throws ServletException
     *             抛出例外
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        // 取得配置参数
        getProperties();
        // 执行循环处理
        if (loadedProperty)
            run();
        else
            System.out.println("Load properties failed.");
    }

    /**
     * 循环检测是否有数据在发送表中,假如有则同步到CP端
     * 
     */
    public void run() {
        Timer timer = new Timer();
        TimerTask task = new SyncSendTimer();
        // 间隔sendTimer秒执行一次任务
        timer.schedule( task, 1000, sendTimer * 1000);
    }

}

 

SyncSendTimer 类

其中 SyncSendTimer extends TimerTask

还要提供

public void run(){

}

的方法

new java.util.TimerTask() {
   public void run() {
    schedule();
   }
  }
这里就相当于SyncSendTimer类的实现


web.xml 配置信息

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
    <display-name>
    SyncXLTSms</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>SyncSms</servlet-name>
        <servlet-class>
          com.gdcn.xlt.sms.sync.cp.SyncSendToCP
        </servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
   
</web-app>
 

原创粉丝点击