正确处理InterruptException

Sonar-2142: "InterruptedException" should not be ignored
This commit is contained in:
Binary Wang 2018-11-24 21:20:59 +08:00
parent 9ed9c2a58c
commit 856b021d2c
7 changed files with 60 additions and 43 deletions

View File

@ -76,7 +76,7 @@ public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChec
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); Thread.currentThread().interrupt();
} }
} }
}); });

View File

@ -196,6 +196,7 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
Thread.sleep(StandardSessionManager.this.backgroundProcessorDelay * 1000L); Thread.sleep(StandardSessionManager.this.backgroundProcessorDelay * 1000L);
backgroundProcess(); backgroundProcess();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
StandardSessionManager.this.log.error("SessionManagerImpl.backgroundProcess error", e); StandardSessionManager.this.log.error("SessionManagerImpl.backgroundProcess error", e);
} }
} }

View File

@ -1,5 +1,9 @@
package me.chanjar.weixin.common.util.http.apache; package me.chanjar.weixin.common.util.http.apache;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
@ -25,10 +29,6 @@ import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* httpclient 连接管理器 自带DNS解析. * httpclient 连接管理器 自带DNS解析.
* <p>大部分代码拷贝自DefaultApacheHttpClientBuilder</p> * <p>大部分代码拷贝自DefaultApacheHttpClientBuilder</p>
@ -292,6 +292,7 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder {
} }
} }
} catch (InterruptedException ignore) { } catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
} }
} }

View File

@ -1,5 +1,11 @@
package me.chanjar.weixin.cp.api.impl; package me.chanjar.weixin.cp.api.impl;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -17,14 +23,17 @@ import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.cp.api.*; import me.chanjar.weixin.cp.api.WxCpAgentService;
import me.chanjar.weixin.cp.bean.*; import me.chanjar.weixin.cp.api.WxCpDepartmentService;
import me.chanjar.weixin.cp.api.WxCpMediaService;
import me.chanjar.weixin.cp.api.WxCpMenuService;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpTagService;
import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
public abstract class WxCpServiceAbstractImpl<H, P> implements WxCpService, RequestHttp<H, P> { public abstract class WxCpServiceAbstractImpl<H, P> implements WxCpService, RequestHttp<H, P> {
protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final Logger log = LoggerFactory.getLogger(this.getClass());
@ -184,7 +193,7 @@ public abstract class WxCpServiceAbstractImpl<H, P> implements WxCpService, Requ
this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1); this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
Thread.sleep(sleepMillis); Thread.sleep(sleepMillis);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
throw new RuntimeException(e1); Thread.currentThread().interrupt();
} }
} else { } else {
throw e; throw e;

View File

@ -1,5 +1,18 @@
package me.chanjar.weixin.cp.message; package me.chanjar.weixin.cp.message;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
@ -11,18 +24,6 @@ import me.chanjar.weixin.common.util.LogExceptionHandler;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/** /**
* <pre> * <pre>
@ -195,6 +196,7 @@ public class WxCpMessageRouter {
sessionEndAccess(wxMessage); sessionEndAccess(wxMessage);
} catch (InterruptedException e) { } catch (InterruptedException e) {
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e); WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) { } catch (ExecutionException e) {
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e); WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
} }
@ -207,12 +209,11 @@ public class WxCpMessageRouter {
/** /**
* 处理微信消息 * 处理微信消息.
* *
* @param wxMessage
*/ */
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) { public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
return this.route(wxMessage, new HashMap<String, Object>()); return this.route(wxMessage, new HashMap<String, Object>(2));
} }
protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) { protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) {

View File

@ -212,7 +212,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1); log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
Thread.sleep(sleepMillis); Thread.sleep(sleepMillis);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
throw new RuntimeException(e1); Thread.currentThread().interrupt();
} }
} else { } else {
throw e; throw e;

View File

@ -1,5 +1,18 @@
package me.chanjar.weixin.mp.api; package me.chanjar.weixin.mp.api;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
@ -10,18 +23,6 @@ import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.LogExceptionHandler; import me.chanjar.weixin.common.util.LogExceptionHandler;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/** /**
* <pre> * <pre>
@ -155,6 +156,7 @@ public class WxMpMessageRouter {
public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage, final Map<String, Object> context) { public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage, final Map<String, Object> context) {
return route(wxMessage, context, null); return route(wxMessage, context, null);
} }
/** /**
* 处理微信消息 * 处理微信消息
*/ */
@ -214,7 +216,10 @@ public class WxMpMessageRouter {
WxMpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser()); WxMpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser());
// 异步操作结束session访问结束 // 异步操作结束session访问结束
sessionEndAccess(wxMessage); sessionEndAccess(wxMessage);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException e) {
WxMpMessageRouter.this.log.error("Error happened when wait task finish", e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
WxMpMessageRouter.this.log.error("Error happened when wait task finish", e); WxMpMessageRouter.this.log.error("Error happened when wait task finish", e);
} }
} }