Commit c0d2ad161cf83287f5c3fb74854a458a752a0ff6

Authored by 潘钊
1 parent 5ee4d437

update...

src/main/java/com/bsth/Application.java
... ... @@ -11,7 +11,7 @@ import java.util.concurrent.ScheduledExecutorService;
11 11 @SpringBootApplication
12 12 public class Application extends SpringBootServletInitializer {
13 13  
14   - public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(5);
  14 + public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(10);
15 15  
16 16 @Override
17 17 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
... ...
src/main/java/com/bsth/redis/OilRedisService.java
1 1 package com.bsth.redis;
2 2  
3 3 import com.bsth.entity.OilInfo;
  4 +import com.bsth.redis.util.RedisUtils;
4 5 import com.bsth.repository.OilInfoRepository;
5 6 import com.bsth.util.ConfigUtil;
6 7 import com.bsth.util.ConvertUtil;
... ... @@ -32,6 +33,9 @@ public class OilRedisService implements CommandLineRunner {
32 33 @Autowired
33 34 OilInfoRepository oilInfoRepository;
34 35  
  36 + @Autowired
  37 + RedisUtils redisUtils;
  38 +
35 39 Logger logger = LoggerFactory.getLogger(this.getClass());
36 40  
37 41 private final static String REDIS_KEY_PREFIX = "oil:";
... ... @@ -107,10 +111,9 @@ public class OilRedisService implements CommandLineRunner {
107 111 if (!list.contains(oil))
108 112 list.add(oil);
109 113 }
110   - //删除redis数据
111   - redisTemplate.delete(key);
112   - //重新写入
113   - ops.leftPushAll(key, list);
  114 +
  115 + //更新
  116 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
114 117 }
115 118  
116 119 @Override
... ...
src/main/java/com/bsth/redis/PlanScheduleRedisService.java
1 1 package com.bsth.redis;
2 2  
  3 +import com.bsth.Application;
3 4 import com.bsth.entity.SchedulePlanInfo;
  5 +import com.bsth.redis.util.RedisUtils;
4 6 import com.bsth.repository.SchedulePlanInfoRepository;
  7 +import com.bsth.server_rs.base_info.line.Line;
  8 +import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
5 9 import com.bsth.util.ConfigUtil;
6 10 import com.bsth.util.ConvertUtil;
7 11 import com.google.common.collect.ArrayListMultimap;
... ... @@ -14,15 +18,17 @@ import org.springframework.core.annotation.Order;
14 18 import org.springframework.data.redis.core.ListOperations;
15 19 import org.springframework.data.redis.core.RedisTemplate;
16 20 import org.springframework.data.redis.serializer.StringRedisSerializer;
  21 +import org.springframework.stereotype.Component;
17 22 import org.springframework.stereotype.Service;
18 23  
19 24 import java.util.ArrayList;
20 25 import java.util.Date;
21 26 import java.util.Iterator;
22 27 import java.util.List;
  28 +import java.util.concurrent.TimeUnit;
23 29  
24 30 /**
25   - * 计调的 计划排班redis缓存
  31 + * 计调的 计划排班redis缓存
26 32 * Created by panzhao on 2017/3/27.
27 33 */
28 34 @Service
... ... @@ -35,7 +41,10 @@ public class PlanScheduleRedisService implements CommandLineRunner {
35 41 @Autowired
36 42 SchedulePlanInfoRepository planInfoRepository;
37 43  
38   - Logger logger = LoggerFactory.getLogger(this.getClass());
  44 + @Autowired
  45 + RedisUtils redisUtils;
  46 +
  47 + static Logger logger = LoggerFactory.getLogger(PlanScheduleRedisService.class);
39 48  
40 49 private final static String REDIS_KEY_PREFIX = "plan:";
41 50  
... ... @@ -58,7 +67,7 @@ public class PlanScheduleRedisService implements CommandLineRunner {
58 67 String key;
59 68 while (iterator.hasNext()) {
60 69 key = iterator.next();
61   - mergeData(key, multimap.get(key));
  70 + replace(key, multimap.get(key));
62 71 }
63 72 } catch (Exception e) {
64 73 logger.error("", e);
... ... @@ -81,10 +90,20 @@ public class PlanScheduleRedisService implements CommandLineRunner {
81 90 if (!list.contains(plan))
82 91 list.add(plan);
83 92 }
84   - //删除redis数据
85   - redisTemplate.delete(key);
86   - //重新写入
87   - ops.leftPushAll(key, list);
  93 +
  94 + //更新
  95 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  96 + }
  97 +
  98 + /**
  99 + * 覆盖数据
  100 + *
  101 + * @param key
  102 + * @param list
  103 + */
  104 + public void replace(String key, List<SchedulePlanInfo> list) {
  105 + key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
  106 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
88 107 }
89 108  
90 109 /**
... ... @@ -98,6 +117,10 @@ public class PlanScheduleRedisService implements CommandLineRunner {
98 117 return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1);
99 118 }
100 119  
  120 +
  121 + @Autowired
  122 + PlanClearThread planClearThread;
  123 +
101 124 @Override
102 125 public void run(String... strings) throws Exception {
103 126 int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
... ... @@ -108,17 +131,50 @@ public class PlanScheduleRedisService implements CommandLineRunner {
108 131 dt = dt.minusDays(cacheDays);
109 132 dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
110 133 List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(dt.toDate(), new Date());
111   -
112 134 //写入redis
113 135 wirte(list);
  136 +
  137 + //24小时清理一次计划排班数据
  138 + Application.mainServices.scheduleWithFixedDelay(planClearThread, 24, 24, TimeUnit.HOURS);
114 139 }
115 140  
116 141 public List<SchedulePlanInfo> findByMultiLine(List<String> lineArray, String rq) {
117 142 rq = rq.replaceAll("-", "");
118 143 List<SchedulePlanInfo> rs = new ArrayList<>();
119   - for(String lineCode : lineArray){
  144 + for (String lineCode : lineArray) {
120 145 rs.addAll(read(rq, lineCode));
121 146 }
122 147 return rs;
123 148 }
  149 +
  150 + @Component
  151 + public static class PlanClearThread extends Thread {
  152 +
  153 + @Autowired
  154 + PlanScheduleRedisService planRedisService;
  155 +
  156 + @Override
  157 + public void run() {
  158 + try {
  159 + logger.info("redis -清理计划排班");
  160 +
  161 + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
  162 + DateTime dt = new DateTime();
  163 + dt = dt.minusDays(cacheDays);
  164 + String rq = dt.toString("yyyy-MM-dd");
  165 +
  166 + List<Line> lines = LineBufferData.findAll();
  167 + for (Line line : lines) {
  168 + planRedisService.delete(line.getLineCode(), rq);
  169 + }
  170 + } catch (Exception e) {
  171 + logger.error("", e);
  172 + }
  173 + }
  174 + }
  175 +
  176 + private void delete(String lineCode, String rq) {
  177 + String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", "");
  178 + redisTemplate.delete(key);
  179 + }
124 180 }
... ...
src/main/java/com/bsth/redis/ScheduleRedisService.java
1 1 package com.bsth.redis;
2 2  
  3 +import com.bsth.Application;
3 4 import com.bsth.entity.ScheduleRealInfo;
  5 +import com.bsth.redis.util.RedisUtils;
4 6 import com.bsth.repository.ScheduleRealInfoRepository;
  7 +import com.bsth.server_rs.base_info.line.Line;
  8 +import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
  9 +import com.bsth.server_ws.WebServiceProxy;
5 10 import com.bsth.util.ConfigUtil;
6 11 import com.bsth.util.ConvertUtil;
7   -import com.bsth.server_ws.WebServiceProxy;
8 12 import com.google.common.collect.ArrayListMultimap;
9 13 import org.joda.time.DateTime;
10 14 import org.slf4j.Logger;
... ... @@ -15,11 +19,13 @@ import org.springframework.core.annotation.Order;
15 19 import org.springframework.data.redis.core.ListOperations;
16 20 import org.springframework.data.redis.core.RedisTemplate;
17 21 import org.springframework.data.redis.serializer.StringRedisSerializer;
  22 +import org.springframework.stereotype.Component;
18 23 import org.springframework.stereotype.Service;
19 24  
20 25 import java.util.ArrayList;
21 26 import java.util.Iterator;
22 27 import java.util.List;
  28 +import java.util.concurrent.TimeUnit;
23 29  
24 30 /**
25 31 * 班次 redis 缓存管理
... ... @@ -40,7 +46,10 @@ public class ScheduleRedisService implements CommandLineRunner {
40 46 @Autowired
41 47 WebServiceProxy webServiceProxy;
42 48  
43   - Logger logger = LoggerFactory.getLogger(this.getClass());
  49 + @Autowired
  50 + RedisUtils redisUtils;
  51 +
  52 + static Logger logger = LoggerFactory.getLogger(ScheduleRedisService.class);
44 53  
45 54 /**
46 55 * 将一批班次写入redis
... ... @@ -61,7 +70,7 @@ public class ScheduleRedisService implements CommandLineRunner {
61 70 String key;
62 71 while (iterator.hasNext()) {
63 72 key = iterator.next();
64   - mergeData(key, multimap.get(key));
  73 + replace(key, multimap.get(key));
65 74 }
66 75 } catch (Exception e) {
67 76 logger.error("", e);
... ... @@ -84,10 +93,30 @@ public class ScheduleRedisService implements CommandLineRunner {
84 93 if (!list.contains(sch))
85 94 list.add(sch);
86 95 }
87   - //删除redis数据
88   - redisTemplate.delete(key);
89   - //重新写入
90   - ops.leftPushAll(key, list);
  96 +
  97 + //更新
  98 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  99 + }
  100 +
  101 + /**
  102 + * 覆盖数据
  103 + *
  104 + * @param key
  105 + * @param list
  106 + */
  107 + public void replace(String key, List<ScheduleRealInfo> list) {
  108 + key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
  109 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  110 + }
  111 +
  112 + /**
  113 + * 删除数据
  114 + *
  115 + * @param key
  116 + */
  117 + public void delete(String lineCode, String rq) {
  118 + String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", "");
  119 + redisTemplate.delete(key);
91 120 }
92 121  
93 122 /**
... ... @@ -103,22 +132,23 @@ public class ScheduleRedisService implements CommandLineRunner {
103 132  
104 133 /**
105 134 * 返回指定日期,公司的实际排班,并按线路_车辆分组
106   - * (新老系统并行时调用这个函数)
  135 + * (新老系统并行时调用这个函数)
  136 + *
107 137 * @param rq
108 138 * @param companyId
109 139 * @return
110 140 */
111   - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm_bingxing(String rq, String companyId){
  141 + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm_bingxing(String rq, String companyId) {
112 142 List<String> lineArray = webServiceProxy.findLinesByCompany_new(companyId);
113 143 ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
114 144  
115 145 rq = rq.replaceAll("-", "");
116 146 List<ScheduleRealInfo> list;
117   - for(String lineCode : lineArray){
  147 + for (String lineCode : lineArray) {
118 148  
119 149 list = read(rq, lineCode);
120 150  
121   - for(ScheduleRealInfo sch : list){
  151 + for (ScheduleRealInfo sch : list) {
122 152 rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch);
123 153 }
124 154 }
... ... @@ -127,21 +157,22 @@ public class ScheduleRedisService implements CommandLineRunner {
127 157  
128 158 /**
129 159 * 返回指定日期,公司的实际排班,并按线路_车辆分组
  160 + *
130 161 * @param rq
131 162 * @param companyId
132 163 * @return
133 164 */
134   - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId){
  165 + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId) {
135 166 List<String> lineArray = webServiceProxy.findLinesByCompany(companyId);
136 167 ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
137 168  
138 169 rq = rq.replaceAll("-", "");
139 170 List<ScheduleRealInfo> list;
140   - for(String lineCode : lineArray){
  171 + for (String lineCode : lineArray) {
141 172  
142 173 list = read(rq, lineCode);
143 174  
144   - for(ScheduleRealInfo sch : list){
  175 + for (ScheduleRealInfo sch : list) {
145 176 rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch);
146 177 }
147 178 }
... ... @@ -150,28 +181,34 @@ public class ScheduleRedisService implements CommandLineRunner {
150 181  
151 182 /**
152 183 * 返回指定日期,公司的实际排班,并按线路分组
  184 + *
153 185 * @param rq
154 186 * @param companyId
155 187 * @return
156 188 */
157   - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId){
  189 + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId) {
158 190  
159 191 List<String> lineArray = webServiceProxy.findLinesByCompany(companyId);
160 192 ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
161 193  
162 194 rq = rq.replaceAll("-", "");
163 195 List<ScheduleRealInfo> list;
164   - for(String lineCode : lineArray){
  196 + for (String lineCode : lineArray) {
165 197  
166 198 list = read(rq, lineCode);
167 199  
168   - for(ScheduleRealInfo sch : list){
  200 + for (ScheduleRealInfo sch : list) {
169 201 rs.put(sch.getXlBm(), sch);
170 202 }
171 203 }
172 204 return rs;
173 205 }
174 206  
  207 + @Autowired
  208 + ScheduleRefreshThread scheduleRefreshThread;
  209 + @Autowired
  210 + ScheduleClearThread scheduleClearThread;
  211 +
175 212 @Override
176 213 public void run(String... strings) throws Exception {
177 214 int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
... ... @@ -183,14 +220,65 @@ public class ScheduleRedisService implements CommandLineRunner {
183 220 List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd"));
184 221 //写入redis
185 222 wirte(list);
  223 +
  224 + //定时刷新一次当日实际排班
  225 + int minute = 30;
  226 + Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute, minute, TimeUnit.MINUTES);
  227 + //24小时清理一次实际排班数据
  228 + Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS);
186 229 }
187 230  
188 231 public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) {
189 232 rq = rq.replaceAll("-", "");
190 233 List<ScheduleRealInfo> rs = new ArrayList<>();
191   - for(String lineCode : lineArray){
  234 + for (String lineCode : lineArray) {
192 235 rs.addAll(read(rq, lineCode));
193 236 }
194 237 return rs;
195 238 }
  239 +
  240 + @Component
  241 + public static class ScheduleRefreshThread extends Thread {
  242 +
  243 + @Autowired
  244 + ScheduleRealInfoRepository realInfoRepository;
  245 +
  246 + @Autowired
  247 + ScheduleRedisService scheduleRedisService;
  248 +
  249 + @Override
  250 + public void run() {
  251 + try {
  252 + DateTime dt = new DateTime();
  253 + List<ScheduleRealInfo> list = realInfoRepository.findAll(dt.toString("yyyy-MM-dd"));
  254 + scheduleRedisService.wirte(list);
  255 + } catch (Exception e) {
  256 + logger.error("", e);
  257 + }
  258 + }
  259 + }
  260 +
  261 + @Component
  262 + public static class ScheduleClearThread extends Thread {
  263 +
  264 + @Autowired
  265 + ScheduleRedisService scheduleRedisService;
  266 +
  267 + @Override
  268 + public void run() {
  269 + try {
  270 + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
  271 + DateTime dt = new DateTime();
  272 + dt = dt.minusDays(cacheDays);
  273 + String rq = dt.toString("yyyy-MM-dd");
  274 +
  275 + List<Line> lines = LineBufferData.findAll();
  276 + for (Line line : lines) {
  277 + scheduleRedisService.delete(line.getLineCode(), rq);
  278 + }
  279 + } catch (Exception e) {
  280 + logger.error("", e);
  281 + }
  282 + }
  283 + }
196 284 }
... ...
src/main/java/com/bsth/redis/util/RedisUtils.java 0 → 100644
  1 +package com.bsth.redis.util;
  2 +
  3 +import org.springframework.dao.DataAccessException;
  4 +import org.springframework.data.redis.core.RedisOperations;
  5 +import org.springframework.data.redis.core.SessionCallback;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +import java.util.List;
  9 +
  10 +/**
  11 + * Created by panzhao on 2017/3/30.
  12 + */
  13 +@Component
  14 +public class RedisUtils<T> {
  15 +
  16 + public SessionCallback getUpdateCallback(final String key, final List<T> list){
  17 +
  18 + SessionCallback<Object> sessionCallback = new SessionCallback<Object>() {
  19 +
  20 + @Override
  21 + public Object execute(RedisOperations redisOperations) throws DataAccessException {
  22 + redisOperations.multi();
  23 + redisOperations.delete(key);
  24 + redisOperations.opsForList().leftPushAll(key, list);
  25 + return redisOperations.exec();
  26 + }
  27 + };
  28 +
  29 + return sessionCallback;
  30 + }
  31 +}
... ...
src/main/java/com/bsth/server_rs/AuthorizeInterceptor_IN.java
... ... @@ -57,7 +57,7 @@ public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor&lt;Message&gt; i
57 57 return ;
58 58 }
59 59  
60   - //获取参数,不包括 url 路径参数 ?号之后的
  60 + //获取参数,不包括 url 路径参数 只包括?号之后的
61 61 String queryString = StringEscapeUtils.unescapeHtml4(message.get(Message.QUERY_STRING).toString());
62 62 MultiMap<String> params = new MultiMap<>();
63 63 UrlEncoded.decodeTo(queryString, params, "utf-8");
... ...