|
| 1 | +package me.chanjar.weixin.mp.api; |
| 2 | + |
| 3 | +import me.chanjar.weixin.mp.bean.WxMpXmlMessage; |
| 4 | +import sun.applet.Main; |
| 5 | + |
| 6 | +import java.net.SocketTimeoutException; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Random; |
| 9 | +import java.util.concurrent.ConcurrentHashMap; |
| 10 | + |
| 11 | +/** |
| 12 | + * <pre> |
| 13 | + * 消息去重拦截器 |
| 14 | + * 微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次 |
| 15 | + * 使用方法: |
| 16 | + * WxMpMessageRouter router = new WxMpMessageRouter(); |
| 17 | + * router |
| 18 | + * .rule() |
| 19 | + * .interceptor(new WxMpDuplicateMessageInterceptor()) |
| 20 | + * .next() |
| 21 | + * .rule() |
| 22 | + * .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") |
| 23 | + * .interceptor(interceptor, ...).handler(handler, ...) |
| 24 | + * .end() |
| 25 | + * .rule() |
| 26 | + * // 另外一个匹配规则 |
| 27 | + * .end() |
| 28 | + * ; |
| 29 | + * </pre> |
| 30 | + */ |
| 31 | +public class WxMpDuplicateMessageInterceptor implements WxMpMessageInterceptor { |
| 32 | + |
| 33 | + private static final Long PERIOD = 15 * 1000l; |
| 34 | + |
| 35 | + private final ConcurrentHashMap<Long, Long> msgId2timestamp; |
| 36 | + |
| 37 | + public WxMpDuplicateMessageInterceptor() { |
| 38 | + this.msgId2timestamp = new ConcurrentHashMap<Long, Long>(); |
| 39 | + Thread t = new Thread(new Runnable() { |
| 40 | + @Override |
| 41 | + public void run() { |
| 42 | + try { |
| 43 | + while (true) { |
| 44 | + Thread.sleep(PERIOD); |
| 45 | + Long now = System.currentTimeMillis(); |
| 46 | + for (Map.Entry<Long, Long> entry : msgId2timestamp.entrySet()) { |
| 47 | + if (now - entry.getValue() > PERIOD) { |
| 48 | + msgId2timestamp.entrySet().remove(entry); |
| 49 | + } |
| 50 | + } |
| 51 | + msgId2timestamp.clear(); |
| 52 | + } |
| 53 | + } catch (InterruptedException e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + t.setDaemon(true); |
| 59 | + t.start(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService) { |
| 64 | + Long now = System.currentTimeMillis(); |
| 65 | + Long timestamp = msgId2timestamp.putIfAbsent(wxMessage.getMsgId(), now); |
| 66 | + if (timestamp == null) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + if (timestamp.equals(now)) { |
| 70 | + // 第一次接收到这个消息 |
| 71 | + return true; |
| 72 | + } |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + WxMpDuplicateMessageInterceptor d = new WxMpDuplicateMessageInterceptor(); |
| 78 | + Long endTime = System.currentTimeMillis() + 30 * 1000; |
| 79 | + Random r = new Random(); |
| 80 | + |
| 81 | + while(System.currentTimeMillis() < endTime) { |
| 82 | + WxMpXmlMessage m = new WxMpXmlMessage(); |
| 83 | + m.setMsgId(r.nextLong() % 100); |
| 84 | + d.intercept(m, null, null); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | +} |
0 commit comments