ScheduledExecutorService —定时周期执行指定的任务
2015-07-21 17:50
597 查看
//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);
}
}
//接口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);
}
}
相关文章推荐
- 图片轮播器以及线程控制
- C/C++ typedef用法
- 海量数据相似度计算之simhash和海明距离
- curl wget 不验证证书进行https请求
- 浅析格式化串漏洞
- Access-Control-Allow-Origin与跨域
- 自定义对话框<转>
- Js_日期格式化
- with ur锁
- 源码安装PHP报错:error: xml2-config not found.
- [黑马程序员](第6-9天)面向对象(下)
- Android开发中的SQLite事务处理,即beginTransaction()方法
- [LeetCode]Valid Parentheses
- dd命令使用详解
- 数据表生成javabean工具
- thinkphp网站缓存写入失败
- 2015 Multi-University Training Contest 1
- 简易版桶排序
- zx2000 HDMI audio
- 判断字符串是否为数字的多种方法