纯java简易定时器

本文介绍了一个使用Spring框架实现的定时任务案例,通过Calendar类设定每天0点5分15秒执行关闭过期合同及无效供应商的任务,并利用Timer类进行周期性调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果你用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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值