11package me .chanjar .weixin .cp .api ;
22
3+ import me .chanjar .weixin .common .util .WxMsgIdDuplicateChecker ;
4+ import me .chanjar .weixin .common .util .WxMsgIdInMemoryDuplicateChecker ;
35import me .chanjar .weixin .cp .bean .WxCpXmlMessage ;
46import me .chanjar .weixin .cp .bean .WxCpXmlOutMessage ;
57
@@ -45,18 +47,38 @@ public class WxCpMessageRouter {
4547
4648 private final List <Rule > rules = new ArrayList <Rule >();
4749
48- private final ExecutorService executorService ;
49-
5050 private final WxCpService wxCpService ;
5151
52+ private ExecutorService executorService ;
53+
54+ private WxMsgIdDuplicateChecker wxMsgIdDuplicateChecker ;
55+
5256 public WxCpMessageRouter (WxCpService wxCpService ) {
5357 this .wxCpService = wxCpService ;
5458 this .executorService = Executors .newFixedThreadPool (DEFAULT_THREAD_POOL_SIZE );
59+ this .wxMsgIdDuplicateChecker = new WxMsgIdInMemoryDuplicateChecker ();
5560 }
5661
5762 public WxCpMessageRouter (WxCpService wxMpService , int threadPoolSize ) {
5863 this .wxCpService = wxMpService ;
5964 this .executorService = Executors .newFixedThreadPool (threadPoolSize );
65+ this .wxMsgIdDuplicateChecker = new WxMsgIdInMemoryDuplicateChecker ();
66+ }
67+
68+ /**
69+ * 设置自定义的ExecutorService
70+ * @param executorService
71+ */
72+ public void setExecutorService (ExecutorService executorService ) {
73+ this .executorService = executorService ;
74+ }
75+
76+ /**
77+ * 设置自定义的WxMsgIdDuplicateChecker
78+ * @param wxMsgIdDuplicateChecker
79+ */
80+ public void setWxMsgIdDuplicateChecker (WxMsgIdDuplicateChecker wxMsgIdDuplicateChecker ) {
81+ this .wxMsgIdDuplicateChecker = wxMsgIdDuplicateChecker ;
6082 }
6183
6284 /**
@@ -72,6 +94,11 @@ public Rule rule() {
7294 * @param wxMessage
7395 */
7496 public WxCpXmlOutMessage route (final WxCpXmlMessage wxMessage ) {
97+ if (wxMsgIdDuplicateChecker .isDuplicate (wxMessage .getMsgId ())) {
98+ // 如果是重复消息,那么就不做处理
99+ return null ;
100+ }
101+
75102 final List <Rule > matchRules = new ArrayList <Rule >();
76103 // 收集匹配的规则
77104 for (final Rule rule : rules ) {
0 commit comments