1. 程式人生 > >java兩種定時器

java兩種定時器

etl 兩種 des cal 固定 指定 let num content

第一種:循環執行的程序


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* java定時器
* @author lin
*
*/
public class ScheduledExecutorTest {
private ScheduledExecutorService scheduExec;

public long start;

ScheduledExecutorTest(){
this.scheduExec = Executors.newScheduledThreadPool(2);
this.start = System.currentTimeMillis();
}

public void timerOne(){
scheduExec.schedule(new Runnable() {
public void run() {
throw new RuntimeException();
}
},1000,TimeUnit.MILLISECONDS);
}

public void timerTwo(){
scheduExec.scheduleAtFixedRate(new Runnable() {
public void run() {

//循環執行的代碼
System.out.println("timerTwo invoked ....."+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
},0,1000,TimeUnit.MILLISECONDS);//0代表多久以後執行 1000代表多久執行一次 單位毫秒
}

public static void main(String[] args) {
ScheduledExecutorTest test = new ScheduledExecutorTest();
test.timerOne();
test.timerTwo();
}
}

第二種:定時任務

1.在項目的web.xml寫監聽器

<!-- 定時器 -->
<listener>
<listener-class>com.blue.common.task.NFDFlightDataTaskListener</listener-class>
</listener>

2.監聽器 NFDFlightDataTaskListener

package com.blue.common.task;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class NFDFlightDataTaskListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent event) {
new TimerManager();
}

public void contextDestroyed(ServletContextEvent event) {
}

}

3.定時器 TimerManager

package com.blue.common.task;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

public class TimerManager {

//時間間隔
private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;

public TimerManager() {
Calendar calendar = Calendar.getInstance();

/*** 定制每日2:00執行方法 ***/

calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Date date=calendar.getTime(); //第一次執行定時任務的時間

//如果第一次執行定時任務的時間 小於 當前的時間
//此時要在 第一次執行定時任務的時間 加一天,以便此任務在下個時間點執行。如果不加一天,任務會立即執行。
if (date.before(new Date())) {
date = this.addDay(date, 1);
}

Timer timer = new Timer();

NFDFlightDataTimerTask task = new NFDFlightDataTimerTask();
//安排指定的任務在指定的時間開始進行重復的固定延遲執行。
timer.schedule(task,date,PERIOD_DAY);
}

// 增加或減少天數
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}

}

4.定時任務NFDFlightDataTimerTask

package com.blue.common.task;

import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.blue.common.util.SpringUtils;
import com.blue.system.service.ContentService;

public class NFDFlightDataTimerTask extends TimerTask {

private ContentService contentService=SpringUtils.getBean("contentService");
private Logger logger = LoggerFactory.getLogger(NFDFlightDataTimerTask.class);
/**
* 定時更新新聞的量
*/
@Override
public void run() {
try {
contentService.updateContentAmounts();//文章量
} catch (Exception e) {
logger.info("-------------定時任務發生異常--------------");
}
}
}

java兩種定時器