如果你用spring, 那直接用spring 的好了,
这里是自己设定调度时间的,也好用,
public class CloseTaskFacade implements IAppInitJob {
private static Logger log = Logger.getLogger(CloseTaskFacade.class);
private ContractBPO myContractBPO;
private SuppliersUserBPO mySuppliersUserBPO;
public void doJob(ServletContext context) {
try {
// 设定为每天晚上0点5分15秒开始执行任务。
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, 1);
cale.set(Calendar.HOUR_OF_DAY, 0);
cale.set(Calendar.MINUTE, 5);
cale.set(Calendar.SECOND, 15);
Date firstTime = cale.getTime();
log.info(firstTime);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
log.info("beging close contract that need close......");
myContractBPO.closeExpireContractTX();
log
.info("beging close nvalidSuppliersUser that need close...");
mySuppliersUserBPO.closeInvalidSuppliersUserTX();
}
}, firstTime, 86400000);
} catch (Exception e) {
log.error("excute timer task exception :" + e.getMessage());
}
}
public void setMyContractBPO(ContractBPO myContractBPO) {
this.myContractBPO = myContractBPO;
}
public void setMySuppliersUserBPO(SuppliersUserBPO mySuppliersUserBPO) {
this.mySuppliersUserBPO = mySuppliersUserBPO;
}
}
这里是自己设定调度时间的,也好用,
public class CloseTaskFacade implements IAppInitJob {
private static Logger log = Logger.getLogger(CloseTaskFacade.class);
private ContractBPO myContractBPO;
private SuppliersUserBPO mySuppliersUserBPO;
public void doJob(ServletContext context) {
try {
// 设定为每天晚上0点5分15秒开始执行任务。
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, 1);
cale.set(Calendar.HOUR_OF_DAY, 0);
cale.set(Calendar.MINUTE, 5);
cale.set(Calendar.SECOND, 15);
Date firstTime = cale.getTime();
log.info(firstTime);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
log.info("beging close contract that need close......");
myContractBPO.closeExpireContractTX();
log
.info("beging close nvalidSuppliersUser that need close...");
mySuppliersUserBPO.closeInvalidSuppliersUserTX();
}
}, firstTime, 86400000);
} catch (Exception e) {
log.error("excute timer task exception :" + e.getMessage());
}
}
public void setMyContractBPO(ContractBPO myContractBPO) {
this.myContractBPO = myContractBPO;
}
public void setMySuppliersUserBPO(SuppliersUserBPO mySuppliersUserBPO) {
this.mySuppliersUserBPO = mySuppliersUserBPO;
}
}