Commit 735fa87251067b133d9487a573103df482a56858
1 parent
7438cf1e
1.chengdu分支配置变更
Showing
11 changed files
with
1076 additions
and
1132 deletions
src/main/java/com/bsth/redis/ElecRedisService.java
| 1 | -package com.bsth.redis; | |
| 2 | - | |
| 3 | -import com.bsth.Application; | |
| 4 | -import com.bsth.entity.ElecInfo; | |
| 5 | -import com.bsth.redis.util.RedisUtils; | |
| 6 | -import com.bsth.repository.ElecInfoRepository; | |
| 7 | -import com.bsth.util.ConfigUtil; | |
| 8 | -import com.bsth.util.ConvertUtil; | |
| 9 | -import com.google.common.collect.ArrayListMultimap; | |
| 10 | -import org.joda.time.DateTime; | |
| 11 | -import org.slf4j.Logger; | |
| 12 | -import org.slf4j.LoggerFactory; | |
| 13 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | -import org.springframework.boot.CommandLineRunner; | |
| 15 | -import org.springframework.core.annotation.Order; | |
| 16 | -import org.springframework.data.redis.core.RedisTemplate; | |
| 17 | -import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 18 | -import org.springframework.stereotype.Component; | |
| 19 | -import org.springframework.stereotype.Service; | |
| 20 | - | |
| 21 | -import java.util.*; | |
| 22 | -import java.util.concurrent.TimeUnit; | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * 油量数据Redis缓存 | |
| 26 | - * Created by panzhao on 2017/3/19. | |
| 27 | - */ | |
| 28 | -@Service | |
| 29 | -@Order(3) | |
| 30 | -public class ElecRedisService implements CommandLineRunner { | |
| 31 | - | |
| 32 | - @Autowired | |
| 33 | - private RedisTemplate redisTemplate; | |
| 34 | - | |
| 35 | - @Autowired | |
| 36 | - ElecInfoRepository elecInfoRepository; | |
| 37 | - | |
| 38 | - @Autowired | |
| 39 | - RedisUtils redisUtils; | |
| 40 | - | |
| 41 | - static Logger logger = LoggerFactory.getLogger(ElecRedisService.class); | |
| 42 | - | |
| 43 | - private final static String REDIS_KEY_PREFIX = "elec:"; | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * 将油量数据写入redis | |
| 47 | - * | |
| 48 | - * @param list | |
| 49 | - */ | |
| 50 | - public void wirte(List<ElecInfo> list) { | |
| 51 | - ArrayListMultimap<String, ElecInfo> multimap; | |
| 52 | - try { | |
| 53 | - if (list.size() == 0) | |
| 54 | - return; | |
| 55 | - //按日期和线路分组数据 | |
| 56 | - Class clazz = ElecInfo.class; | |
| 57 | - multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq")); | |
| 58 | - | |
| 59 | - //写入redis | |
| 60 | - Iterator<String> iterator = multimap.keySet().iterator(); | |
| 61 | - String key; | |
| 62 | - while (iterator.hasNext()) { | |
| 63 | - key = iterator.next(); | |
| 64 | - mergeData(key, multimap.get(key)); | |
| 65 | - } | |
| 66 | - } catch (Exception e) { | |
| 67 | - logger.error("", e); | |
| 68 | - } | |
| 69 | - } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key | |
| 73 | - * | |
| 74 | - * @param nbbmArray | |
| 75 | - * @param rq | |
| 76 | - * @return | |
| 77 | - */ | |
| 78 | - public ArrayListMultimap findByNbbmGroup(Iterable<String> nbbmArray, String rq) { | |
| 79 | - ArrayListMultimap rs = ArrayListMultimap.create(); | |
| 80 | - | |
| 81 | - rq = rq.replaceAll("-", ""); | |
| 82 | - try { | |
| 83 | - List<ElecInfo> list = new ArrayList<>(); | |
| 84 | - for (String nbbm : nbbmArray) { | |
| 85 | - nbbm = nbbm.split("_")[1]; | |
| 86 | - list.addAll(read(nbbm, rq)); | |
| 87 | - } | |
| 88 | - Class clazz = ElecInfo.class; | |
| 89 | - rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy")); | |
| 90 | - } catch (Exception e) { | |
| 91 | - logger.error("", e); | |
| 92 | - } | |
| 93 | - return rs; | |
| 94 | - } | |
| 95 | - | |
| 96 | - /** | |
| 97 | - * 根据车辆和日期获取油耗数据,以 车辆 为key | |
| 98 | - * | |
| 99 | - * @param nbbmArray | |
| 100 | - * @param rq | |
| 101 | - * @return | |
| 102 | - */ | |
| 103 | - public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) { | |
| 104 | - ArrayListMultimap rs = ArrayListMultimap.create(); | |
| 105 | - | |
| 106 | - rq = rq.replaceAll("-", ""); | |
| 107 | - try { | |
| 108 | - List<ElecInfo> list = new ArrayList<>(); | |
| 109 | - for (String nbbm : nbbmArray) { | |
| 110 | - list.addAll(read(nbbm, rq)); | |
| 111 | - } | |
| 112 | - Class clazz = ElecInfo.class; | |
| 113 | - rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm")); | |
| 114 | - } catch (Exception e) { | |
| 115 | - logger.error("", e); | |
| 116 | - } | |
| 117 | - return rs; | |
| 118 | - } | |
| 119 | - | |
| 120 | - public List<ElecInfo> read(String nbbm, String rq) { | |
| 121 | - return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * 将 list 与redis里的数据合并 | |
| 126 | - * | |
| 127 | - * @param key | |
| 128 | - * @param list | |
| 129 | - */ | |
| 130 | - public void mergeData(String key, List<ElecInfo> list) { | |
| 131 | - key = REDIS_KEY_PREFIX + key; | |
| 132 | - | |
| 133 | - //更新 直接覆盖更新 | |
| 134 | - redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 135 | - } | |
| 136 | - | |
| 137 | - @Autowired | |
| 138 | - ElecRefreshThread elecRefreshThread; | |
| 139 | - | |
| 140 | - @Override | |
| 141 | - public void run(String... strings) throws Exception { | |
| 142 | - Application.mainServices.schedule(new Runnable() { | |
| 143 | - @Override | |
| 144 | - public void run() { | |
| 145 | - //启动加载油耗缓存 | |
| 146 | - synchData(null); | |
| 147 | - } | |
| 148 | - }, 30, TimeUnit.SECONDS); | |
| 149 | - | |
| 150 | - | |
| 151 | - //定时刷新油耗信息 | |
| 152 | - Application.mainServices.scheduleWithFixedDelay(elecRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS); | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * 和数据库同步数据 | |
| 157 | - */ | |
| 158 | - public void synchData(Integer days) { | |
| 159 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 160 | - if (null != days && days < cacheDays) | |
| 161 | - cacheDays = days; | |
| 162 | - //设置key 序列化器 | |
| 163 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 164 | - | |
| 165 | - DateTime dt = new DateTime(); | |
| 166 | - dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | |
| 167 | - List<ElecInfo> list = elecInfoRepository.findByDateLT(dt.toDate()); | |
| 168 | - //写入redis | |
| 169 | - wirte(list); | |
| 170 | - logger.info("刷新电耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd")); | |
| 171 | - } | |
| 172 | - | |
| 173 | - @Component | |
| 174 | - public static class ElecRefreshThread extends Thread { | |
| 175 | - | |
| 176 | - @Autowired | |
| 177 | - ElecRedisService elecRedisService; | |
| 178 | - | |
| 179 | - @Override | |
| 180 | - public void run() { | |
| 181 | - try { | |
| 182 | - elecRedisService.synchData(5); | |
| 183 | - } catch (Exception e) { | |
| 184 | - logger.error("", e); | |
| 185 | - } | |
| 186 | - } | |
| 187 | - } | |
| 188 | -} | |
| 1 | +package com.bsth.redis; | |
| 2 | + | |
| 3 | +import com.bsth.Application; | |
| 4 | +import com.bsth.entity.ElecInfo; | |
| 5 | +import com.bsth.redis.util.RedisUtils; | |
| 6 | +import com.bsth.repository.ElecInfoRepository; | |
| 7 | +import com.bsth.util.AppProperties; | |
| 8 | +import com.bsth.util.ConvertUtil; | |
| 9 | +import com.google.common.collect.ArrayListMultimap; | |
| 10 | +import org.joda.time.DateTime; | |
| 11 | +import org.slf4j.Logger; | |
| 12 | +import org.slf4j.LoggerFactory; | |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | +import org.springframework.boot.CommandLineRunner; | |
| 15 | +import org.springframework.core.annotation.Order; | |
| 16 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 17 | +import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 18 | +import org.springframework.stereotype.Component; | |
| 19 | +import org.springframework.stereotype.Service; | |
| 20 | + | |
| 21 | +import java.util.*; | |
| 22 | +import java.util.concurrent.TimeUnit; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * 油量数据Redis缓存 | |
| 26 | + * Created by panzhao on 2017/3/19. | |
| 27 | + */ | |
| 28 | +@Service | |
| 29 | +@Order(3) | |
| 30 | +public class ElecRedisService implements CommandLineRunner { | |
| 31 | + | |
| 32 | + @Autowired | |
| 33 | + private RedisTemplate redisTemplate; | |
| 34 | + | |
| 35 | + @Autowired | |
| 36 | + ElecInfoRepository elecInfoRepository; | |
| 37 | + | |
| 38 | + @Autowired | |
| 39 | + RedisUtils redisUtils; | |
| 40 | + | |
| 41 | + static Logger logger = LoggerFactory.getLogger(ElecRedisService.class); | |
| 42 | + | |
| 43 | + private final static String REDIS_KEY_PREFIX = "elec:"; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * 将油量数据写入redis | |
| 47 | + * | |
| 48 | + * @param list | |
| 49 | + */ | |
| 50 | + public void wirte(List<ElecInfo> list) { | |
| 51 | + ArrayListMultimap<String, ElecInfo> multimap; | |
| 52 | + try { | |
| 53 | + if (list.size() == 0) | |
| 54 | + return; | |
| 55 | + //按日期和线路分组数据 | |
| 56 | + Class clazz = ElecInfo.class; | |
| 57 | + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq")); | |
| 58 | + | |
| 59 | + //写入redis | |
| 60 | + Iterator<String> iterator = multimap.keySet().iterator(); | |
| 61 | + String key; | |
| 62 | + while (iterator.hasNext()) { | |
| 63 | + key = iterator.next(); | |
| 64 | + mergeData(key, multimap.get(key)); | |
| 65 | + } | |
| 66 | + } catch (Exception e) { | |
| 67 | + logger.error("", e); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key | |
| 73 | + * | |
| 74 | + * @param nbbmArray | |
| 75 | + * @param rq | |
| 76 | + * @return | |
| 77 | + */ | |
| 78 | + public ArrayListMultimap findByNbbmGroup(Iterable<String> nbbmArray, String rq) { | |
| 79 | + ArrayListMultimap rs = ArrayListMultimap.create(); | |
| 80 | + | |
| 81 | + rq = rq.replaceAll("-", ""); | |
| 82 | + try { | |
| 83 | + List<ElecInfo> list = new ArrayList<>(); | |
| 84 | + for (String nbbm : nbbmArray) { | |
| 85 | + nbbm = nbbm.split("_")[1]; | |
| 86 | + list.addAll(read(nbbm, rq)); | |
| 87 | + } | |
| 88 | + Class clazz = ElecInfo.class; | |
| 89 | + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy")); | |
| 90 | + } catch (Exception e) { | |
| 91 | + logger.error("", e); | |
| 92 | + } | |
| 93 | + return rs; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * 根据车辆和日期获取油耗数据,以 车辆 为key | |
| 98 | + * | |
| 99 | + * @param nbbmArray | |
| 100 | + * @param rq | |
| 101 | + * @return | |
| 102 | + */ | |
| 103 | + public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) { | |
| 104 | + ArrayListMultimap rs = ArrayListMultimap.create(); | |
| 105 | + | |
| 106 | + rq = rq.replaceAll("-", ""); | |
| 107 | + try { | |
| 108 | + List<ElecInfo> list = new ArrayList<>(); | |
| 109 | + for (String nbbm : nbbmArray) { | |
| 110 | + list.addAll(read(nbbm, rq)); | |
| 111 | + } | |
| 112 | + Class clazz = ElecInfo.class; | |
| 113 | + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm")); | |
| 114 | + } catch (Exception e) { | |
| 115 | + logger.error("", e); | |
| 116 | + } | |
| 117 | + return rs; | |
| 118 | + } | |
| 119 | + | |
| 120 | + public List<ElecInfo> read(String nbbm, String rq) { | |
| 121 | + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1); | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * 将 list 与redis里的数据合并 | |
| 126 | + * | |
| 127 | + * @param key | |
| 128 | + * @param list | |
| 129 | + */ | |
| 130 | + public void mergeData(String key, List<ElecInfo> list) { | |
| 131 | + key = REDIS_KEY_PREFIX + key; | |
| 132 | + | |
| 133 | + //更新 直接覆盖更新 | |
| 134 | + redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 135 | + } | |
| 136 | + | |
| 137 | + @Autowired | |
| 138 | + ElecRefreshThread elecRefreshThread; | |
| 139 | + | |
| 140 | + @Override | |
| 141 | + public void run(String... strings) throws Exception { | |
| 142 | + Application.mainServices.schedule(new Runnable() { | |
| 143 | + @Override | |
| 144 | + public void run() { | |
| 145 | + //启动加载油耗缓存 | |
| 146 | + synchData(null); | |
| 147 | + } | |
| 148 | + }, 30, TimeUnit.SECONDS); | |
| 149 | + | |
| 150 | + | |
| 151 | + //定时刷新油耗信息 | |
| 152 | + Application.mainServices.scheduleWithFixedDelay(elecRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS); | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * 和数据库同步数据 | |
| 157 | + */ | |
| 158 | + public void synchData(Integer days) { | |
| 159 | + int cacheDays = AppProperties.getCacheDays(); | |
| 160 | + if (null != days && days < cacheDays) | |
| 161 | + cacheDays = days; | |
| 162 | + //设置key 序列化器 | |
| 163 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 164 | + | |
| 165 | + DateTime dt = new DateTime(); | |
| 166 | + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | |
| 167 | + List<ElecInfo> list = elecInfoRepository.findByDateLT(dt.toDate()); | |
| 168 | + //写入redis | |
| 169 | + wirte(list); | |
| 170 | + logger.info("刷新电耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd")); | |
| 171 | + } | |
| 172 | + | |
| 173 | + @Component | |
| 174 | + public static class ElecRefreshThread extends Thread { | |
| 175 | + | |
| 176 | + @Autowired | |
| 177 | + ElecRedisService elecRedisService; | |
| 178 | + | |
| 179 | + @Override | |
| 180 | + public void run() { | |
| 181 | + try { | |
| 182 | + elecRedisService.synchData(5); | |
| 183 | + } catch (Exception e) { | |
| 184 | + logger.error("", e); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + } | |
| 188 | +} | ... | ... |
src/main/java/com/bsth/redis/OilRedisService.java
| 1 | -package com.bsth.redis; | |
| 2 | - | |
| 3 | -import com.bsth.Application; | |
| 4 | -import com.bsth.entity.OilInfo; | |
| 5 | -import com.bsth.redis.util.RedisUtils; | |
| 6 | -import com.bsth.repository.OilInfoRepository; | |
| 7 | -import com.bsth.util.ConfigUtil; | |
| 8 | -import com.bsth.util.ConvertUtil; | |
| 9 | -import com.google.common.collect.ArrayListMultimap; | |
| 10 | -import org.joda.time.DateTime; | |
| 11 | -import org.slf4j.Logger; | |
| 12 | -import org.slf4j.LoggerFactory; | |
| 13 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | -import org.springframework.boot.CommandLineRunner; | |
| 15 | -import org.springframework.core.annotation.Order; | |
| 16 | -import org.springframework.data.redis.core.RedisTemplate; | |
| 17 | -import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 18 | -import org.springframework.stereotype.Component; | |
| 19 | -import org.springframework.stereotype.Service; | |
| 20 | - | |
| 21 | -import java.util.*; | |
| 22 | -import java.util.concurrent.TimeUnit; | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * 油量数据Redis缓存 | |
| 26 | - * Created by panzhao on 2017/3/19. | |
| 27 | - */ | |
| 28 | -@Service | |
| 29 | -@Order(3) | |
| 30 | -public class OilRedisService implements CommandLineRunner { | |
| 31 | - | |
| 32 | - @Autowired | |
| 33 | - private RedisTemplate redisTemplate; | |
| 34 | - | |
| 35 | - @Autowired | |
| 36 | - OilInfoRepository oilInfoRepository; | |
| 37 | - | |
| 38 | - @Autowired | |
| 39 | - RedisUtils redisUtils; | |
| 40 | - | |
| 41 | - static Logger logger = LoggerFactory.getLogger(OilRedisService.class); | |
| 42 | - | |
| 43 | - private final static String REDIS_KEY_PREFIX = "oil:"; | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * 将油量数据写入redis | |
| 47 | - * | |
| 48 | - * @param list | |
| 49 | - */ | |
| 50 | - public void wirte(List<OilInfo> list) { | |
| 51 | - ArrayListMultimap<String, OilInfo> multimap; | |
| 52 | - try { | |
| 53 | - if (list.size() == 0) | |
| 54 | - return; | |
| 55 | - //按日期和线路分组数据 | |
| 56 | - Class clazz = OilInfo.class; | |
| 57 | - multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq")); | |
| 58 | - | |
| 59 | - //写入redis | |
| 60 | - Iterator<String> iterator = multimap.keySet().iterator(); | |
| 61 | - String key; | |
| 62 | - while (iterator.hasNext()) { | |
| 63 | - key = iterator.next(); | |
| 64 | - mergeData(key, multimap.get(key)); | |
| 65 | - } | |
| 66 | - } catch (Exception e) { | |
| 67 | - logger.error("", e); | |
| 68 | - } | |
| 69 | - } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key | |
| 73 | - * | |
| 74 | - * @param nbbmArray | |
| 75 | - * @param rq | |
| 76 | - * @return | |
| 77 | - */ | |
| 78 | - public ArrayListMultimap<String, OilInfo> findByNbbmGroup(Iterable<String> nbbmArray, String rq) { | |
| 79 | - ArrayListMultimap<String, OilInfo> rs = ArrayListMultimap.create(); | |
| 80 | - | |
| 81 | - rq = rq.replaceAll("-", ""); | |
| 82 | - try { | |
| 83 | - List<OilInfo> list = new ArrayList<>(); | |
| 84 | - Set<String> nbbms = new HashSet<>(); | |
| 85 | - for (String nbbm : nbbmArray) { | |
| 86 | - nbbm = nbbm.split("_")[1]; | |
| 87 | - if (!nbbms.contains(nbbm)) { | |
| 88 | - nbbms.add(nbbm); | |
| 89 | - list.addAll(read(nbbm, rq)); | |
| 90 | - } | |
| 91 | - } | |
| 92 | - Class clazz = OilInfo.class; | |
| 93 | - rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy")); | |
| 94 | - } catch (Exception e) { | |
| 95 | - logger.error("", e); | |
| 96 | - } | |
| 97 | - return rs; | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key | |
| 102 | - * | |
| 103 | - * @param nbbmArray | |
| 104 | - * @param rq | |
| 105 | - * @return | |
| 106 | - */ | |
| 107 | - public Map<String, OilInfo> findByNbbmGroupRaw(Iterable<String> nbbmArray, String rq) { | |
| 108 | - Map<String, OilInfo> rs = new HashMap<>(); | |
| 109 | - | |
| 110 | - rq = rq.replaceAll("-", ""); | |
| 111 | - try { | |
| 112 | - List<OilInfo> list = new ArrayList<>(); | |
| 113 | - for (String nbbm : nbbmArray) { | |
| 114 | - nbbm = nbbm.split("_")[1]; | |
| 115 | - list.addAll(read(nbbm, rq)); | |
| 116 | - } | |
| 117 | - Class clazz = OilInfo.class; | |
| 118 | - rs = new ConvertUtil().groupList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy")); | |
| 119 | - } catch (Exception e) { | |
| 120 | - logger.error("", e); | |
| 121 | - } | |
| 122 | - return rs; | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * 根据车辆和日期获取油耗数据,以 车辆 为key | |
| 127 | - * | |
| 128 | - * @param nbbmArray | |
| 129 | - * @param rq | |
| 130 | - * @return | |
| 131 | - */ | |
| 132 | - public ArrayListMultimap<String, OilInfo> findByNbbmGroup1(Iterable<String> nbbmArray, String rq) { | |
| 133 | - ArrayListMultimap<String, OilInfo> rs = ArrayListMultimap.create(); | |
| 134 | - | |
| 135 | - rq = rq.replaceAll("-", ""); | |
| 136 | - try { | |
| 137 | - List<OilInfo> list = new ArrayList<>(); | |
| 138 | - Set<String> nbbms = new HashSet<>(); | |
| 139 | - for (String nbbm : nbbmArray) { | |
| 140 | - if (!nbbms.contains(nbbm)) { | |
| 141 | - nbbms.add(nbbm); | |
| 142 | - list.addAll(read(nbbm, rq)); | |
| 143 | - } | |
| 144 | - } | |
| 145 | - Class clazz = OilInfo.class; | |
| 146 | - rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm")); | |
| 147 | - } catch (Exception e) { | |
| 148 | - logger.error("", e); | |
| 149 | - } | |
| 150 | - return rs; | |
| 151 | - } | |
| 152 | - | |
| 153 | - public List<OilInfo> read(String nbbm, String rq) { | |
| 154 | - return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1); | |
| 155 | - } | |
| 156 | - | |
| 157 | - /** | |
| 158 | - * 将 list 与redis里的数据合并 | |
| 159 | - * | |
| 160 | - * @param key | |
| 161 | - * @param list | |
| 162 | - */ | |
| 163 | - public void mergeData(String key, List<OilInfo> list) { | |
| 164 | - key = REDIS_KEY_PREFIX + key; | |
| 165 | - | |
| 166 | - //更新 直接覆盖更新 | |
| 167 | - redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 168 | - } | |
| 169 | - | |
| 170 | - @Autowired | |
| 171 | - OilRefreshThread oilRefreshThread; | |
| 172 | - | |
| 173 | - @Override | |
| 174 | - public void run(String... strings) throws Exception { | |
| 175 | - Application.mainServices.schedule(new Runnable() { | |
| 176 | - @Override | |
| 177 | - public void run() { | |
| 178 | - //启动加载油耗缓存 | |
| 179 | - synchData(null); | |
| 180 | - } | |
| 181 | - }, 30, TimeUnit.SECONDS); | |
| 182 | - | |
| 183 | - | |
| 184 | - //定时刷新油耗信息 | |
| 185 | - Application.mainServices.scheduleWithFixedDelay(oilRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS); | |
| 186 | - } | |
| 187 | - | |
| 188 | - /** | |
| 189 | - * 和数据库同步数据 | |
| 190 | - */ | |
| 191 | - public void synchData(Integer days) { | |
| 192 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 193 | - if (null != days && days < cacheDays) | |
| 194 | - cacheDays = days; | |
| 195 | - //设置key 序列化器 | |
| 196 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 197 | - | |
| 198 | - DateTime dt = new DateTime(); | |
| 199 | - dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | |
| 200 | - List<OilInfo> list = oilInfoRepository.findByDateLT(dt.toDate()); | |
| 201 | - //写入redis | |
| 202 | - wirte(list); | |
| 203 | - logger.info("刷新油耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd")); | |
| 204 | - } | |
| 205 | - | |
| 206 | - @Component | |
| 207 | - public static class OilRefreshThread extends Thread { | |
| 208 | - | |
| 209 | - @Autowired | |
| 210 | - OilRedisService oilRedisService; | |
| 211 | - | |
| 212 | - @Override | |
| 213 | - public void run() { | |
| 214 | - try { | |
| 215 | - oilRedisService.synchData(5); | |
| 216 | - } catch (Exception e) { | |
| 217 | - logger.error("", e); | |
| 218 | - } | |
| 219 | - } | |
| 220 | - } | |
| 221 | -} | |
| 1 | +package com.bsth.redis; | |
| 2 | + | |
| 3 | +import com.bsth.Application; | |
| 4 | +import com.bsth.entity.OilInfo; | |
| 5 | +import com.bsth.redis.util.RedisUtils; | |
| 6 | +import com.bsth.repository.OilInfoRepository; | |
| 7 | +import com.bsth.util.AppProperties; | |
| 8 | +import com.bsth.util.ConvertUtil; | |
| 9 | +import com.google.common.collect.ArrayListMultimap; | |
| 10 | +import org.joda.time.DateTime; | |
| 11 | +import org.slf4j.Logger; | |
| 12 | +import org.slf4j.LoggerFactory; | |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | +import org.springframework.boot.CommandLineRunner; | |
| 15 | +import org.springframework.core.annotation.Order; | |
| 16 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 17 | +import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 18 | +import org.springframework.stereotype.Component; | |
| 19 | +import org.springframework.stereotype.Service; | |
| 20 | + | |
| 21 | +import java.util.*; | |
| 22 | +import java.util.concurrent.TimeUnit; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * 油量数据Redis缓存 | |
| 26 | + * Created by panzhao on 2017/3/19. | |
| 27 | + */ | |
| 28 | +@Service | |
| 29 | +@Order(3) | |
| 30 | +public class OilRedisService implements CommandLineRunner { | |
| 31 | + | |
| 32 | + @Autowired | |
| 33 | + private RedisTemplate redisTemplate; | |
| 34 | + | |
| 35 | + @Autowired | |
| 36 | + OilInfoRepository oilInfoRepository; | |
| 37 | + | |
| 38 | + @Autowired | |
| 39 | + RedisUtils redisUtils; | |
| 40 | + | |
| 41 | + static Logger logger = LoggerFactory.getLogger(OilRedisService.class); | |
| 42 | + | |
| 43 | + private final static String REDIS_KEY_PREFIX = "oil:"; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * 将油量数据写入redis | |
| 47 | + * | |
| 48 | + * @param list | |
| 49 | + */ | |
| 50 | + public void wirte(List<OilInfo> list) { | |
| 51 | + ArrayListMultimap<String, OilInfo> multimap; | |
| 52 | + try { | |
| 53 | + if (list.size() == 0) | |
| 54 | + return; | |
| 55 | + //按日期和线路分组数据 | |
| 56 | + Class clazz = OilInfo.class; | |
| 57 | + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq")); | |
| 58 | + | |
| 59 | + //写入redis | |
| 60 | + Iterator<String> iterator = multimap.keySet().iterator(); | |
| 61 | + String key; | |
| 62 | + while (iterator.hasNext()) { | |
| 63 | + key = iterator.next(); | |
| 64 | + mergeData(key, multimap.get(key)); | |
| 65 | + } | |
| 66 | + } catch (Exception e) { | |
| 67 | + logger.error("", e); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key | |
| 73 | + * | |
| 74 | + * @param nbbmArray | |
| 75 | + * @param rq | |
| 76 | + * @return | |
| 77 | + */ | |
| 78 | + public ArrayListMultimap<String, OilInfo> findByNbbmGroup(Iterable<String> nbbmArray, String rq) { | |
| 79 | + ArrayListMultimap<String, OilInfo> rs = ArrayListMultimap.create(); | |
| 80 | + | |
| 81 | + rq = rq.replaceAll("-", ""); | |
| 82 | + try { | |
| 83 | + List<OilInfo> list = new ArrayList<>(); | |
| 84 | + Set<String> nbbms = new HashSet<>(); | |
| 85 | + for (String nbbm : nbbmArray) { | |
| 86 | + nbbm = nbbm.split("_")[1]; | |
| 87 | + if (!nbbms.contains(nbbm)) { | |
| 88 | + nbbms.add(nbbm); | |
| 89 | + list.addAll(read(nbbm, rq)); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + Class clazz = OilInfo.class; | |
| 93 | + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy")); | |
| 94 | + } catch (Exception e) { | |
| 95 | + logger.error("", e); | |
| 96 | + } | |
| 97 | + return rs; | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key | |
| 102 | + * | |
| 103 | + * @param nbbmArray | |
| 104 | + * @param rq | |
| 105 | + * @return | |
| 106 | + */ | |
| 107 | + public Map<String, OilInfo> findByNbbmGroupRaw(Iterable<String> nbbmArray, String rq) { | |
| 108 | + Map<String, OilInfo> rs = new HashMap<>(); | |
| 109 | + | |
| 110 | + rq = rq.replaceAll("-", ""); | |
| 111 | + try { | |
| 112 | + List<OilInfo> list = new ArrayList<>(); | |
| 113 | + for (String nbbm : nbbmArray) { | |
| 114 | + nbbm = nbbm.split("_")[1]; | |
| 115 | + list.addAll(read(nbbm, rq)); | |
| 116 | + } | |
| 117 | + Class clazz = OilInfo.class; | |
| 118 | + rs = new ConvertUtil().groupList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy")); | |
| 119 | + } catch (Exception e) { | |
| 120 | + logger.error("", e); | |
| 121 | + } | |
| 122 | + return rs; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * 根据车辆和日期获取油耗数据,以 车辆 为key | |
| 127 | + * | |
| 128 | + * @param nbbmArray | |
| 129 | + * @param rq | |
| 130 | + * @return | |
| 131 | + */ | |
| 132 | + public ArrayListMultimap<String, OilInfo> findByNbbmGroup1(Iterable<String> nbbmArray, String rq) { | |
| 133 | + ArrayListMultimap<String, OilInfo> rs = ArrayListMultimap.create(); | |
| 134 | + | |
| 135 | + rq = rq.replaceAll("-", ""); | |
| 136 | + try { | |
| 137 | + List<OilInfo> list = new ArrayList<>(); | |
| 138 | + Set<String> nbbms = new HashSet<>(); | |
| 139 | + for (String nbbm : nbbmArray) { | |
| 140 | + if (!nbbms.contains(nbbm)) { | |
| 141 | + nbbms.add(nbbm); | |
| 142 | + list.addAll(read(nbbm, rq)); | |
| 143 | + } | |
| 144 | + } | |
| 145 | + Class clazz = OilInfo.class; | |
| 146 | + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm")); | |
| 147 | + } catch (Exception e) { | |
| 148 | + logger.error("", e); | |
| 149 | + } | |
| 150 | + return rs; | |
| 151 | + } | |
| 152 | + | |
| 153 | + public List<OilInfo> read(String nbbm, String rq) { | |
| 154 | + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1); | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * 将 list 与redis里的数据合并 | |
| 159 | + * | |
| 160 | + * @param key | |
| 161 | + * @param list | |
| 162 | + */ | |
| 163 | + public void mergeData(String key, List<OilInfo> list) { | |
| 164 | + key = REDIS_KEY_PREFIX + key; | |
| 165 | + | |
| 166 | + //更新 直接覆盖更新 | |
| 167 | + redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 168 | + } | |
| 169 | + | |
| 170 | + @Autowired | |
| 171 | + OilRefreshThread oilRefreshThread; | |
| 172 | + | |
| 173 | + @Override | |
| 174 | + public void run(String... strings) throws Exception { | |
| 175 | + Application.mainServices.schedule(new Runnable() { | |
| 176 | + @Override | |
| 177 | + public void run() { | |
| 178 | + //启动加载油耗缓存 | |
| 179 | + synchData(null); | |
| 180 | + } | |
| 181 | + }, 30, TimeUnit.SECONDS); | |
| 182 | + | |
| 183 | + | |
| 184 | + //定时刷新油耗信息 | |
| 185 | + Application.mainServices.scheduleWithFixedDelay(oilRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS); | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * 和数据库同步数据 | |
| 190 | + */ | |
| 191 | + public void synchData(Integer days) { | |
| 192 | + int cacheDays = AppProperties.getCacheDays(); | |
| 193 | + if (null != days && days < cacheDays) | |
| 194 | + cacheDays = days; | |
| 195 | + //设置key 序列化器 | |
| 196 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 197 | + | |
| 198 | + DateTime dt = new DateTime(); | |
| 199 | + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | |
| 200 | + List<OilInfo> list = oilInfoRepository.findByDateLT(dt.toDate()); | |
| 201 | + //写入redis | |
| 202 | + wirte(list); | |
| 203 | + logger.info("刷新油耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd")); | |
| 204 | + } | |
| 205 | + | |
| 206 | + @Component | |
| 207 | + public static class OilRefreshThread extends Thread { | |
| 208 | + | |
| 209 | + @Autowired | |
| 210 | + OilRedisService oilRedisService; | |
| 211 | + | |
| 212 | + @Override | |
| 213 | + public void run() { | |
| 214 | + try { | |
| 215 | + oilRedisService.synchData(5); | |
| 216 | + } catch (Exception e) { | |
| 217 | + logger.error("", e); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + } | |
| 221 | +} | ... | ... |
src/main/java/com/bsth/redis/PlanScheduleRedisService.java
| ... | ... | @@ -7,7 +7,7 @@ import com.bsth.redis.util.RedisUtils; |
| 7 | 7 | import com.bsth.repository.SchedulePlanInfoRepository; |
| 8 | 8 | import com.bsth.server_rs.base_info.line.Line; |
| 9 | 9 | import com.bsth.server_rs.base_info.line.buffer.LineBufferData; |
| 10 | -import com.bsth.util.ConfigUtil; | |
| 10 | +import com.bsth.util.AppProperties; | |
| 11 | 11 | import com.bsth.util.ConvertUtil; |
| 12 | 12 | import com.google.common.collect.ArrayListMultimap; |
| 13 | 13 | import org.joda.time.DateTime; |
| ... | ... | @@ -23,7 +23,6 @@ import org.springframework.stereotype.Component; |
| 23 | 23 | import org.springframework.stereotype.Service; |
| 24 | 24 | |
| 25 | 25 | import java.util.ArrayList; |
| 26 | -import java.util.Date; | |
| 27 | 26 | import java.util.Iterator; |
| 28 | 27 | import java.util.List; |
| 29 | 28 | import java.util.concurrent.TimeUnit; |
| ... | ... | @@ -127,7 +126,7 @@ public class PlanScheduleRedisService implements CommandLineRunner { |
| 127 | 126 | Application.mainServices.schedule(new Runnable() { |
| 128 | 127 | @Override |
| 129 | 128 | public void run() { |
| 130 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 129 | + int cacheDays = AppProperties.getCacheDays(); | |
| 131 | 130 | //设置key 序列化器 |
| 132 | 131 | redisTemplate.setKeySerializer(new StringRedisSerializer()); |
| 133 | 132 | |
| ... | ... | @@ -169,7 +168,7 @@ public class PlanScheduleRedisService implements CommandLineRunner { |
| 169 | 168 | try { |
| 170 | 169 | logger.info("redis -清理计划排班"); |
| 171 | 170 | |
| 172 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 171 | + int cacheDays = AppProperties.getCacheDays(); | |
| 173 | 172 | DateTime dt = new DateTime(); |
| 174 | 173 | dt = dt.minusDays(cacheDays); |
| 175 | 174 | String rq = dt.toString("yyyy-MM-dd"); | ... | ... |
src/main/java/com/bsth/redis/ScheduleRedisService.java
| 1 | -package com.bsth.redis; | |
| 2 | - | |
| 3 | -import com.bsth.Application; | |
| 4 | -import com.bsth.common.BasicData; | |
| 5 | -import com.bsth.entity.ScheduleRealInfo; | |
| 6 | -import com.bsth.redis.util.RedisUtils; | |
| 7 | -import com.bsth.repository.ScheduleRealInfoRepository; | |
| 8 | -import com.bsth.server_rs.base_info.line.Line; | |
| 9 | -import com.bsth.server_rs.base_info.line.buffer.LineBufferData; | |
| 10 | -import com.bsth.util.ConfigUtil; | |
| 11 | -import com.bsth.util.ConvertUtil; | |
| 12 | -import com.google.common.collect.ArrayListMultimap; | |
| 13 | -import org.apache.commons.lang3.StringUtils; | |
| 14 | -import org.joda.time.DateTime; | |
| 15 | -import org.joda.time.format.DateTimeFormat; | |
| 16 | -import org.joda.time.format.DateTimeFormatter; | |
| 17 | -import org.slf4j.Logger; | |
| 18 | -import org.slf4j.LoggerFactory; | |
| 19 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 20 | -import org.springframework.boot.CommandLineRunner; | |
| 21 | -import org.springframework.core.annotation.Order; | |
| 22 | -import org.springframework.data.redis.core.ListOperations; | |
| 23 | -import org.springframework.data.redis.core.RedisTemplate; | |
| 24 | -import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 25 | -import org.springframework.stereotype.Component; | |
| 26 | -import org.springframework.stereotype.Service; | |
| 27 | - | |
| 28 | -import java.util.ArrayList; | |
| 29 | -import java.util.Iterator; | |
| 30 | -import java.util.List; | |
| 31 | -import java.util.concurrent.TimeUnit; | |
| 32 | - | |
| 33 | -/** | |
| 34 | - * 班次 redis 缓存管理 | |
| 35 | - * Created by panzhao on 2017/3/13. | |
| 36 | - */ | |
| 37 | -@Service | |
| 38 | -@Order(2) | |
| 39 | -public class ScheduleRedisService implements CommandLineRunner { | |
| 40 | - | |
| 41 | - @Autowired | |
| 42 | - ScheduleRealInfoRepository scheduleRealInfoRepository; | |
| 43 | - | |
| 44 | - private final static String REDIS_KEY_PREFIX = "schedule:"; | |
| 45 | - | |
| 46 | - @Autowired | |
| 47 | - private RedisTemplate redisTemplate; | |
| 48 | - | |
| 49 | - @Autowired | |
| 50 | - RedisUtils redisUtils; | |
| 51 | - | |
| 52 | - static Logger logger = LoggerFactory.getLogger(ScheduleRedisService.class); | |
| 53 | - | |
| 54 | - private final static long DAY_TIME = 1000 * 60 * 60 * 24L; | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * 将一批班次写入redis | |
| 58 | - * | |
| 59 | - * @param list | |
| 60 | - */ | |
| 61 | - public void wirte(List<ScheduleRealInfo> list) { | |
| 62 | - ArrayListMultimap<String, ScheduleRealInfo> multimap; | |
| 63 | - try { | |
| 64 | - if (list.size() == 0) | |
| 65 | - return; | |
| 66 | - //按日期和线路分组数据 | |
| 67 | - Class clazz = ScheduleRealInfo.class; | |
| 68 | - multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDateStr")); | |
| 69 | - | |
| 70 | - //写入redis | |
| 71 | - Iterator<String> iterator = multimap.keySet().iterator(); | |
| 72 | - String key; | |
| 73 | - while (iterator.hasNext()) { | |
| 74 | - key = iterator.next(); | |
| 75 | - replace(key, multimap.get(key)); | |
| 76 | - } | |
| 77 | - } catch (Exception e) { | |
| 78 | - logger.error("", e); | |
| 79 | - } | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * 将 list 与redis里的数据合并 | |
| 84 | - * | |
| 85 | - * @param key | |
| 86 | - * @param list | |
| 87 | - */ | |
| 88 | - public void mergeData(String key, List<ScheduleRealInfo> list) { | |
| 89 | - key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 90 | - | |
| 91 | - ListOperations<String, ScheduleRealInfo> ops = redisTemplate.opsForList(); | |
| 92 | - List<ScheduleRealInfo> cacheList = ops.range(key, 0, -1); | |
| 93 | - | |
| 94 | - for (ScheduleRealInfo sch : cacheList) { | |
| 95 | - if (!list.contains(sch)) | |
| 96 | - list.add(sch); | |
| 97 | - } | |
| 98 | - | |
| 99 | - //更新 | |
| 100 | - redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * 覆盖数据 | |
| 105 | - * | |
| 106 | - * @param key | |
| 107 | - * @param list | |
| 108 | - */ | |
| 109 | - public void replace(String key, List<ScheduleRealInfo> list) { | |
| 110 | - key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 111 | - redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 112 | - } | |
| 113 | - | |
| 114 | - /** | |
| 115 | - * 删除数据 | |
| 116 | - * | |
| 117 | - * @param key | |
| 118 | - */ | |
| 119 | - public void delete(String lineCode, String rq) { | |
| 120 | - String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", ""); | |
| 121 | - redisTemplate.delete(key); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * 根据日期和线路编码从redis获取班次 | |
| 126 | - * | |
| 127 | - * @param dateStr | |
| 128 | - * @param lineCode | |
| 129 | - * @return | |
| 130 | - */ | |
| 131 | - public List<ScheduleRealInfo> read(String dateStr, String lineCode) { | |
| 132 | - return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1); | |
| 133 | - } | |
| 134 | - | |
| 135 | - /** | |
| 136 | - * 返回指定日期,公司的实际排班,并按线路_车辆分组 | |
| 137 | - * | |
| 138 | - * @param rq | |
| 139 | - * @param companyId | |
| 140 | - * @return | |
| 141 | - */ | |
| 142 | - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId) { | |
| 143 | - List<String> lineArray = LineBufferData.findCodesByCompany(companyId); | |
| 144 | - ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create(); | |
| 145 | - | |
| 146 | - rq = rq.replaceAll("-", ""); | |
| 147 | - List<ScheduleRealInfo> list; | |
| 148 | - for (String lineCode : lineArray) { | |
| 149 | - | |
| 150 | - list = read(rq, lineCode); | |
| 151 | - | |
| 152 | - for (ScheduleRealInfo sch : list) { | |
| 153 | - rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - return rs; | |
| 157 | - } | |
| 158 | - | |
| 159 | - /** | |
| 160 | - * 返回指定日期,公司的实际排班,并按线路分组 | |
| 161 | - * | |
| 162 | - * @param rq | |
| 163 | - * @param companyId | |
| 164 | - * @return | |
| 165 | - */ | |
| 166 | - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId) { | |
| 167 | - | |
| 168 | - List<String> lineArray = LineBufferData.findCodesByCompany(companyId); | |
| 169 | - ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create(); | |
| 170 | - | |
| 171 | - rq = rq.replaceAll("-", ""); | |
| 172 | - List<ScheduleRealInfo> list; | |
| 173 | - for (String lineCode : lineArray) { | |
| 174 | - | |
| 175 | - list = read(rq, lineCode); | |
| 176 | - | |
| 177 | - for (ScheduleRealInfo sch : list) { | |
| 178 | - rs.put(sch.getXlBm(), sch); | |
| 179 | - } | |
| 180 | - } | |
| 181 | - return rs; | |
| 182 | - } | |
| 183 | - | |
| 184 | - /** | |
| 185 | - * 返回指定日期,所有实际排班,并按自编号分组 | |
| 186 | - * | |
| 187 | - * @param rq | |
| 188 | - * @return | |
| 189 | - */ | |
| 190 | - public ArrayListMultimap<String, ScheduleRealInfo> findByDate(String rq) { | |
| 191 | - | |
| 192 | - List<Line> lines = LineBufferData.findAll(); | |
| 193 | - ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create(); | |
| 194 | - | |
| 195 | - rq = rq.replaceAll("-", ""); | |
| 196 | - List<ScheduleRealInfo> list; | |
| 197 | - for (Line line : lines) { | |
| 198 | - | |
| 199 | - list = read(rq, line.getLineCode()); | |
| 200 | - | |
| 201 | - for (ScheduleRealInfo sch : list) { | |
| 202 | - rs.put(sch.getClZbh(), sch); | |
| 203 | - } | |
| 204 | - } | |
| 205 | - return rs; | |
| 206 | - } | |
| 207 | - | |
| 208 | - @Autowired | |
| 209 | - ScheduleRefreshThread scheduleRefreshThread; | |
| 210 | - @Autowired | |
| 211 | - ScheduleClearThread scheduleClearThread; | |
| 212 | - | |
| 213 | - @Override | |
| 214 | - public void run(String... strings) throws Exception { | |
| 215 | - //用子线程去加载,,不要阻塞 | |
| 216 | - Application.mainServices.schedule(new Runnable() { | |
| 217 | - @Override | |
| 218 | - public void run() { | |
| 219 | - try { | |
| 220 | - logger.info("redis 实际排班 start..."); | |
| 221 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 222 | - //设置key 序列化器 | |
| 223 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 224 | - | |
| 225 | - DateTime dt = new DateTime(); | |
| 226 | - dt = dt.minusDays(cacheDays); | |
| 227 | - List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd")); | |
| 228 | - calcTime(list); | |
| 229 | - //写入redis | |
| 230 | - wirte(list); | |
| 231 | - logger.info("redis 实际排班 over..."); | |
| 232 | - } catch (Exception e) { | |
| 233 | - logger.info("redis 实际排班 异常", e); | |
| 234 | - } | |
| 235 | - } | |
| 236 | - }, 5, TimeUnit.SECONDS); | |
| 237 | - | |
| 238 | - //定时刷新一次当日实际排班 | |
| 239 | - int minute = 5; | |
| 240 | - Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute, minute, TimeUnit.MINUTES); | |
| 241 | - //24小时清理一次实际排班数据 | |
| 242 | - Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS); | |
| 243 | - } | |
| 244 | - | |
| 245 | - public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) { | |
| 246 | - rq = rq.replaceAll("-", ""); | |
| 247 | - List<ScheduleRealInfo> rs = new ArrayList<>(); | |
| 248 | - for (String lineCode : lineArray) { | |
| 249 | - rs.addAll(read(rq, lineCode)); | |
| 250 | - } | |
| 251 | - return rs; | |
| 252 | - } | |
| 253 | - | |
| 254 | - @Component | |
| 255 | - public static class ScheduleRefreshThread extends Thread { | |
| 256 | - | |
| 257 | - @Autowired | |
| 258 | - ScheduleRealInfoRepository realInfoRepository; | |
| 259 | - | |
| 260 | - @Autowired | |
| 261 | - ScheduleRedisService scheduleRedisService; | |
| 262 | - | |
| 263 | - @Override | |
| 264 | - public void run() { | |
| 265 | - try { | |
| 266 | - DateTime dt = new DateTime(); | |
| 267 | - DateTime yesterday = dt.minusDays(2); | |
| 268 | - | |
| 269 | - String rq = yesterday.toString("yyyy-MM-dd"); | |
| 270 | - logger.info("refresh lt yesterday ..." + rq + " -start"); | |
| 271 | - List<ScheduleRealInfo> list = realInfoRepository.findByDateLT(rq); | |
| 272 | - | |
| 273 | - //计算时间戳 | |
| 274 | - scheduleRedisService.calcTime(list); | |
| 275 | - scheduleRedisService.wirte(list); | |
| 276 | - | |
| 277 | - logger.info("refresh lt yesterday ..." + rq + " -end### size: " + list.size()); | |
| 278 | - } catch (Exception e) { | |
| 279 | - logger.error("", e); | |
| 280 | - } | |
| 281 | - } | |
| 282 | - } | |
| 283 | - | |
| 284 | - @Component | |
| 285 | - public static class ScheduleClearThread extends Thread { | |
| 286 | - | |
| 287 | - @Autowired | |
| 288 | - ScheduleRedisService scheduleRedisService; | |
| 289 | - | |
| 290 | - @Override | |
| 291 | - public void run() { | |
| 292 | - try { | |
| 293 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 294 | - DateTime dt = new DateTime(); | |
| 295 | - dt = dt.minusDays(cacheDays); | |
| 296 | - String rq = dt.toString("yyyy-MM-dd"); | |
| 297 | - | |
| 298 | - List<Line> lines = LineBufferData.findAll(); | |
| 299 | - for (Line line : lines) { | |
| 300 | - scheduleRedisService.delete(line.getLineCode(), rq); | |
| 301 | - } | |
| 302 | - } catch (Exception e) { | |
| 303 | - logger.error("", e); | |
| 304 | - } | |
| 305 | - } | |
| 306 | - } | |
| 307 | - | |
| 308 | - /** | |
| 309 | - * ############ 时间戳计算 ########## | |
| 310 | - */ | |
| 311 | - private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"), | |
| 312 | - fmtHHmm = DateTimeFormat.forPattern("HH:mm"); | |
| 313 | - | |
| 314 | - public void calcTime(List<ScheduleRealInfo> list) { | |
| 315 | - if (list.size() == 0) | |
| 316 | - return; | |
| 317 | - | |
| 318 | - //计算真实执行日期 和 时间戳 | |
| 319 | - for (ScheduleRealInfo sch : list) { | |
| 320 | - calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch); | |
| 321 | - } | |
| 322 | - } | |
| 323 | - | |
| 324 | - /** | |
| 325 | - * @Title: calcRealDate | |
| 326 | - * @Description: TODO(计算班次的真实执行日期) | |
| 327 | - */ | |
| 328 | - public void calcRealDate(String startTime, ScheduleRealInfo sch) { | |
| 329 | - try { | |
| 330 | - if (null == startTime) | |
| 331 | - return; | |
| 332 | - | |
| 333 | - if (null == sch.getBcsj()) | |
| 334 | - sch.setBcsj(0); | |
| 335 | - | |
| 336 | - String rq = sch.getScheduleDateStr(); | |
| 337 | - //计发时间 | |
| 338 | - sch.setFcsjT(parseTime(rq, sch.getFcsj(), startTime)); | |
| 339 | - //待发时间 | |
| 340 | - sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime)); | |
| 341 | - //计划终点时间 | |
| 342 | - if (StringUtils.isEmpty(sch.getZdsj())) { | |
| 343 | - sch.setZdsjT(sch.getFcsjT() + (sch.getBcsj() * 60 * 1000)); | |
| 344 | - sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); | |
| 345 | - } else | |
| 346 | - sch.setZdsjT(sch.getFcsjT() + (sch.getBcsj() * 60 * 1000)); | |
| 347 | - //实发时间 | |
| 348 | - if (StringUtils.isNotEmpty(sch.getFcsjActual())) | |
| 349 | - sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime)); | |
| 350 | - | |
| 351 | - //实达时间 | |
| 352 | - if (StringUtils.isNotEmpty(sch.getZdsjActual())) | |
| 353 | - sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime)); | |
| 354 | - } catch (Exception e) { | |
| 355 | - logger.error("", e); | |
| 356 | - } | |
| 357 | - } | |
| 358 | - | |
| 359 | - private long parseTime(String rq, String sj, String startTime) { | |
| 360 | - long t = fmtyyyyMMddHHmm.parseMillis(rq + sj); | |
| 361 | - if (sj.compareTo(startTime) < 0) { | |
| 362 | - t += DAY_TIME; | |
| 363 | - } | |
| 364 | - return t; | |
| 365 | - } | |
| 366 | -} | |
| 1 | +package com.bsth.redis; | |
| 2 | + | |
| 3 | +import com.bsth.Application; | |
| 4 | +import com.bsth.common.BasicData; | |
| 5 | +import com.bsth.entity.ScheduleRealInfo; | |
| 6 | +import com.bsth.redis.util.RedisUtils; | |
| 7 | +import com.bsth.repository.ScheduleRealInfoRepository; | |
| 8 | +import com.bsth.server_rs.base_info.line.Line; | |
| 9 | +import com.bsth.server_rs.base_info.line.buffer.LineBufferData; | |
| 10 | +import com.bsth.util.AppProperties; | |
| 11 | +import com.bsth.util.ConvertUtil; | |
| 12 | +import com.google.common.collect.ArrayListMultimap; | |
| 13 | +import org.apache.commons.lang3.StringUtils; | |
| 14 | +import org.joda.time.DateTime; | |
| 15 | +import org.joda.time.format.DateTimeFormat; | |
| 16 | +import org.joda.time.format.DateTimeFormatter; | |
| 17 | +import org.slf4j.Logger; | |
| 18 | +import org.slf4j.LoggerFactory; | |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 20 | +import org.springframework.boot.CommandLineRunner; | |
| 21 | +import org.springframework.core.annotation.Order; | |
| 22 | +import org.springframework.data.redis.core.ListOperations; | |
| 23 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 24 | +import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 25 | +import org.springframework.stereotype.Component; | |
| 26 | +import org.springframework.stereotype.Service; | |
| 27 | + | |
| 28 | +import java.util.ArrayList; | |
| 29 | +import java.util.Iterator; | |
| 30 | +import java.util.List; | |
| 31 | +import java.util.concurrent.TimeUnit; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * 班次 redis 缓存管理 | |
| 35 | + * Created by panzhao on 2017/3/13. | |
| 36 | + */ | |
| 37 | +@Service | |
| 38 | +@Order(2) | |
| 39 | +public class ScheduleRedisService implements CommandLineRunner { | |
| 40 | + | |
| 41 | + @Autowired | |
| 42 | + ScheduleRealInfoRepository scheduleRealInfoRepository; | |
| 43 | + | |
| 44 | + private final static String REDIS_KEY_PREFIX = "schedule:"; | |
| 45 | + | |
| 46 | + @Autowired | |
| 47 | + private RedisTemplate redisTemplate; | |
| 48 | + | |
| 49 | + @Autowired | |
| 50 | + RedisUtils redisUtils; | |
| 51 | + | |
| 52 | + static Logger logger = LoggerFactory.getLogger(ScheduleRedisService.class); | |
| 53 | + | |
| 54 | + private final static long DAY_TIME = 1000 * 60 * 60 * 24L; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * 将一批班次写入redis | |
| 58 | + * | |
| 59 | + * @param list | |
| 60 | + */ | |
| 61 | + public void wirte(List<ScheduleRealInfo> list) { | |
| 62 | + ArrayListMultimap<String, ScheduleRealInfo> multimap; | |
| 63 | + try { | |
| 64 | + if (list.size() == 0) | |
| 65 | + return; | |
| 66 | + //按日期和线路分组数据 | |
| 67 | + Class clazz = ScheduleRealInfo.class; | |
| 68 | + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDateStr")); | |
| 69 | + | |
| 70 | + //写入redis | |
| 71 | + Iterator<String> iterator = multimap.keySet().iterator(); | |
| 72 | + String key; | |
| 73 | + while (iterator.hasNext()) { | |
| 74 | + key = iterator.next(); | |
| 75 | + replace(key, multimap.get(key)); | |
| 76 | + } | |
| 77 | + } catch (Exception e) { | |
| 78 | + logger.error("", e); | |
| 79 | + } | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * 将 list 与redis里的数据合并 | |
| 84 | + * | |
| 85 | + * @param key | |
| 86 | + * @param list | |
| 87 | + */ | |
| 88 | + public void mergeData(String key, List<ScheduleRealInfo> list) { | |
| 89 | + key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 90 | + | |
| 91 | + ListOperations<String, ScheduleRealInfo> ops = redisTemplate.opsForList(); | |
| 92 | + List<ScheduleRealInfo> cacheList = ops.range(key, 0, -1); | |
| 93 | + | |
| 94 | + for (ScheduleRealInfo sch : cacheList) { | |
| 95 | + if (!list.contains(sch)) | |
| 96 | + list.add(sch); | |
| 97 | + } | |
| 98 | + | |
| 99 | + //更新 | |
| 100 | + redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * 覆盖数据 | |
| 105 | + * | |
| 106 | + * @param key | |
| 107 | + * @param list | |
| 108 | + */ | |
| 109 | + public void replace(String key, List<ScheduleRealInfo> list) { | |
| 110 | + key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 111 | + redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * 删除数据 | |
| 116 | + * | |
| 117 | + * @param key | |
| 118 | + */ | |
| 119 | + public void delete(String lineCode, String rq) { | |
| 120 | + String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", ""); | |
| 121 | + redisTemplate.delete(key); | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * 根据日期和线路编码从redis获取班次 | |
| 126 | + * | |
| 127 | + * @param dateStr | |
| 128 | + * @param lineCode | |
| 129 | + * @return | |
| 130 | + */ | |
| 131 | + public List<ScheduleRealInfo> read(String dateStr, String lineCode) { | |
| 132 | + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1); | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * 返回指定日期,公司的实际排班,并按线路_车辆分组 | |
| 137 | + * | |
| 138 | + * @param rq | |
| 139 | + * @param companyId | |
| 140 | + * @return | |
| 141 | + */ | |
| 142 | + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId) { | |
| 143 | + List<String> lineArray = LineBufferData.findCodesByCompany(companyId); | |
| 144 | + ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create(); | |
| 145 | + | |
| 146 | + rq = rq.replaceAll("-", ""); | |
| 147 | + List<ScheduleRealInfo> list; | |
| 148 | + for (String lineCode : lineArray) { | |
| 149 | + | |
| 150 | + list = read(rq, lineCode); | |
| 151 | + | |
| 152 | + for (ScheduleRealInfo sch : list) { | |
| 153 | + rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + return rs; | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * 返回指定日期,公司的实际排班,并按线路分组 | |
| 161 | + * | |
| 162 | + * @param rq | |
| 163 | + * @param companyId | |
| 164 | + * @return | |
| 165 | + */ | |
| 166 | + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId) { | |
| 167 | + | |
| 168 | + List<String> lineArray = LineBufferData.findCodesByCompany(companyId); | |
| 169 | + ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create(); | |
| 170 | + | |
| 171 | + rq = rq.replaceAll("-", ""); | |
| 172 | + List<ScheduleRealInfo> list; | |
| 173 | + for (String lineCode : lineArray) { | |
| 174 | + | |
| 175 | + list = read(rq, lineCode); | |
| 176 | + | |
| 177 | + for (ScheduleRealInfo sch : list) { | |
| 178 | + rs.put(sch.getXlBm(), sch); | |
| 179 | + } | |
| 180 | + } | |
| 181 | + return rs; | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * 返回指定日期,所有实际排班,并按自编号分组 | |
| 186 | + * | |
| 187 | + * @param rq | |
| 188 | + * @return | |
| 189 | + */ | |
| 190 | + public ArrayListMultimap<String, ScheduleRealInfo> findByDate(String rq) { | |
| 191 | + | |
| 192 | + List<Line> lines = LineBufferData.findAll(); | |
| 193 | + ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create(); | |
| 194 | + | |
| 195 | + rq = rq.replaceAll("-", ""); | |
| 196 | + List<ScheduleRealInfo> list; | |
| 197 | + for (Line line : lines) { | |
| 198 | + | |
| 199 | + list = read(rq, line.getLineCode()); | |
| 200 | + | |
| 201 | + for (ScheduleRealInfo sch : list) { | |
| 202 | + rs.put(sch.getClZbh(), sch); | |
| 203 | + } | |
| 204 | + } | |
| 205 | + return rs; | |
| 206 | + } | |
| 207 | + | |
| 208 | + @Autowired | |
| 209 | + ScheduleRefreshThread scheduleRefreshThread; | |
| 210 | + @Autowired | |
| 211 | + ScheduleClearThread scheduleClearThread; | |
| 212 | + | |
| 213 | + @Override | |
| 214 | + public void run(String... strings) throws Exception { | |
| 215 | + //用子线程去加载,,不要阻塞 | |
| 216 | + Application.mainServices.schedule(new Runnable() { | |
| 217 | + @Override | |
| 218 | + public void run() { | |
| 219 | + try { | |
| 220 | + logger.info("redis 实际排班 start..."); | |
| 221 | + int cacheDays = AppProperties.getCacheDays(); | |
| 222 | + //设置key 序列化器 | |
| 223 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 224 | + | |
| 225 | + DateTime dt = new DateTime(); | |
| 226 | + dt = dt.minusDays(cacheDays); | |
| 227 | + List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd")); | |
| 228 | + calcTime(list); | |
| 229 | + //写入redis | |
| 230 | + wirte(list); | |
| 231 | + logger.info("redis 实际排班 over..."); | |
| 232 | + } catch (Exception e) { | |
| 233 | + logger.info("redis 实际排班 异常", e); | |
| 234 | + } | |
| 235 | + } | |
| 236 | + }, 5, TimeUnit.SECONDS); | |
| 237 | + | |
| 238 | + //定时刷新一次当日实际排班 | |
| 239 | + int minute = 5; | |
| 240 | + Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute, minute, TimeUnit.MINUTES); | |
| 241 | + //24小时清理一次实际排班数据 | |
| 242 | + Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS); | |
| 243 | + } | |
| 244 | + | |
| 245 | + public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) { | |
| 246 | + rq = rq.replaceAll("-", ""); | |
| 247 | + List<ScheduleRealInfo> rs = new ArrayList<>(); | |
| 248 | + for (String lineCode : lineArray) { | |
| 249 | + rs.addAll(read(rq, lineCode)); | |
| 250 | + } | |
| 251 | + return rs; | |
| 252 | + } | |
| 253 | + | |
| 254 | + @Component | |
| 255 | + public static class ScheduleRefreshThread extends Thread { | |
| 256 | + | |
| 257 | + @Autowired | |
| 258 | + ScheduleRealInfoRepository realInfoRepository; | |
| 259 | + | |
| 260 | + @Autowired | |
| 261 | + ScheduleRedisService scheduleRedisService; | |
| 262 | + | |
| 263 | + @Override | |
| 264 | + public void run() { | |
| 265 | + try { | |
| 266 | + DateTime dt = new DateTime(); | |
| 267 | + DateTime yesterday = dt.minusDays(2); | |
| 268 | + | |
| 269 | + String rq = yesterday.toString("yyyy-MM-dd"); | |
| 270 | + logger.info("refresh lt yesterday ..." + rq + " -start"); | |
| 271 | + List<ScheduleRealInfo> list = realInfoRepository.findByDateLT(rq); | |
| 272 | + | |
| 273 | + //计算时间戳 | |
| 274 | + scheduleRedisService.calcTime(list); | |
| 275 | + scheduleRedisService.wirte(list); | |
| 276 | + | |
| 277 | + logger.info("refresh lt yesterday ..." + rq + " -end### size: " + list.size()); | |
| 278 | + } catch (Exception e) { | |
| 279 | + logger.error("", e); | |
| 280 | + } | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + @Component | |
| 285 | + public static class ScheduleClearThread extends Thread { | |
| 286 | + | |
| 287 | + @Autowired | |
| 288 | + ScheduleRedisService scheduleRedisService; | |
| 289 | + | |
| 290 | + @Override | |
| 291 | + public void run() { | |
| 292 | + try { | |
| 293 | + int cacheDays = AppProperties.getCacheDays(); | |
| 294 | + DateTime dt = new DateTime(); | |
| 295 | + dt = dt.minusDays(cacheDays); | |
| 296 | + String rq = dt.toString("yyyy-MM-dd"); | |
| 297 | + | |
| 298 | + List<Line> lines = LineBufferData.findAll(); | |
| 299 | + for (Line line : lines) { | |
| 300 | + scheduleRedisService.delete(line.getLineCode(), rq); | |
| 301 | + } | |
| 302 | + } catch (Exception e) { | |
| 303 | + logger.error("", e); | |
| 304 | + } | |
| 305 | + } | |
| 306 | + } | |
| 307 | + | |
| 308 | + /** | |
| 309 | + * ############ 时间戳计算 ########## | |
| 310 | + */ | |
| 311 | + private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"), | |
| 312 | + fmtHHmm = DateTimeFormat.forPattern("HH:mm"); | |
| 313 | + | |
| 314 | + public void calcTime(List<ScheduleRealInfo> list) { | |
| 315 | + if (list.size() == 0) | |
| 316 | + return; | |
| 317 | + | |
| 318 | + //计算真实执行日期 和 时间戳 | |
| 319 | + for (ScheduleRealInfo sch : list) { | |
| 320 | + calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch); | |
| 321 | + } | |
| 322 | + } | |
| 323 | + | |
| 324 | + /** | |
| 325 | + * @Title: calcRealDate | |
| 326 | + * @Description: TODO(计算班次的真实执行日期) | |
| 327 | + */ | |
| 328 | + public void calcRealDate(String startTime, ScheduleRealInfo sch) { | |
| 329 | + try { | |
| 330 | + if (null == startTime) | |
| 331 | + return; | |
| 332 | + | |
| 333 | + if (null == sch.getBcsj()) | |
| 334 | + sch.setBcsj(0); | |
| 335 | + | |
| 336 | + String rq = sch.getScheduleDateStr(); | |
| 337 | + //计发时间 | |
| 338 | + sch.setFcsjT(parseTime(rq, sch.getFcsj(), startTime)); | |
| 339 | + //待发时间 | |
| 340 | + sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime)); | |
| 341 | + //计划终点时间 | |
| 342 | + if (StringUtils.isEmpty(sch.getZdsj())) { | |
| 343 | + sch.setZdsjT(sch.getFcsjT() + (sch.getBcsj() * 60 * 1000)); | |
| 344 | + sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); | |
| 345 | + } else | |
| 346 | + sch.setZdsjT(sch.getFcsjT() + (sch.getBcsj() * 60 * 1000)); | |
| 347 | + //实发时间 | |
| 348 | + if (StringUtils.isNotEmpty(sch.getFcsjActual())) | |
| 349 | + sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime)); | |
| 350 | + | |
| 351 | + //实达时间 | |
| 352 | + if (StringUtils.isNotEmpty(sch.getZdsjActual())) | |
| 353 | + sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime)); | |
| 354 | + } catch (Exception e) { | |
| 355 | + logger.error("", e); | |
| 356 | + } | |
| 357 | + } | |
| 358 | + | |
| 359 | + private long parseTime(String rq, String sj, String startTime) { | |
| 360 | + long t = fmtyyyyMMddHHmm.parseMillis(rq + sj); | |
| 361 | + if (sj.compareTo(startTime) < 0) { | |
| 362 | + t += DAY_TIME; | |
| 363 | + } | |
| 364 | + return t; | |
| 365 | + } | |
| 366 | +} | ... | ... |
src/main/java/com/bsth/server_rs/directive/DirectiveRestService.java
| 1 | -package com.bsth.server_rs.directive; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSON; | |
| 4 | -import com.bsth.server_rs.schedule.real.SchRealDataBuffer; | |
| 5 | -import com.bsth.util.ConfigUtil; | |
| 6 | -import com.bsth.util.HttpClientUtils; | |
| 7 | -import org.slf4j.Logger; | |
| 8 | -import org.slf4j.LoggerFactory; | |
| 9 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | -import org.springframework.stereotype.Component; | |
| 11 | -import org.springframework.web.bind.annotation.RequestBody; | |
| 12 | - | |
| 13 | -import javax.ws.rs.*; | |
| 14 | -import javax.ws.rs.core.MediaType; | |
| 15 | -import java.util.List; | |
| 16 | -import java.util.Map; | |
| 17 | - | |
| 18 | -/** | |
| 19 | - * Created by panzhao on 2017/11/10. | |
| 20 | - */ | |
| 21 | -@Component | |
| 22 | -@Path("/directive") | |
| 23 | -@Produces({MediaType.APPLICATION_JSON}) | |
| 24 | -public class DirectiveRestService { | |
| 25 | - | |
| 26 | - static String url; | |
| 27 | - static String secretKey; | |
| 28 | - | |
| 29 | - @Autowired | |
| 30 | - SchRealDataBuffer schRealDataBuffer; | |
| 31 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 32 | - | |
| 33 | - static { | |
| 34 | - secretKey = ConfigUtil.get("http.control.secret.key"); | |
| 35 | - url = ConfigUtil.get("http.control.service_data_url") + "/send60Phrase?secretKey=" + secretKey; | |
| 36 | - } | |
| 37 | - | |
| 38 | - @POST | |
| 39 | - @Path("/send") | |
| 40 | - public int send(@RequestBody Map<String, Object> map){ | |
| 41 | - try{ | |
| 42 | - map.put("sender", "接口@系统"); | |
| 43 | - StringBuilder sb = HttpClientUtils.post(url , JSON.toJSONString(map)); | |
| 44 | - return Integer.parseInt(sb.toString()); | |
| 45 | - }catch (Exception e){ | |
| 46 | - logger.error("", e); | |
| 47 | - return -500; | |
| 48 | - } | |
| 49 | - } | |
| 50 | - | |
| 51 | - @GET | |
| 52 | - @Path("/reply/{msgIds}") | |
| 53 | - public List<Map> reply(@PathParam("msgIds") String msgIds){ | |
| 54 | - try{ | |
| 55 | - StringBuilder sb = HttpClientUtils.get(url + "&msgIds=" + msgIds); | |
| 56 | - return JSON.parseArray(sb.toString(), Map.class); | |
| 57 | - }catch (Exception e){ | |
| 58 | - logger.error("", e); | |
| 59 | - return null; | |
| 60 | - } | |
| 61 | - } | |
| 62 | -} | |
| 1 | +package com.bsth.server_rs.directive; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.bsth.server_rs.schedule.real.SchRealDataBuffer; | |
| 5 | +import com.bsth.util.AppProperties; | |
| 6 | +import com.bsth.util.HttpClientUtils; | |
| 7 | +import org.slf4j.Logger; | |
| 8 | +import org.slf4j.LoggerFactory; | |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | +import org.springframework.stereotype.Component; | |
| 11 | +import org.springframework.web.bind.annotation.RequestBody; | |
| 12 | + | |
| 13 | +import javax.ws.rs.*; | |
| 14 | +import javax.ws.rs.core.MediaType; | |
| 15 | +import java.util.List; | |
| 16 | +import java.util.Map; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Created by panzhao on 2017/11/10. | |
| 20 | + */ | |
| 21 | +@Component | |
| 22 | +@Path("/directive") | |
| 23 | +@Produces({MediaType.APPLICATION_JSON}) | |
| 24 | +public class DirectiveRestService { | |
| 25 | + | |
| 26 | + static String url = AppProperties.getUrl(); | |
| 27 | + static String secretKey = AppProperties.getSecretKey(); | |
| 28 | + | |
| 29 | + @Autowired | |
| 30 | + SchRealDataBuffer schRealDataBuffer; | |
| 31 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 32 | + | |
| 33 | + @POST | |
| 34 | + @Path("/send") | |
| 35 | + public int send(@RequestBody Map<String, Object> map){ | |
| 36 | + try{ | |
| 37 | + map.put("sender", "接口@系统"); | |
| 38 | + StringBuilder sb = HttpClientUtils.post(url , JSON.toJSONString(map)); | |
| 39 | + return Integer.parseInt(sb.toString()); | |
| 40 | + }catch (Exception e){ | |
| 41 | + logger.error("", e); | |
| 42 | + return -500; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + @GET | |
| 47 | + @Path("/reply/{msgIds}") | |
| 48 | + public List<Map> reply(@PathParam("msgIds") String msgIds){ | |
| 49 | + try{ | |
| 50 | + StringBuilder sb = HttpClientUtils.get(url + "&msgIds=" + msgIds); | |
| 51 | + return JSON.parseArray(sb.toString(), Map.class); | |
| 52 | + }catch (Exception e){ | |
| 53 | + logger.error("", e); | |
| 54 | + return null; | |
| 55 | + } | |
| 56 | + } | |
| 57 | +} | ... | ... |
src/main/java/com/bsth/server_rs/gps/buffer/GpsRefreshThread.java
| 1 | -package com.bsth.server_rs.gps.buffer; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSON; | |
| 4 | -import com.alibaba.fastjson.JSONObject; | |
| 5 | -import com.bsth.server_rs.gps.entity.GpsEntity; | |
| 6 | -import com.bsth.util.ConfigUtil; | |
| 7 | -import org.apache.http.HttpEntity; | |
| 8 | -import org.apache.http.client.config.RequestConfig; | |
| 9 | -import org.apache.http.client.methods.CloseableHttpResponse; | |
| 10 | -import org.apache.http.client.methods.HttpGet; | |
| 11 | -import org.apache.http.impl.client.CloseableHttpClient; | |
| 12 | -import org.apache.http.impl.client.HttpClients; | |
| 13 | -import org.slf4j.Logger; | |
| 14 | -import org.slf4j.LoggerFactory; | |
| 15 | -import org.springframework.stereotype.Component; | |
| 16 | - | |
| 17 | -import java.io.BufferedReader; | |
| 18 | -import java.io.InputStreamReader; | |
| 19 | -import java.util.List; | |
| 20 | - | |
| 21 | -/** | |
| 22 | - * Created by panzhao on 2017/3/30. | |
| 23 | - */ | |
| 24 | -@Component | |
| 25 | -public class GpsRefreshThread extends Thread{ | |
| 26 | - | |
| 27 | - static String url; | |
| 28 | - | |
| 29 | - static { | |
| 30 | - url = ConfigUtil.get("http.gps.real.url"); | |
| 31 | - } | |
| 32 | - | |
| 33 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 34 | - | |
| 35 | - @Override | |
| 36 | - public void run() { | |
| 37 | - try { | |
| 38 | - CloseableHttpClient httpClient = null; | |
| 39 | - List<GpsEntity> rs = null; | |
| 40 | - httpClient = HttpClients.createDefault(); | |
| 41 | - //超时时间 | |
| 42 | - RequestConfig requestConfig = RequestConfig.custom() | |
| 43 | - .setConnectTimeout(2000).setConnectionRequestTimeout(1000) | |
| 44 | - .setSocketTimeout(3000).build(); | |
| 45 | - | |
| 46 | - HttpGet get = new HttpGet(url); | |
| 47 | - get.setConfig(requestConfig); | |
| 48 | - | |
| 49 | - CloseableHttpResponse response = httpClient.execute(get); | |
| 50 | - | |
| 51 | - HttpEntity entity = response.getEntity(); | |
| 52 | - BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); | |
| 53 | - StringBuffer stringBuffer = new StringBuffer(); | |
| 54 | - String str = ""; | |
| 55 | - while ((str = br.readLine()) != null) | |
| 56 | - stringBuffer.append(str); | |
| 57 | - | |
| 58 | - JSONObject jsonObj = JSON.parseObject(stringBuffer.toString()); | |
| 59 | - | |
| 60 | - if (jsonObj != null) | |
| 61 | - rs = JSON.parseArray(jsonObj.getString("data"), GpsEntity.class); | |
| 62 | - | |
| 63 | - for(GpsEntity gps : rs){ | |
| 64 | - GpsRealDataBuffer.put(gps); | |
| 65 | - } | |
| 66 | - }catch (Exception e){ | |
| 67 | - logger.error("", e); | |
| 68 | - } | |
| 69 | - } | |
| 70 | -} | |
| 1 | +package com.bsth.server_rs.gps.buffer; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 5 | +import com.bsth.server_rs.gps.entity.GpsEntity; | |
| 6 | +import com.bsth.util.AppProperties; | |
| 7 | +import org.apache.http.HttpEntity; | |
| 8 | +import org.apache.http.client.config.RequestConfig; | |
| 9 | +import org.apache.http.client.methods.CloseableHttpResponse; | |
| 10 | +import org.apache.http.client.methods.HttpGet; | |
| 11 | +import org.apache.http.impl.client.CloseableHttpClient; | |
| 12 | +import org.apache.http.impl.client.HttpClients; | |
| 13 | +import org.slf4j.Logger; | |
| 14 | +import org.slf4j.LoggerFactory; | |
| 15 | +import org.springframework.stereotype.Component; | |
| 16 | + | |
| 17 | +import java.io.BufferedReader; | |
| 18 | +import java.io.InputStreamReader; | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Created by panzhao on 2017/3/30. | |
| 23 | + */ | |
| 24 | +@Component | |
| 25 | +public class GpsRefreshThread extends Thread{ | |
| 26 | + | |
| 27 | + static String url = AppProperties.getUrl(); | |
| 28 | + | |
| 29 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + public void run() { | |
| 33 | + try { | |
| 34 | + CloseableHttpClient httpClient = null; | |
| 35 | + List<GpsEntity> rs = null; | |
| 36 | + httpClient = HttpClients.createDefault(); | |
| 37 | + //超时时间 | |
| 38 | + RequestConfig requestConfig = RequestConfig.custom() | |
| 39 | + .setConnectTimeout(2000).setConnectionRequestTimeout(1000) | |
| 40 | + .setSocketTimeout(3000).build(); | |
| 41 | + | |
| 42 | + HttpGet get = new HttpGet(url); | |
| 43 | + get.setConfig(requestConfig); | |
| 44 | + | |
| 45 | + CloseableHttpResponse response = httpClient.execute(get); | |
| 46 | + | |
| 47 | + HttpEntity entity = response.getEntity(); | |
| 48 | + BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); | |
| 49 | + StringBuffer stringBuffer = new StringBuffer(); | |
| 50 | + String str = ""; | |
| 51 | + while ((str = br.readLine()) != null) | |
| 52 | + stringBuffer.append(str); | |
| 53 | + | |
| 54 | + JSONObject jsonObj = JSON.parseObject(stringBuffer.toString()); | |
| 55 | + | |
| 56 | + if (jsonObj != null) | |
| 57 | + rs = JSON.parseArray(jsonObj.getString("data"), GpsEntity.class); | |
| 58 | + | |
| 59 | + for(GpsEntity gps : rs){ | |
| 60 | + GpsRealDataBuffer.put(gps); | |
| 61 | + } | |
| 62 | + }catch (Exception e){ | |
| 63 | + logger.error("", e); | |
| 64 | + } | |
| 65 | + } | |
| 66 | +} | ... | ... |
src/main/java/com/bsth/server_rs/schedule/real/ScheduleRealService.java
| ... | ... | @@ -3,14 +3,11 @@ package com.bsth.server_rs.schedule.real; |
| 3 | 3 | import com.alibaba.fastjson.JSON; |
| 4 | 4 | import com.alibaba.fastjson.JSONArray; |
| 5 | 5 | import com.alibaba.fastjson.JSONObject; |
| 6 | -import com.bsth.Application; | |
| 7 | 6 | import com.bsth.common.BasicData; |
| 8 | 7 | import com.bsth.common.ResponseCode; |
| 9 | 8 | import com.bsth.entity.ScheduleRealInfo; |
| 10 | -import com.bsth.entity.ScheduleRealInfoVo; | |
| 11 | 9 | import com.bsth.redis.ScheduleRedisService; |
| 12 | 10 | import com.bsth.repository.ScheduleRealInfoRepository; |
| 13 | -import com.bsth.server_rs.base_info.car.Car; | |
| 14 | 11 | import com.bsth.server_rs.base_info.car.buffer.CarBufferData; |
| 15 | 12 | import com.bsth.server_rs.base_info.line.Line; |
| 16 | 13 | import com.bsth.server_rs.base_info.line.buffer.LineBufferData; |
| ... | ... | @@ -19,33 +16,25 @@ import com.bsth.server_rs.gps.entity.GpsEntity; |
| 19 | 16 | import com.bsth.server_rs.schedule.dto.ScheduleCcInfoConfig; |
| 20 | 17 | import com.bsth.server_rs.schedule.dto.ScheduleInOut; |
| 21 | 18 | import com.bsth.server_rs.schedule.dto.ScheduleRealInfoDTO_JK; |
| 22 | -import com.bsth.util.ConfigUtil; | |
| 19 | +import com.bsth.util.AppProperties; | |
| 23 | 20 | import com.bsth.util.HttpClientUtils; |
| 24 | -import com.fasterxml.jackson.core.JsonProcessingException; | |
| 25 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
| 26 | 21 | import com.google.common.base.Splitter; |
| 27 | 22 | import org.apache.commons.lang3.StringUtils; |
| 28 | -import org.apache.kafka.clients.producer.ProducerRecord; | |
| 29 | 23 | import org.joda.time.DateTime; |
| 30 | 24 | import org.joda.time.format.DateTimeFormat; |
| 31 | 25 | import org.joda.time.format.DateTimeFormatter; |
| 32 | 26 | import org.slf4j.Logger; |
| 33 | 27 | import org.slf4j.LoggerFactory; |
| 34 | -import org.springframework.beans.BeanUtils; | |
| 35 | 28 | import org.springframework.beans.factory.InitializingBean; |
| 36 | 29 | import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.beans.factory.annotation.Value; | |
| 37 | 31 | import org.springframework.kafka.core.KafkaTemplate; |
| 38 | -import org.springframework.kafka.support.SendResult; | |
| 39 | -import org.springframework.messaging.Message; | |
| 40 | 32 | import org.springframework.stereotype.Component; |
| 41 | -import org.springframework.util.concurrent.ListenableFuture; | |
| 42 | -import org.springframework.util.concurrent.ListenableFutureCallback; | |
| 43 | 33 | |
| 44 | 34 | import javax.ws.rs.*; |
| 45 | 35 | import javax.ws.rs.core.MediaType; |
| 46 | 36 | import java.net.URLEncoder; |
| 47 | 37 | import java.util.*; |
| 48 | -import java.util.concurrent.*; | |
| 49 | 38 | |
| 50 | 39 | /** |
| 51 | 40 | * Created by panzhao on 2017/8/24. |
| ... | ... | @@ -69,18 +58,14 @@ public class ScheduleRealService implements InitializingBean { |
| 69 | 58 | |
| 70 | 59 | Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 71 | 60 | |
| 72 | - static String url; | |
| 73 | - static String secretKey; | |
| 61 | + static String url = AppProperties.getUrl(); | |
| 62 | + | |
| 63 | + static String secretKey = AppProperties.getSecretKey(); | |
| 74 | 64 | |
| 75 | 65 | private volatile long timestamp; |
| 76 | 66 | |
| 77 | 67 | private final static int KAFKA_BATCH_SIZE = 100; |
| 78 | 68 | |
| 79 | - static { | |
| 80 | - secretKey = ConfigUtil.get("http.control.secret.key"); | |
| 81 | - url = ConfigUtil.get("http.control.service_data_url");// + "/execSchList?secretKey=" + secretKey; | |
| 82 | - } | |
| 83 | - | |
| 84 | 69 | @POST |
| 85 | 70 | @Path("tcc_tzrc") |
| 86 | 71 | public Map<String, Object> tccTzrc(String jsonStr) { | ... | ... |
src/main/java/com/bsth/server_rs/schedule/real/thread/ExecSchDataRefreshThread.java
| 1 | -package com.bsth.server_rs.schedule.real.thread; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSON; | |
| 4 | -import com.alibaba.fastjson.JSONObject; | |
| 5 | -import com.bsth.server_rs.schedule.real.SchRealDataBuffer; | |
| 6 | -import com.bsth.util.ConfigUtil; | |
| 7 | -import com.bsth.util.HttpClientUtils; | |
| 8 | -import org.slf4j.Logger; | |
| 9 | -import org.slf4j.LoggerFactory; | |
| 10 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | -import org.springframework.stereotype.Component; | |
| 12 | - | |
| 13 | -import java.util.HashMap; | |
| 14 | -import java.util.List; | |
| 15 | -import java.util.Map; | |
| 16 | - | |
| 17 | -/** | |
| 18 | - * 车辆当前执行班次数据刷新线程 | |
| 19 | - * Created by panzhao on 2017/11/9. | |
| 20 | - */ | |
| 21 | -@Component | |
| 22 | -public class ExecSchDataRefreshThread extends Thread{ | |
| 23 | - | |
| 24 | - static String url; | |
| 25 | - static String secretKey; | |
| 26 | - | |
| 27 | - @Autowired | |
| 28 | - SchRealDataBuffer schRealDataBuffer; | |
| 29 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 30 | - | |
| 31 | - static { | |
| 32 | - secretKey = ConfigUtil.get("http.control.secret.key"); | |
| 33 | - url = ConfigUtil.get("http.control.service_data_url") + "/execSchList?secretKey=" + secretKey; | |
| 34 | - } | |
| 35 | - | |
| 36 | - @Override | |
| 37 | - public void run() { | |
| 38 | - try { | |
| 39 | - StringBuilder sb = HttpClientUtils.get(url); | |
| 40 | - List<JSONObject> list = JSON.parseArray(sb.toString(), JSONObject.class); | |
| 41 | - | |
| 42 | - Map<String, JSONObject> map = new HashMap<>(); | |
| 43 | - for(JSONObject obj : list){ | |
| 44 | - map.put(obj.getString("clZbh"), obj); | |
| 45 | - } | |
| 46 | - | |
| 47 | - if(map.size() > 0) | |
| 48 | - schRealDataBuffer.setExecMap(map); | |
| 49 | - } catch (Exception e) { | |
| 50 | - logger.error("", e); | |
| 51 | - } | |
| 52 | - } | |
| 53 | -} | |
| 1 | +package com.bsth.server_rs.schedule.real.thread; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 5 | +import com.bsth.server_rs.schedule.real.SchRealDataBuffer; | |
| 6 | +import com.bsth.util.AppProperties; | |
| 7 | +import com.bsth.util.HttpClientUtils; | |
| 8 | +import org.slf4j.Logger; | |
| 9 | +import org.slf4j.LoggerFactory; | |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | +import org.springframework.stereotype.Component; | |
| 12 | + | |
| 13 | +import java.util.HashMap; | |
| 14 | +import java.util.List; | |
| 15 | +import java.util.Map; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * 车辆当前执行班次数据刷新线程 | |
| 19 | + * Created by panzhao on 2017/11/9. | |
| 20 | + */ | |
| 21 | +@Component | |
| 22 | +public class ExecSchDataRefreshThread extends Thread{ | |
| 23 | + | |
| 24 | + static String url = AppProperties.getUrl(); | |
| 25 | + static String secretKey = AppProperties.getSecretKey(); | |
| 26 | + | |
| 27 | + @Autowired | |
| 28 | + SchRealDataBuffer schRealDataBuffer; | |
| 29 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + public void run() { | |
| 33 | + try { | |
| 34 | + StringBuilder sb = HttpClientUtils.get(url); | |
| 35 | + List<JSONObject> list = JSON.parseArray(sb.toString(), JSONObject.class); | |
| 36 | + | |
| 37 | + Map<String, JSONObject> map = new HashMap<>(); | |
| 38 | + for(JSONObject obj : list){ | |
| 39 | + map.put(obj.getString("clZbh"), obj); | |
| 40 | + } | |
| 41 | + | |
| 42 | + if(map.size() > 0) | |
| 43 | + schRealDataBuffer.setExecMap(map); | |
| 44 | + } catch (Exception e) { | |
| 45 | + logger.error("", e); | |
| 46 | + } | |
| 47 | + } | |
| 48 | +} | ... | ... |
src/main/java/com/bsth/server_rs/schedule/real/thread/SchInOutDataRefreshThread.java
| 1 | -package com.bsth.server_rs.schedule.real.thread; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSON; | |
| 4 | -import com.bsth.entity.ScheduleRealInfo; | |
| 5 | -import com.bsth.server_rs.schedule.real.SchRealDataBuffer; | |
| 6 | -import com.bsth.util.ConfigUtil; | |
| 7 | -import com.bsth.util.HttpClientUtils; | |
| 8 | -import org.slf4j.Logger; | |
| 9 | -import org.slf4j.LoggerFactory; | |
| 10 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | -import org.springframework.stereotype.Component; | |
| 12 | - | |
| 13 | -import java.util.List; | |
| 14 | - | |
| 15 | -/** | |
| 16 | - * 从调度系统获取进出场数据线程 | |
| 17 | - * Created by panzhao on 2017/9/27. | |
| 18 | - */ | |
| 19 | -@Component | |
| 20 | -public class SchInOutDataRefreshThread extends Thread { | |
| 21 | - | |
| 22 | - static String url; | |
| 23 | - static String secretKey; | |
| 24 | - | |
| 25 | - @Autowired | |
| 26 | - SchRealDataBuffer schRealDataBuffer; | |
| 27 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 28 | - | |
| 29 | - static { | |
| 30 | - secretKey = ConfigUtil.get("http.control.secret.key"); | |
| 31 | - url = ConfigUtil.get("http.control.service_data_url") + "/findCurrInAndOut?secretKey=" + secretKey; | |
| 32 | - } | |
| 33 | - | |
| 34 | - @Override | |
| 35 | - public void run() { | |
| 36 | - try { | |
| 37 | - StringBuilder sb = HttpClientUtils.get(url); | |
| 38 | - List<ScheduleRealInfo> list = JSON.parseArray(sb.toString(), ScheduleRealInfo.class); | |
| 39 | - | |
| 40 | - if(null != list && list.size() > 0){ | |
| 41 | - schRealDataBuffer.putInOutData(list); | |
| 42 | - logger.info("从调度系统刷新进出场班次: " + list.size()); | |
| 43 | - } | |
| 44 | - } catch (Exception e) { | |
| 45 | - logger.error("", e); | |
| 46 | - } | |
| 47 | - } | |
| 48 | -} | |
| 1 | +package com.bsth.server_rs.schedule.real.thread; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.bsth.entity.ScheduleRealInfo; | |
| 5 | +import com.bsth.server_rs.schedule.real.SchRealDataBuffer; | |
| 6 | +import com.bsth.util.AppProperties; | |
| 7 | +import com.bsth.util.HttpClientUtils; | |
| 8 | +import org.slf4j.Logger; | |
| 9 | +import org.slf4j.LoggerFactory; | |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | +import org.springframework.stereotype.Component; | |
| 12 | + | |
| 13 | +import java.util.List; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * 从调度系统获取进出场数据线程 | |
| 17 | + * Created by panzhao on 2017/9/27. | |
| 18 | + */ | |
| 19 | +@Component | |
| 20 | +public class SchInOutDataRefreshThread extends Thread { | |
| 21 | + | |
| 22 | + static String url = AppProperties.getUrl(); | |
| 23 | + static String secretKey = AppProperties.getSecretKey(); | |
| 24 | + | |
| 25 | + @Autowired | |
| 26 | + SchRealDataBuffer schRealDataBuffer; | |
| 27 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + public void run() { | |
| 31 | + try { | |
| 32 | + StringBuilder sb = HttpClientUtils.get(url); | |
| 33 | + List<ScheduleRealInfo> list = JSON.parseArray(sb.toString(), ScheduleRealInfo.class); | |
| 34 | + | |
| 35 | + if(null != list && list.size() > 0){ | |
| 36 | + schRealDataBuffer.putInOutData(list); | |
| 37 | + logger.info("从调度系统刷新进出场班次: " + list.size()); | |
| 38 | + } | |
| 39 | + } catch (Exception e) { | |
| 40 | + logger.error("", e); | |
| 41 | + } | |
| 42 | + } | |
| 43 | +} | ... | ... |
src/main/java/com/bsth/server_ws/util/ControlHttpUtils.java
| 1 | -package com.bsth.server_ws.util; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSONArray; | |
| 4 | -import com.bsth.entity.ScheduleRealInfo; | |
| 5 | -import com.bsth.util.ConfigUtil; | |
| 6 | -import org.apache.http.client.config.RequestConfig; | |
| 7 | -import org.apache.http.client.methods.CloseableHttpResponse; | |
| 8 | -import org.apache.http.client.methods.HttpGet; | |
| 9 | -import org.apache.http.impl.client.CloseableHttpClient; | |
| 10 | -import org.apache.http.impl.client.HttpClients; | |
| 11 | -import org.apache.http.util.EntityUtils; | |
| 12 | -import org.slf4j.Logger; | |
| 13 | -import org.slf4j.LoggerFactory; | |
| 14 | - | |
| 15 | -import java.io.IOException; | |
| 16 | -import java.util.List; | |
| 17 | - | |
| 18 | -/** | |
| 19 | - * Created by panzhao on 2017/3/15. | |
| 20 | - */ | |
| 21 | -public class ControlHttpUtils { | |
| 22 | - | |
| 23 | - static Logger logger = LoggerFactory.getLogger(ControlHttpUtils.class); | |
| 24 | - | |
| 25 | - static String url; | |
| 26 | - | |
| 27 | - static String secretKey; | |
| 28 | - | |
| 29 | - static { | |
| 30 | - url = ConfigUtil.get("http.control.service_data_url"); | |
| 31 | - secretKey = ConfigUtil.get("http.control.secret.key"); | |
| 32 | - } | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * 从调度系统获取班次信息 | |
| 36 | - * | |
| 37 | - * @return | |
| 38 | - */ | |
| 39 | - public static List<ScheduleRealInfo> getCurrentDayPlan(String companyId, String workId) throws IOException { | |
| 40 | - | |
| 41 | - CloseableHttpClient httpClient = null; | |
| 42 | - List<ScheduleRealInfo> rs = null; | |
| 43 | - httpClient = HttpClients.createDefault(); | |
| 44 | - //超时时间 | |
| 45 | - RequestConfig requestConfig = RequestConfig.custom() | |
| 46 | - .setConnectTimeout(2000).setConnectionRequestTimeout(1000) | |
| 47 | - .setSocketTimeout(3000).build(); | |
| 48 | - | |
| 49 | - HttpGet get = new HttpGet(url + "/getCurrentDayPlan?companyId=" + companyId + "&workId=" + workId + "&secretKey=" + secretKey); | |
| 50 | - get.setConfig(requestConfig); | |
| 51 | - | |
| 52 | - CloseableHttpResponse response = httpClient.execute(get); | |
| 53 | - | |
| 54 | - rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class); | |
| 55 | - return rs; | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * 从调度系统获取进出场班次信息 | |
| 60 | - * @param comanyId | |
| 61 | - * @param jcOrCc | |
| 62 | - * @return | |
| 63 | - */ | |
| 64 | - public static List<ScheduleRealInfo> returnJCCInfo(String companyId, String jcOrCc) throws IOException{ | |
| 65 | - | |
| 66 | - String method = jcOrCc.equals("out")?"/returnCCInfo":"/returnJCInfo"; | |
| 67 | - | |
| 68 | - CloseableHttpClient httpClient = null; | |
| 69 | - List<ScheduleRealInfo> rs = null; | |
| 70 | - httpClient = HttpClients.createDefault(); | |
| 71 | - //超时时间 | |
| 72 | - RequestConfig requestConfig = RequestConfig.custom() | |
| 73 | - .setConnectTimeout(2000).setConnectionRequestTimeout(1000) | |
| 74 | - .setSocketTimeout(3000).build(); | |
| 75 | - | |
| 76 | - HttpGet get = new HttpGet(url + method+"?companyId=" + companyId + "&secretKey=" + secretKey); | |
| 77 | - get.setConfig(requestConfig); | |
| 78 | - | |
| 79 | - CloseableHttpResponse response = httpClient.execute(get); | |
| 80 | - | |
| 81 | - rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class); | |
| 82 | - return rs; | |
| 83 | - } | |
| 84 | -} | |
| 1 | +package com.bsth.server_ws.util; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSONArray; | |
| 4 | +import com.bsth.entity.ScheduleRealInfo; | |
| 5 | +import com.bsth.util.AppProperties; | |
| 6 | +import org.apache.http.client.config.RequestConfig; | |
| 7 | +import org.apache.http.client.methods.CloseableHttpResponse; | |
| 8 | +import org.apache.http.client.methods.HttpGet; | |
| 9 | +import org.apache.http.impl.client.CloseableHttpClient; | |
| 10 | +import org.apache.http.impl.client.HttpClients; | |
| 11 | +import org.apache.http.util.EntityUtils; | |
| 12 | +import org.slf4j.Logger; | |
| 13 | +import org.slf4j.LoggerFactory; | |
| 14 | + | |
| 15 | +import java.io.IOException; | |
| 16 | +import java.util.List; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Created by panzhao on 2017/3/15. | |
| 20 | + */ | |
| 21 | +public class ControlHttpUtils { | |
| 22 | + | |
| 23 | + static Logger logger = LoggerFactory.getLogger(ControlHttpUtils.class); | |
| 24 | + | |
| 25 | + static String url = AppProperties.getUrl(); | |
| 26 | + | |
| 27 | + static String secretKey = AppProperties.getSecretKey(); | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * 从调度系统获取班次信息 | |
| 31 | + * | |
| 32 | + * @return | |
| 33 | + */ | |
| 34 | + public static List<ScheduleRealInfo> getCurrentDayPlan(String companyId, String workId) throws IOException { | |
| 35 | + | |
| 36 | + CloseableHttpClient httpClient = null; | |
| 37 | + List<ScheduleRealInfo> rs = null; | |
| 38 | + httpClient = HttpClients.createDefault(); | |
| 39 | + //超时时间 | |
| 40 | + RequestConfig requestConfig = RequestConfig.custom() | |
| 41 | + .setConnectTimeout(2000).setConnectionRequestTimeout(1000) | |
| 42 | + .setSocketTimeout(3000).build(); | |
| 43 | + | |
| 44 | + HttpGet get = new HttpGet(url + "/getCurrentDayPlan?companyId=" + companyId + "&workId=" + workId + "&secretKey=" + secretKey); | |
| 45 | + get.setConfig(requestConfig); | |
| 46 | + | |
| 47 | + CloseableHttpResponse response = httpClient.execute(get); | |
| 48 | + | |
| 49 | + rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class); | |
| 50 | + return rs; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * 从调度系统获取进出场班次信息 | |
| 55 | + * @param comanyId | |
| 56 | + * @param jcOrCc | |
| 57 | + * @return | |
| 58 | + */ | |
| 59 | + public static List<ScheduleRealInfo> returnJCCInfo(String companyId, String jcOrCc) throws IOException{ | |
| 60 | + | |
| 61 | + String method = jcOrCc.equals("out")?"/returnCCInfo":"/returnJCInfo"; | |
| 62 | + | |
| 63 | + CloseableHttpClient httpClient = null; | |
| 64 | + List<ScheduleRealInfo> rs = null; | |
| 65 | + httpClient = HttpClients.createDefault(); | |
| 66 | + //超时时间 | |
| 67 | + RequestConfig requestConfig = RequestConfig.custom() | |
| 68 | + .setConnectTimeout(2000).setConnectionRequestTimeout(1000) | |
| 69 | + .setSocketTimeout(3000).build(); | |
| 70 | + | |
| 71 | + HttpGet get = new HttpGet(url + method+"?companyId=" + companyId + "&secretKey=" + secretKey); | |
| 72 | + get.setConfig(requestConfig); | |
| 73 | + | |
| 74 | + CloseableHttpResponse response = httpClient.execute(get); | |
| 75 | + | |
| 76 | + rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class); | |
| 77 | + return rs; | |
| 78 | + } | |
| 79 | +} | ... | ... |
src/main/java/com/bsth/util/ConfigUtil.java deleted
100644 → 0
| 1 | -package com.bsth.util; | |
| 2 | - | |
| 3 | -public class ConfigUtil { | |
| 4 | - | |
| 5 | - static Tools tools; | |
| 6 | - | |
| 7 | - static{ | |
| 8 | - tools = new Tools("application.properties"); | |
| 9 | - String active = tools.getValue("spring.profiles.active"); | |
| 10 | - tools = new Tools("application-"+active+".properties"); | |
| 11 | - } | |
| 12 | - | |
| 13 | - public static String get(String key){ | |
| 14 | - return tools.getValue(key); | |
| 15 | - } | |
| 16 | -} |