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");
  }
      }
     
      /** 
       * 追加文件:使用FileWriter 
       *  
       * @param fileName 
       * @param content 
       */ 
      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;

/**
 * @author achievo
 *
 */
public class ContextListener implements ServletContextListener {
 
 private SmsTimer smtimer = new SmsTimer();

 /* (non-Javadoc)
  * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
  */
 @Override
 public void contextDestroyed(ServletContextEvent event) {
  smtimer.cancel();
  event.getServletContext().log("定时器销毁");

 }

 /* (non-Javadoc)
  * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
  */
 @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>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值