Commit 0fe187ab6aa46e7880864450983af29a35192e54
1 parent
c1b0e05a
update...
Showing
3 changed files
with
55 additions
and
35 deletions
src/main/java/com/bsth/redis/OilRedisService.java
| @@ -115,8 +115,15 @@ public class OilRedisService implements CommandLineRunner { | @@ -115,8 +115,15 @@ public class OilRedisService implements CommandLineRunner { | ||
| 115 | 115 | ||
| 116 | @Override | 116 | @Override |
| 117 | public void run(String... strings) throws Exception { | 117 | public void run(String... strings) throws Exception { |
| 118 | - //启动加载油耗缓存 | ||
| 119 | - synchData(null); | 118 | + Application.mainServices.schedule(new Runnable() { |
| 119 | + @Override | ||
| 120 | + public void run() { | ||
| 121 | + //启动加载油耗缓存 | ||
| 122 | + synchData(null); | ||
| 123 | + } | ||
| 124 | + }, 30, TimeUnit.SECONDS); | ||
| 125 | + | ||
| 126 | + | ||
| 120 | //定时刷新油耗信息 | 127 | //定时刷新油耗信息 |
| 121 | Application.mainServices.scheduleWithFixedDelay(oilRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS); | 128 | Application.mainServices.scheduleWithFixedDelay(oilRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS); |
| 122 | } | 129 | } |
| @@ -124,9 +131,9 @@ public class OilRedisService implements CommandLineRunner { | @@ -124,9 +131,9 @@ public class OilRedisService implements CommandLineRunner { | ||
| 124 | /** | 131 | /** |
| 125 | * 和数据库同步数据 | 132 | * 和数据库同步数据 |
| 126 | */ | 133 | */ |
| 127 | - public void synchData(Integer days){ | 134 | + public void synchData(Integer days) { |
| 128 | int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | 135 | int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); |
| 129 | - if(null != days && days < cacheDays) | 136 | + if (null != days && days < cacheDays) |
| 130 | cacheDays = days; | 137 | cacheDays = days; |
| 131 | //设置key 序列化器 | 138 | //设置key 序列化器 |
| 132 | redisTemplate.setKeySerializer(new StringRedisSerializer()); | 139 | redisTemplate.setKeySerializer(new StringRedisSerializer()); |
| @@ -144,6 +151,7 @@ public class OilRedisService implements CommandLineRunner { | @@ -144,6 +151,7 @@ public class OilRedisService implements CommandLineRunner { | ||
| 144 | 151 | ||
| 145 | @Autowired | 152 | @Autowired |
| 146 | OilRedisService oilRedisService; | 153 | OilRedisService oilRedisService; |
| 154 | + | ||
| 147 | @Override | 155 | @Override |
| 148 | public void run() { | 156 | public void run() { |
| 149 | try { | 157 | try { |
src/main/java/com/bsth/redis/PlanScheduleRedisService.java
| @@ -124,16 +124,22 @@ public class PlanScheduleRedisService implements CommandLineRunner { | @@ -124,16 +124,22 @@ public class PlanScheduleRedisService implements CommandLineRunner { | ||
| 124 | 124 | ||
| 125 | @Override | 125 | @Override |
| 126 | public void run(String... strings) throws Exception { | 126 | public void run(String... strings) throws Exception { |
| 127 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 128 | - //设置key 序列化器 | ||
| 129 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | 127 | + Application.mainServices.schedule(new Runnable() { |
| 128 | + @Override | ||
| 129 | + public void run() { | ||
| 130 | + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 131 | + //设置key 序列化器 | ||
| 132 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
| 133 | + | ||
| 134 | + DateTime dt = new DateTime(); | ||
| 135 | + dt = dt.minusDays(cacheDays); | ||
| 136 | + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | ||
| 137 | + List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(dt.toDate(), new Date()); | ||
| 138 | + //写入redis | ||
| 139 | + wirte(list); | ||
| 140 | + } | ||
| 141 | + }, 60 * 5, TimeUnit.SECONDS); | ||
| 130 | 142 | ||
| 131 | - DateTime dt = new DateTime(); | ||
| 132 | - dt = dt.minusDays(cacheDays); | ||
| 133 | - dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | ||
| 134 | - List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(dt.toDate(), new Date()); | ||
| 135 | - //写入redis | ||
| 136 | - wirte(list); | ||
| 137 | 143 | ||
| 138 | //定时 00:05 分清理计划,并加载当天的计划 | 144 | //定时 00:05 分清理计划,并加载当天的计划 |
| 139 | long diff = (DateUtils.getTimestamp() + 1000 * 60 * 5) - System.currentTimeMillis(); | 145 | long diff = (DateUtils.getTimestamp() + 1000 * 60 * 5) - System.currentTimeMillis(); |
| @@ -158,6 +164,7 @@ public class PlanScheduleRedisService implements CommandLineRunner { | @@ -158,6 +164,7 @@ public class PlanScheduleRedisService implements CommandLineRunner { | ||
| 158 | PlanScheduleRedisService planRedisService; | 164 | PlanScheduleRedisService planRedisService; |
| 159 | @Autowired | 165 | @Autowired |
| 160 | SchedulePlanInfoRepository planInfoRepository; | 166 | SchedulePlanInfoRepository planInfoRepository; |
| 167 | + | ||
| 161 | @Override | 168 | @Override |
| 162 | public void run() { | 169 | public void run() { |
| 163 | try { | 170 | try { |
src/main/java/com/bsth/redis/ScheduleRedisService.java
| @@ -188,20 +188,26 @@ public class ScheduleRedisService implements CommandLineRunner { | @@ -188,20 +188,26 @@ public class ScheduleRedisService implements CommandLineRunner { | ||
| 188 | 188 | ||
| 189 | @Override | 189 | @Override |
| 190 | public void run(String... strings) throws Exception { | 190 | public void run(String... strings) throws Exception { |
| 191 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 192 | - //设置key 序列化器 | ||
| 193 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | 191 | + //用子线程去加载,,不要阻塞 |
| 192 | + Application.mainServices.schedule(new Runnable() { | ||
| 193 | + @Override | ||
| 194 | + public void run() { | ||
| 195 | + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 196 | + //设置key 序列化器 | ||
| 197 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
| 194 | 198 | ||
| 195 | - DateTime dt = new DateTime(); | ||
| 196 | - dt = dt.minusDays(cacheDays); | ||
| 197 | - List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd")); | ||
| 198 | - calcTime(list); | ||
| 199 | - //写入redis | ||
| 200 | - wirte(list); | 199 | + DateTime dt = new DateTime(); |
| 200 | + dt = dt.minusDays(cacheDays); | ||
| 201 | + List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd")); | ||
| 202 | + calcTime(list); | ||
| 203 | + //写入redis | ||
| 204 | + wirte(list); | ||
| 205 | + } | ||
| 206 | + }, 5, TimeUnit.SECONDS); | ||
| 201 | 207 | ||
| 202 | //定时刷新一次当日实际排班 | 208 | //定时刷新一次当日实际排班 |
| 203 | int minute = 10; | 209 | int minute = 10; |
| 204 | - Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute, minute, TimeUnit.MINUTES); | 210 | + Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute + 4, minute, TimeUnit.MINUTES); |
| 205 | //24小时清理一次实际排班数据 | 211 | //24小时清理一次实际排班数据 |
| 206 | Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS); | 212 | Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS); |
| 207 | } | 213 | } |
| @@ -275,12 +281,12 @@ public class ScheduleRedisService implements CommandLineRunner { | @@ -275,12 +281,12 @@ public class ScheduleRedisService implements CommandLineRunner { | ||
| 275 | private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"), | 281 | private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"), |
| 276 | fmtHHmm = DateTimeFormat.forPattern("HH:mm"); | 282 | fmtHHmm = DateTimeFormat.forPattern("HH:mm"); |
| 277 | 283 | ||
| 278 | - private void calcTime(List<ScheduleRealInfo> list){ | ||
| 279 | - if(list.size() == 0) | 284 | + private void calcTime(List<ScheduleRealInfo> list) { |
| 285 | + if (list.size() == 0) | ||
| 280 | return; | 286 | return; |
| 281 | 287 | ||
| 282 | //计算真实执行日期 和 时间戳 | 288 | //计算真实执行日期 和 时间戳 |
| 283 | - for(ScheduleRealInfo sch : list){ | 289 | + for (ScheduleRealInfo sch : list) { |
| 284 | calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch); | 290 | calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch); |
| 285 | } | 291 | } |
| 286 | } | 292 | } |
| @@ -291,10 +297,10 @@ public class ScheduleRedisService implements CommandLineRunner { | @@ -291,10 +297,10 @@ public class ScheduleRedisService implements CommandLineRunner { | ||
| 291 | */ | 297 | */ |
| 292 | public void calcRealDate(String startTime, ScheduleRealInfo sch) { | 298 | public void calcRealDate(String startTime, ScheduleRealInfo sch) { |
| 293 | try { | 299 | try { |
| 294 | - if(null == startTime) | 300 | + if (null == startTime) |
| 295 | return; | 301 | return; |
| 296 | 302 | ||
| 297 | - if(null == sch.getBcsj()) | 303 | + if (null == sch.getBcsj()) |
| 298 | sch.setBcsj(0); | 304 | sch.setBcsj(0); |
| 299 | 305 | ||
| 300 | String rq = sch.getScheduleDateStr(); | 306 | String rq = sch.getScheduleDateStr(); |
| @@ -303,27 +309,26 @@ public class ScheduleRedisService implements CommandLineRunner { | @@ -303,27 +309,26 @@ public class ScheduleRedisService implements CommandLineRunner { | ||
| 303 | //待发时间 | 309 | //待发时间 |
| 304 | sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime)); | 310 | sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime)); |
| 305 | //计划终点时间 | 311 | //计划终点时间 |
| 306 | - if(StringUtils.isEmpty(sch.getZdsj())){ | 312 | + if (StringUtils.isEmpty(sch.getZdsj())) { |
| 307 | sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); | 313 | sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); |
| 308 | sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); | 314 | sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); |
| 309 | - } | ||
| 310 | - else | 315 | + } else |
| 311 | sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); | 316 | sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); |
| 312 | //实发时间 | 317 | //实发时间 |
| 313 | - if(StringUtils.isNotEmpty(sch.getFcsjActual())) | 318 | + if (StringUtils.isNotEmpty(sch.getFcsjActual())) |
| 314 | sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime)); | 319 | sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime)); |
| 315 | 320 | ||
| 316 | //实达时间 | 321 | //实达时间 |
| 317 | - if(StringUtils.isNotEmpty(sch.getZdsjActual())) | 322 | + if (StringUtils.isNotEmpty(sch.getZdsjActual())) |
| 318 | sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime)); | 323 | sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime)); |
| 319 | } catch (Exception e) { | 324 | } catch (Exception e) { |
| 320 | logger.error("", e); | 325 | logger.error("", e); |
| 321 | } | 326 | } |
| 322 | } | 327 | } |
| 323 | 328 | ||
| 324 | - private long parseTime(String rq, String sj, String startTime){ | 329 | + private long parseTime(String rq, String sj, String startTime) { |
| 325 | long t = fmtyyyyMMddHHmm.parseMillis(rq + sj); | 330 | long t = fmtyyyyMMddHHmm.parseMillis(rq + sj); |
| 326 | - if(sj.compareTo(startTime) < 0){ | 331 | + if (sj.compareTo(startTime) < 0) { |
| 327 | t += DAY_TIME; | 332 | t += DAY_TIME; |
| 328 | } | 333 | } |
| 329 | return t; | 334 | return t; |