Commit 60f8673d9ff23eff4229006972bc5eae4734afd0
1 parent
de274183
1.对处理GPS的线程池做了超时处理,以及出现超时后通知处理
Showing
3 changed files
with
77 additions
and
11 deletions
src/main/java/com/bsth/data/ThreadMonotor.java
| ... | ... | @@ -2,6 +2,9 @@ package com.bsth.data; |
| 2 | 2 | |
| 3 | 3 | import com.bsth.data.gpsdata_v2.DataHandleProcess; |
| 4 | 4 | import com.bsth.data.gpsdata_v2.thread.GpsDataLoaderThread; |
| 5 | + | |
| 6 | +import org.slf4j.Logger; | |
| 7 | +import org.slf4j.LoggerFactory; | |
| 5 | 8 | import org.springframework.stereotype.Component; |
| 6 | 9 | |
| 7 | 10 | /** |
| ... | ... | @@ -9,11 +12,15 @@ import org.springframework.stereotype.Component; |
| 9 | 12 | */ |
| 10 | 13 | @Component |
| 11 | 14 | public class ThreadMonotor extends Thread{ |
| 15 | + | |
| 16 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 12 | 17 | |
| 13 | 18 | @Override |
| 14 | 19 | public void run() { |
| 15 | 20 | //切换到备用的网关实时GPS对照数据 |
| 16 | - if(DataHandleProcess.isBlock()) | |
| 17 | - GpsDataLoaderThread.setFlag(-1); | |
| 21 | + if(DataHandleProcess.isBlock()) { | |
| 22 | + GpsDataLoaderThread.setFlag(-1); | |
| 23 | + logger.error("切换到备用GPS数据源"); | |
| 24 | + } | |
| 18 | 25 | } |
| 19 | 26 | } |
| 20 | 27 | \ No newline at end of file | ... | ... |
src/main/java/com/bsth/data/gpsdata_v2/DataHandleProcess.java
| ... | ... | @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON; |
| 4 | 4 | import com.bsth.data.gpsdata_v2.cache.GpsCacheData; |
| 5 | 5 | import com.bsth.data.gpsdata_v2.entity.GpsEntity; |
| 6 | 6 | import com.bsth.data.gpsdata_v2.handlers.*; |
| 7 | +import com.bsth.email.SendEmailController; | |
| 8 | +import com.bsth.email.entity.EmailBean; | |
| 7 | 9 | import com.google.common.collect.ArrayListMultimap; |
| 8 | 10 | import org.apache.commons.lang3.StringUtils; |
| 9 | 11 | import org.slf4j.Logger; |
| ... | ... | @@ -15,6 +17,8 @@ import java.util.*; |
| 15 | 17 | import java.util.concurrent.CountDownLatch; |
| 16 | 18 | import java.util.concurrent.ExecutorService; |
| 17 | 19 | import java.util.concurrent.Executors; |
| 20 | +import java.util.concurrent.ThreadFactory; | |
| 21 | +import java.util.concurrent.TimeUnit; | |
| 18 | 22 | |
| 19 | 23 | /** |
| 20 | 24 | * 实时信号数据处理 |
| ... | ... | @@ -37,13 +41,27 @@ public class DataHandleProcess { |
| 37 | 41 | ReverseRouteProcess reverseRouteProcess; |
| 38 | 42 | @Autowired |
| 39 | 43 | GpsRealData gpsRealData; |
| 44 | + // 发送邮件 | |
| 45 | + @Autowired | |
| 46 | + private SendEmailController sendEmailController; | |
| 40 | 47 | |
| 41 | 48 | |
| 42 | 49 | static Logger logger = LoggerFactory.getLogger(DataHandleProcess.class); |
| 43 | 50 | |
| 44 | 51 | final static int POOL_SIZE = 20; |
| 45 | 52 | |
| 46 | - static ExecutorService threadPool = Executors.newFixedThreadPool(POOL_SIZE + 1); | |
| 53 | + static ExecutorService threadPool = Executors.newFixedThreadPool(POOL_SIZE + 1, new ThreadFactory() { | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public Thread newThread(Runnable r) { | |
| 57 | + // TODO Auto-generated method stub | |
| 58 | + Thread t = new Thread(r); | |
| 59 | + t.setName("GPSProcessor"); | |
| 60 | + | |
| 61 | + return t; | |
| 62 | + } | |
| 63 | + | |
| 64 | + }); | |
| 47 | 65 | public static CountDownLatch count; |
| 48 | 66 | |
| 49 | 67 | static long lastTime; |
| ... | ... | @@ -51,7 +69,22 @@ public class DataHandleProcess { |
| 51 | 69 | public static boolean isBlock() { |
| 52 | 70 | return System.currentTimeMillis() - lastTime > 1000 * 30; |
| 53 | 71 | } |
| 54 | - | |
| 72 | + | |
| 73 | + private void shutdownAndAwaitTermination(ExecutorService pool) { | |
| 74 | + pool.shutdown(); | |
| 75 | + try { | |
| 76 | + if (!pool.awaitTermination(500, TimeUnit.MILLISECONDS)) { | |
| 77 | + pool.shutdownNow(); | |
| 78 | + } | |
| 79 | + if (!pool.awaitTermination(500, TimeUnit.MILLISECONDS)) { | |
| 80 | + logger.error("线程池无法正常终止"); | |
| 81 | + } | |
| 82 | + } catch (InterruptedException e) { | |
| 83 | + pool.shutdown(); | |
| 84 | + Thread.currentThread().interrupt(); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 55 | 88 | public void handle(List<GpsEntity> list) { |
| 56 | 89 | try { |
| 57 | 90 | if (list.size() == 0) |
| ... | ... | @@ -85,7 +118,32 @@ public class DataHandleProcess { |
| 85 | 118 | |
| 86 | 119 | |
| 87 | 120 | //等待子线程结束 |
| 88 | - count.await(); | |
| 121 | + boolean isNormal = count.await(5, TimeUnit.SECONDS); | |
| 122 | + if (!isNormal) { | |
| 123 | + try { | |
| 124 | + shutdownAndAwaitTermination(threadPool); | |
| 125 | + threadPool = Executors.newFixedThreadPool(POOL_SIZE + 1, new ThreadFactory() { | |
| 126 | + | |
| 127 | + @Override | |
| 128 | + public Thread newThread(Runnable r) { | |
| 129 | + // TODO Auto-generated method stub | |
| 130 | + Thread t = new Thread(r); | |
| 131 | + t.setName("GPSProcessor"); | |
| 132 | + | |
| 133 | + return t; | |
| 134 | + } | |
| 135 | + | |
| 136 | + }); | |
| 137 | + //发送邮件 | |
| 138 | + EmailBean mail = new EmailBean(); | |
| 139 | + mail.setSubject("线调GPS处理<br/>"); | |
| 140 | + mail.setContent("GPS处理超时,检查线程栈文件信息<br/>"); | |
| 141 | + sendEmailController.sendMail("113252620@qq.com", mail); | |
| 142 | + logger.info("DataHandlerProcess:邮件发送成功!"); | |
| 143 | + } catch (Exception e){ | |
| 144 | + logger.error("DataHandlerProcess:邮件发送失败!",e); | |
| 145 | + } | |
| 146 | + } | |
| 89 | 147 | |
| 90 | 148 | //加入实时gps对照 |
| 91 | 149 | for (GpsEntity gps : list) |
| ... | ... | @@ -115,6 +173,7 @@ public class DataHandleProcess { |
| 115 | 173 | Collections.sort(list, comp); |
| 116 | 174 | GpsEntity gps; |
| 117 | 175 | for(int i = 0,len = list.size(); i< len ;i ++){ |
| 176 | + if (Thread.currentThread().isInterrupted()) break; | |
| 118 | 177 | gps = list.get(i); |
| 119 | 178 | |
| 120 | 179 | try { | ... | ... |
src/main/resources/logback.xml
| ... | ... | @@ -72,7 +72,7 @@ |
| 72 | 72 | </rollingPolicy> |
| 73 | 73 | |
| 74 | 74 | <layout class="ch.qos.logback.classic.PatternLayout"> |
| 75 | - <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] %-5level -%msg%n | |
| 75 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level -%msg%n | |
| 76 | 76 | </pattern> |
| 77 | 77 | </layout> |
| 78 | 78 | </appender> |
| ... | ... | @@ -102,7 +102,7 @@ |
| 102 | 102 | </rollingPolicy> |
| 103 | 103 | |
| 104 | 104 | <layout class="ch.qos.logback.classic.PatternLayout"> |
| 105 | - <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] %-5level -%msg%n | |
| 105 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level -%msg%n | |
| 106 | 106 | </pattern> |
| 107 | 107 | </layout> |
| 108 | 108 | </appender> |
| ... | ... | @@ -124,7 +124,7 @@ |
| 124 | 124 | </rollingPolicy> |
| 125 | 125 | |
| 126 | 126 | <layout class="ch.qos.logback.classic.PatternLayout"> |
| 127 | - <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] %-5level -%msg%n | |
| 127 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level -%msg%n | |
| 128 | 128 | </pattern> |
| 129 | 129 | </layout> |
| 130 | 130 | </appender> |
| ... | ... | @@ -141,7 +141,7 @@ |
| 141 | 141 | </rollingPolicy> |
| 142 | 142 | |
| 143 | 143 | <layout class="ch.qos.logback.classic.PatternLayout"> |
| 144 | - <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] %-5level -%msg%n | |
| 144 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level -%msg%n | |
| 145 | 145 | </pattern> |
| 146 | 146 | </layout> |
| 147 | 147 | </appender> |
| ... | ... | @@ -213,7 +213,7 @@ |
| 213 | 213 | </rollingPolicy> |
| 214 | 214 | |
| 215 | 215 | <layout class="ch.qos.logback.classic.PatternLayout"> |
| 216 | - <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] %-5level -%msg%n | |
| 216 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level -%msg%n | |
| 217 | 217 | </pattern> |
| 218 | 218 | </layout> |
| 219 | 219 | </appender> |
| ... | ... | @@ -235,7 +235,7 @@ |
| 235 | 235 | </rollingPolicy> |
| 236 | 236 | |
| 237 | 237 | <layout class="ch.qos.logback.classic.PatternLayout"> |
| 238 | - <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] %-5level -%msg%n | |
| 238 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level -%msg%n | |
| 239 | 239 | </pattern> |
| 240 | 240 | </layout> |
| 241 | 241 | </appender> | ... | ... |