JDK Timer 使用

Java 定时器之 JDK Timer:


package com.wx.jdktimer;

import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.ejb.CreateException;

public class JDKTimerBean implements IJDKTimer {

private static final long serialVersionUID = -7118631760248451136L;
private Timer timer = null;

private void scheduleBatchService() {
timer = new Timer();
TimerTask timerTask = new bizTimerTask();
timer.schedule(timerTask, new Date(), 24*60*60*1000);

}

private void clearTimer(){
Logger.getInstance().info("In method -> clearTimer now");
if (timer != null) {
timer.cancel();
}
Logger.getInstance().info("Exit method -> clearTimer now");
}


class bizTimerTask extends TimerTask {
BizServiceds = null;

@Override
public void run() {
Logger.getInstance().info("do business logic....");
System.out.println("do business logic....");
ds = new BizService();
ds.doBizLogic();
}

}

public void ejbCreate() throws RemoteException, CreateException {

scheduleBatchService();
}

public void ejbRemove() throws RemoteException {
clearTimer();
}
}

你可能感兴趣的:(Java定时器)