ScheduledExecutorService —定时周期执行指定的任务

//ScheduledThreadExecutor只支持相对时间。
//接口scheduleAtFixedRate原型定义及参数说明
//public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit); 
command:执行线程
initialDelay:初始化延时
period:两次开始执行最小间隔时间
unit:计时单位

public class ReportListener implements ServletContextListener{
	private ReportService reportService;
	public ReportService getReportService() {
		return reportService;
	}

	public void setReportService(ReportService reportService) {
		this.reportService = reportService;
	}
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		reportService = (ReportService)wac.getBean("reportService",ReportService.class);
		
		long oneDay = 24 * 60 * 60 * 1000;
		
		long scoreDelay=getTimeMillis("09:00:00") - System.currentTimeMillis();//每月居民积分短信提醒(每月1号上午9点开始)
		scoreDelay = scoreDelay > 0 ? scoreDelay : oneDay + scoreDelay;
		
		//TODO
		Runnable lastMonthScore = new Runnable(){
			public void run(){	
				Calendar c = Calendar.getInstance();
				int nowYear = c.get(Calendar.YEAR);//当前年份
				int lastMonth = c.get(Calendar.MONTH);//上月月份
				int date=c.get(Calendar.DATE);

				//如果当前日期是1号就发送(==1)
				if(c.get(Calendar.DATE) == 1){
					
					for(int i=0;i<comminList.size();i++){
						String comminid = String.valueOf(comminList.get(i).getId());
						List<UserBean> userList = reportService.getUserListByComminid(comminid);
						for(int s=0;s<userList.size();s++){
							//获取用户
							UserBean user=userList.get(s);
							//可回收垃圾投放积分
							String recoveryScore=reportService.getRecoveryScoreForLastMonth(user.getCommid()+"", user.getId().toString());
							//定时定点投放积分
							String otherScore=reportService.getOtherScoreForLastMonth(user.getCommid()+"", user.getId().toString());
							//巡检积分
							String inspection=reportService.getInspectionForLastMonth(user.getCommid()+"", user.getId().toString());
							//总积分
							int totalScore=Integer.parseInt(recoveryScore)+Integer.parseInt(otherScore);
							//上月消费积分
							String businessDisprice=reportService.getBusinessDisprice(user.getCommid()+"", user.getId().toString());
							//短信内容
							String logString="尊敬的用户:"+user.getName()+"您好!"+nowYear+"年"+lastMonth+"月您获得总积分为:"+totalScore+
							",其中巡检积分为:"+inspection+",可回收垃圾投放积分为:"+recoveryScore+",定时定点投放积分为:"+otherScore+
							",您上月消费积分为:"+businessDisprice+",剩余可用积分为:"+user.getIntegral()+"。";
							System.out.println(logString);
							//记录日志
							LoggerUtil.LogForLastMonthScoreSMS(logString);
							//发送短信
							SmsServiceUtils.sendMobileMsg(user.getMobilephone(), logString);
						}
					}
				}
		}};
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
		service.scheduleAtFixedRate(lastMonthScore, scoreDelay, oneDay, TimeUnit.MILLISECONDS);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值