timer

//第一步
public static void main(String[] args)
{
// PropertyConfigurator.configure("log4j.properties");
BaseTimer timer = new ReSendTimer();
timer.startTimer();
}

//第二步(赋值具体要执行任务的类)
/**
*
*/
package com.lolaage.cisvr.timer;

import com.lolaage.cisvr.sysconst.CiSvrConst;

/**
* @author huangye *
*/
public class ReSendTimer extends BaseTimer
{

/**
* constructor
*/
public ReSendTimer()
{
init(CiSvrConst.TIMER, CiSvrConst.RESEND_TIMER_INTERVAL,
CiSvrConst.RESEND_DEFAULT_TIMER_INTERVAL);
setTimerTsk(new ReSendTask());
}

}

//第三步(定时启动的规则)
/**
*
*/
package com.lolaage.cisvr.timer;

import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lolaage.cisvr.sysconf.CiSvrConfig;
import com.lolaage.cisvr.sysconf.ICiSvrConfig;
import com.lolaage.cisvr.sysconst.CiSvrConst;

/**
* @author huangye *
*/
public class BaseTimer
{
/**
* task
*/
private Timer task;

/**
* timerTsk
*/
private TimerTask timerTsk;

/**
* interval
*/
private int interval;

/**
* log
*/
private static Log log = LogFactory.getLog(BaseTimer.class);

/**
* initialization
*/
public void init(String module, String cfgItem, int defaultVaule)
{
if (log.isDebugEnabled())
{
log.debug("timer initialization.");
}
// read interval from config file (从配置中获取定时间隔)
ICiSvrConfig config = CiSvrConfig.getInstance();
interval = config.readInt(module, cfgItem);

if (CiSvrConst.ZERO >= interval)
{
if (log.isWarnEnabled())
{
log.warn("config data errer.");
}
interval = defaultVaule;
}
if (log.isInfoEnabled())
{
log.info("interval->" + interval);
}
task = new Timer();
}

/**
* start timer
*/
public void startTimer()
{
// start timer
if (log.isTraceEnabled())
{
log.trace("start timer successfully.");
}
task.schedule(timerTsk, CiSvrConst.ZERO, interval
* CiSvrConst.ONE_SECOND_IN_MILLSECOND);
}

/**
* @param timerTsk
* the timerTsk to set
*/
public void setTimerTsk(TimerTask timerTsk)
{
this.timerTsk = timerTsk;
}
}

//第四步(定时要执行的任务类)
/**
*
*/
package com.lolaage.cisvr.timer;

import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.lolaage.cisvr.resource.TaskQueue4CommLayer;

/**
* @author huangye *
*/
public class ReSendTaskextends TimerTask
{

/**
* taskQueue4CommLayer
*/
private TaskQueue4CommLayer taskQueue4CommLayer;

/**
* log
*/
private static Log log = LogFactory.getLog(StatTask.class);

/**
* constructor
*/
public ReSendTask()
{
taskQueue4CommLayer = TaskQueue4CommLayer.getInstance();
}

/*
* (non-Javadoc)
*
* @see java.util.TimerTask#run()
*/
public void run()
{
try
{
//定时要执行的任务
}
catch (Exception e)
{
if (log.isErrorEnabled())
{
log.error(e);
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值