天天看點

java定時運作操作

 import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Timer;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class SmsTimer {

 private Timer timer = new Timer();

 private static final Log logger = LogFactory.getLog(SmsTimer.class);

 public void run() {

  MyTask task = new MyTask();

  long nextdate = 0;

  long curdate = 0;

  long rundate = 0;

  DateFormat format=new SimpleDateFormat("yyyy-MM-dd");

  Calendar cal = Calendar.getInstance();

  curdate = cal.getTimeInMillis();

  cal.add(Calendar.DATE, 1);//日期加1天

  //System.out.println(curdate);

  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

  dateFormat.setLenient(false);

  try {

   Date d = (Date)dateFormat.parseObject(format.format(cal.getTime()));

   nextdate = d.getTime() - 2*1000*60;

  } catch (Exception e) {

   logger.error(e);

  }

  //System.out.println(nextdate);

  rundate = nextdate - curdate;

  //System.out.println(rundate);

  if(rundate > 0) {

   // 每天23:58分 寫count記錄

   // timer.schedule(task, rundate, 1000*60*60*24);

  }

  // 1秒後, 每隔5分鐘

  timer.schedule(task, 1000, 5*60*1000);

 }

 public void cancel() {

  timer.cancel();

 }

}

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimerTask;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class MyTask extends TimerTask {

 private static final Log logger = LogFactory.getLog(MyTask.class);

 private String space = "           ";

      public void run() {

       DateFormat format=new SimpleDateFormat("yyyy-MM-dd");

       String nowDate = format.format(new Date());

       logger.info("Timer start");

       String fileName = "c:/123.txt";

       logger.info("Timer file path " + fileName);

          File file = new File(fileName);

          String title = "日期               郵件1        郵件2       郵件3       郵件4      郵件5     郵件總數    短信貓     批量短信  ";

          String content = nowDate + space + TimerCount.get("mail1") + space + TimerCount.get("mail2")

                + space + TimerCount.get("mail3") + space + TimerCount.get("mail4") + space + TimerCount.get("mail5")

                + space + TimerCount.get("mailtotle") + space + TimerCount.get("SmsCat")

                + space + TimerCount.get("SmsBatch");

          try {

           if(file.createNewFile()) {

            writeFile(fileName, title);

           }

           writeFile(fileName, content);

           // 清0操作

           TimerCount.init();

  } catch (IOException e) {

    logger.error(e);

    logger.error("write file failed");

  }

      }

      private void writeFile(String fileName, String content) {  

          try {

           logger.info("write file name : " + fileName);

           logger.info("write file content : " + content);

              // 打開一個寫檔案器,構造函數中的第二個參數true表示以追加形式寫檔案  

              FileWriter writer = new FileWriter(fileName, true);

              writer.write( content + "/r/n");  

              writer.close();

              logger.info("write file success");

          } catch (IOException e) {  

           logger.error(e);

           logger.error("write file failed");

          }  

      } 

}

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import org.iata.framework.sms.timer.SmsTimer;

public class ContextListener implements ServletContextListener {

 private SmsTimer smtimer = new SmsTimer();

 @Override

 public void contextDestroyed(ServletContextEvent event) {

  smtimer.cancel();

  event.getServletContext().log("定時器銷毀");

 }

 @Override

 public void contextInitialized(ServletContextEvent event) {

  event.getServletContext().log("定時器已啟動");

  smtimer.run();

 }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <listener>

  <listener-class>org.iata.framework.util.ContextListener</listener-class>

 </listener>

 <welcome-file-list>

  <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

 <login-config>

  <auth-method>BASIC</auth-method>

 </login-config>

</web-app>