Java 定时线程

功能需求:项目启动时,后天起一个定时线程,每个小时跑一次,查出数据发邮件出来。

主要使用

  • public void schedule(TimerTask task, long delay)

    task被安排在delay(毫秒)指定的时间后执行。

  • public void schedule(TimerTask task,long delay, long period)

      task被安排在delay(毫秒)指定的时间后执行。执行后将每隔period(毫秒)反复执行。

  •   public void scheduleAtFixedRate(TimerTask task,Date firstTime, long period)

      task被安排在firstTime指定的时间执行。执行后将每隔period(毫秒)反复执行。每一次重复的时间时盒第一次执行而不是和前一次执行有关。因此执行的总速度是固定的。

  •   public void scheduleAtFixedRate(TimerTask task,long delay,long period)

      task被安排在delay(毫秒)指定的时间后执行。执行后将每隔period(毫秒)反复执行。每一次重复的时间时盒第一次执行而不是和前一次执行有关。因此执行的总速度是固定的。

 

package com.odianyun.ad.service.alertMailJob;

import com.odianyun.ad.business.read.manage.AdSourceReadManage;
import com.odianyun.ad.business.utils.ConfigUtil;
import com.odianyun.ad.model.po.AdSourcePO;
import com.odianyun.hotword.business.read.manage.HotWordReadManage;
import com.odianyun.hotword.business.read.manage.impl.HotWordReadManageImpl;
import com.odianyun.search.whale.common.util.EmailUtil;
import com.odianyun.search.whale.common.util.NetUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.*;

public class ExpiredJob {

static Logger logger = Logger.getLogger(ExpiredJob.class);

@Autowired
AdSourceReadManage adSourceReadManage;

@Autowired
HotWordReadManage hotWordReadManage;

private static long sendIntervalMinus;
private static boolean isSendErrorMail;
private static String sendTo;

static {
ConfigUtil.loadPropertiesFile("mail.properties");
sendIntervalMinus = ConfigUtil.getLong("mail.sendIntervalMinus", 60);
isSendErrorMail = ConfigUtil.getBool("mail.isSendErrorMail", true);
sendTo = ConfigUtil.get("mail.msgTo");
}


public ExpiredJob() {
     //现在构造函数里面,然后在spring bean的配置文件里增加这个bean,让spring容器创建这个实例,也就达到了项目启动时启动线程的目的

//线程用于扫描24小时以内即将过期的广告
//此线程启动后开始执行,每一个小时执行一次
Timer timer = new Timer(false);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
try {
List<AdSourcePO> adlist = adSourceReadManage.getExpiredAdSource();
List<String> hotwordlist = hotWordReadManage.getExpiredHotWordIds();
if (CollectionUtils.isNotEmpty(adlist)) {
StringBuffer sb = new StringBuffer();
for(AdSourcePO adSourcePO : adlist){
sb.append("广告名称 : " + adSourcePO.getName() + " ,广告位 : " +adSourcePO.getCodeName() +" </br>");
}
EmailUtil.sendMail("Below " + adlist.size() + " ads will be expired in 24 hours , send from " + NetUtil.getLocalIP(), sendTo, sb);
logger.info("Send ad " + adlist.toString() + " mail successfully");
} else {
logger.info("No ads will expired in 24 hours.");
}

if (CollectionUtils.isNotEmpty(hotwordlist)) {
EmailUtil.sendMail("Below " + hotwordlist.size() + " hot words will be expired in 24 hours , send from " + NetUtil.getLocalIP(), sendTo, hotwordlist);
logger.info("Send hot words " + hotwordlist.toString() + " mail successfully");
} else {
logger.info("No hot words will expired in 24 hours.");
}
} catch (Exception e) {
logger.error("Send ad expired mail exception", e);
}
}
}, 1000 * 60 * 1, 1000 * 60 * sendIntervalMinus);
}

;


}
posted @ 2016-05-03 15:59  SoulCoder  阅读(602)  评论(0编辑  收藏  举报