🎨 优化部分代码

This commit is contained in:
Binary Wang
2020-09-20 00:21:08 +08:00
parent 7261f23689
commit e00320dd1c
12 changed files with 200 additions and 228 deletions

View File

@ -45,7 +45,7 @@ public class WxMaMessageRouter {
this.wxMaService = wxMaService;
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("WxMaMessageRouter-pool-%d").build();
this.executorService = new ThreadPoolExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_POOL_SIZE,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), namedThreadFactory);
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
this.sessionManager = new StandardSessionManager();
this.exceptionHandler = new LogExceptionHandler();
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
@ -88,11 +88,8 @@ public class WxMaMessageRouter {
// 返回最后一个非异步的rule的执行结果
if (rule.isAsync()) {
futures.add(
this.executorService.submit(new Runnable() {
@Override
public void run() {
rule.service(wxMessage, context, WxMaMessageRouter.this.wxMaService, WxMaMessageRouter.this.sessionManager, WxMaMessageRouter.this.exceptionHandler);
}
this.executorService.submit(() -> {
rule.service(wxMessage, context, WxMaMessageRouter.this.wxMaService, WxMaMessageRouter.this.sessionManager, WxMaMessageRouter.this.exceptionHandler);
})
);
} else {
@ -104,18 +101,15 @@ public class WxMaMessageRouter {
}
if (futures.size() > 0) {
this.executorService.submit(new Runnable() {
@Override
public void run() {
for (Future<?> future : futures) {
try {
future.get();
WxMaMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser());
// 异步操作结束session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException | ExecutionException e) {
WxMaMessageRouter.this.log.error("Error happened when wait task finish", e);
}
this.executorService.submit(() -> {
for (Future<?> future : futures) {
try {
future.get();
WxMaMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser());
// 异步操作结束session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException | ExecutionException e) {
WxMaMessageRouter.this.log.error("Error happened when wait task finish", e);
}
}
});
@ -124,7 +118,7 @@ public class WxMaMessageRouter {
}
public WxMaXmlOutMessage route(final WxMaMessage wxMessage) {
return this.route(wxMessage, new HashMap<String, Object>(2));
return this.route(wxMessage, new HashMap<>(2));
}
private boolean isMsgDuplicated(WxMaMessage wxMessage) {

View File

@ -6,10 +6,7 @@ import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Pattern;
/**
@ -135,9 +132,7 @@ public class WxMaMessageRouterRule {
public WxMaMessageRouterRule interceptor(WxMaMessageInterceptor interceptor, WxMaMessageInterceptor... otherInterceptors) {
this.interceptors.add(interceptor);
if (otherInterceptors != null && otherInterceptors.length > 0) {
for (WxMaMessageInterceptor i : otherInterceptors) {
this.interceptors.add(i);
}
Collections.addAll(this.interceptors, otherInterceptors);
}
return this;
}
@ -155,9 +150,7 @@ public class WxMaMessageRouterRule {
public WxMaMessageRouterRule handler(WxMaMessageHandler handler, WxMaMessageHandler... otherHandlers) {
this.handlers.add(handler);
if (otherHandlers != null && otherHandlers.length > 0) {
for (WxMaMessageHandler i : otherHandlers) {
this.handlers.add(i);
}
Collections.addAll(this.handlers, otherHandlers);
}
return this;
}