mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	issue #69 添加Session的支持
This commit is contained in:
		| @ -1,16 +1,25 @@ | ||||
| package me.chanjar.weixin.mp.api; | ||||
|  | ||||
| import me.chanjar.weixin.common.session.InternalSession; | ||||
| import me.chanjar.weixin.common.session.SessionManagerImpl; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import me.chanjar.weixin.common.session.WxSessionManager; | ||||
| import me.chanjar.weixin.common.util.WxMsgIdDuplicateChecker; | ||||
| import me.chanjar.weixin.common.util.WxMsgIdMemoryDuplicateChecker; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import javax.swing.text.StyledEditorKit; | ||||
| 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 java.util.regex.Pattern; | ||||
|  | ||||
| /** | ||||
| @ -41,6 +50,8 @@ import java.util.regex.Pattern; | ||||
|  */ | ||||
| public class WxMpMessageRouter { | ||||
|  | ||||
|   protected final Logger log = LoggerFactory.getLogger(WxMpMessageRouter.class); | ||||
|  | ||||
|   private static final int DEFAULT_THREAD_POOL_SIZE = 20; | ||||
|  | ||||
|   private final List<Rule> rules = new ArrayList<Rule>(); | ||||
| @ -51,6 +62,8 @@ public class WxMpMessageRouter { | ||||
|  | ||||
|   private WxMsgIdDuplicateChecker wxMsgIdDuplicateChecker; | ||||
|  | ||||
|   protected WxSessionManager sessionManager = new SessionManagerImpl(); | ||||
|  | ||||
|   public WxMpMessageRouter(WxMpService wxMpService) { | ||||
|     this.wxMpService = wxMpService; | ||||
|     this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); | ||||
| @ -113,21 +126,54 @@ public class WxMpMessageRouter { | ||||
|     } | ||||
|      | ||||
|     WxMpXmlOutMessage res = null; | ||||
|     final List<Future> futures = new ArrayList<Future>(); | ||||
|     for (final Rule rule : matchRules) { | ||||
|       // 返回最后一个非异步的rule的执行结果 | ||||
|       if(rule.async) { | ||||
|         executorService.submit(new Runnable() { | ||||
|           public void run() { | ||||
|             rule.service(wxMessage); | ||||
|           } | ||||
|         }); | ||||
|         futures.add( | ||||
|           executorService.submit(new Runnable() { | ||||
|             public void run() { | ||||
|               rule.service(wxMessage); | ||||
|             } | ||||
|           }) | ||||
|         ); | ||||
|       } else { | ||||
|         res = rule.service(wxMessage); | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     // 告诉session,它已经用不着了 | ||||
|     if (futures.size() > 0) { | ||||
|       executorService.submit(new Runnable() { | ||||
|         @Override | ||||
|         public void run() { | ||||
|           for (Future future : futures) { | ||||
|             try { | ||||
|               future.get(); | ||||
|             } catch (InterruptedException e) { | ||||
|               log.error("Error happened when wait task finish", e); | ||||
|             } catch (ExecutionException e) { | ||||
|               log.error("Error happened when wait task finish", e); | ||||
|             } | ||||
|           } | ||||
|           // 在这里session再也不会被使用了 | ||||
|           sessionEndAccess(wxMessage); | ||||
|         } | ||||
|       }); | ||||
|     } else { | ||||
|       // 在这里session再也不会被使用了 | ||||
|       sessionEndAccess(wxMessage); | ||||
|     } | ||||
|     return res; | ||||
|   } | ||||
|    | ||||
|  | ||||
|   protected void sessionEndAccess(WxMpXmlMessage wxMessage) { | ||||
|     WxSession session = sessionManager.getSession(wxMessage.getFromUserName(), false); | ||||
|     if (session != null) { | ||||
|       ((InternalSession) session).endAccess(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public static class Rule { | ||||
|      | ||||
|     private final WxMpMessageRouter routerBuilder; | ||||
|  | ||||
| @ -3,6 +3,8 @@ package me.chanjar.weixin.mp.api; | ||||
| import me.chanjar.weixin.common.bean.WxMenu; | ||||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import me.chanjar.weixin.common.session.WxSessionManager; | ||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||
| import me.chanjar.weixin.mp.bean.*; | ||||
| import me.chanjar.weixin.mp.bean.result.*; | ||||
| @ -456,10 +458,10 @@ public interface WxMpService { | ||||
|    */ | ||||
|   public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; | ||||
|  | ||||
|     /** | ||||
|      * 注入 {@link WxMpConfigStorage} 的实现 | ||||
|      * @param wxConfigProvider | ||||
|      */ | ||||
|   /** | ||||
|    * 注入 {@link WxMpConfigStorage} 的实现 | ||||
|    * @param wxConfigProvider | ||||
|    */ | ||||
|   public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider); | ||||
|  | ||||
|   /** | ||||
| @ -479,4 +481,5 @@ public interface WxMpService { | ||||
|    * @param maxRetryTimes | ||||
|    */ | ||||
|   void setMaxRetryTimes(int maxRetryTimes); | ||||
|  | ||||
| } | ||||
|  | ||||
| @ -10,6 +10,9 @@ import me.chanjar.weixin.common.bean.WxMenu; | ||||
| import me.chanjar.weixin.common.bean.result.WxError; | ||||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.session.SessionManagerImpl; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import me.chanjar.weixin.common.session.WxSessionManager; | ||||
| import me.chanjar.weixin.common.util.StringUtils; | ||||
| import me.chanjar.weixin.common.util.crypto.SHA1; | ||||
| import me.chanjar.weixin.common.util.fs.FileUtils; | ||||
| @ -66,6 +69,8 @@ public class WxMpServiceImpl implements WxMpService { | ||||
|  | ||||
|   private int maxRetryTimes = 5; | ||||
|  | ||||
|   protected WxSessionManager sessionManager = new SessionManagerImpl(); | ||||
|  | ||||
|   public boolean checkSignature(String timestamp, String nonce, String signature) { | ||||
|     try { | ||||
|       return SHA1.gen(wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature); | ||||
| @ -545,7 +550,6 @@ public class WxMpServiceImpl implements WxMpService { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|  | ||||
|   @Override | ||||
|   public void setRetrySleepMillis(int retrySleepMillis) { | ||||
|     this.retrySleepMillis = retrySleepMillis; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Daniel Qian
					Daniel Qian