This commit is contained in:
Daniel Qian
2015-01-20 09:49:39 +08:00
parent 85168a14aa
commit 9fe73319d1
7 changed files with 199 additions and 92 deletions

View File

@ -1,88 +0,0 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import sun.applet.Main;
import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
/**
* <pre>
* 消息去重拦截器
* 微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次
* 使用方法:
* WxMpMessageRouter router = new WxMpMessageRouter();
* router
* .rule()
* .interceptor(new WxMpDuplicateMessageInterceptor())
* .next()
* .rule()
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT")
* .interceptor(interceptor, ...).handler(handler, ...)
* .end()
* .rule()
* // 另外一个匹配规则
* .end()
* ;
* </pre>
*/
public class WxMpDuplicateMessageInterceptor implements WxMpMessageInterceptor {
private static final Long PERIOD = 15 * 1000l;
private final ConcurrentHashMap<Long, Long> msgId2timestamp;
public WxMpDuplicateMessageInterceptor() {
this.msgId2timestamp = new ConcurrentHashMap<Long, Long>();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
Thread.sleep(PERIOD);
Long now = System.currentTimeMillis();
for (Map.Entry<Long, Long> entry : msgId2timestamp.entrySet()) {
if (now - entry.getValue() > PERIOD) {
msgId2timestamp.entrySet().remove(entry);
}
}
msgId2timestamp.clear();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
}
@Override
public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService) {
Long now = System.currentTimeMillis();
Long timestamp = msgId2timestamp.putIfAbsent(wxMessage.getMsgId(), now);
if (timestamp == null) {
return true;
}
if (timestamp.equals(now)) {
// 第一次接收到这个消息
return true;
}
return false;
}
public static void main(String[] args) {
WxMpDuplicateMessageInterceptor d = new WxMpDuplicateMessageInterceptor();
Long endTime = System.currentTimeMillis() + 30 * 1000;
Random r = new Random();
while(System.currentTimeMillis() < endTime) {
WxMpXmlMessage m = new WxMpXmlMessage();
m.setMsgId(r.nextLong() % 100);
d.intercept(m, null, null);
}
}
}

View File

@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.util.WxMsgIdDuplicateChecker;
import me.chanjar.weixin.common.util.WxMsgIdInMemoryDuplicateChecker;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
@ -45,18 +47,38 @@ public class WxMpMessageRouter {
private final List<Rule> rules = new ArrayList<Rule>();
private final ExecutorService executorService;
private final WxMpService wxMpService;
private ExecutorService executorService;
private WxMsgIdDuplicateChecker wxMsgIdDuplicateChecker;
public WxMpMessageRouter(WxMpService wxMpService) {
this.wxMpService = wxMpService;
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
this.wxMsgIdDuplicateChecker = new WxMsgIdInMemoryDuplicateChecker();
}
public WxMpMessageRouter(WxMpService wxMpService, int threadPoolSize) {
this.wxMpService = wxMpService;
this.executorService = Executors.newFixedThreadPool(threadPoolSize);
this.wxMsgIdDuplicateChecker = new WxMsgIdInMemoryDuplicateChecker();
}
/**
* 设置自定义的ExecutorService
* @param executorService
*/
public void setExecutorService(ExecutorService executorService) {
this.executorService = executorService;
}
/**
* 设置自定义的WxMsgIdDuplicateChecker
* @param wxMsgIdDuplicateChecker
*/
public void setWxMsgIdDuplicateChecker(WxMsgIdDuplicateChecker wxMsgIdDuplicateChecker) {
this.wxMsgIdDuplicateChecker = wxMsgIdDuplicateChecker;
}
/**
@ -72,6 +94,11 @@ public class WxMpMessageRouter {
* @param wxMessage
*/
public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage) {
if (wxMsgIdDuplicateChecker.isDuplicate(wxMessage.getMsgId())) {
// 如果是重复消息,那么就不做处理
return null;
}
final List<Rule> matchRules = new ArrayList<Rule>();
// 收集匹配的规则
for (final Rule rule : rules) {