diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index 43e18d2..974744a 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; import com.hotlcc.wechat4j.api.WebWeixinApi; import com.hotlcc.wechat4j.enums.ExitTypeEnum; import com.hotlcc.wechat4j.enums.LoginTipEnum; +import com.hotlcc.wechat4j.handler.ExitEventHandler; import com.hotlcc.wechat4j.util.CommonUtil; import com.hotlcc.wechat4j.util.PropertiesUtil; import com.hotlcc.wechat4j.util.QRCodeUtil; @@ -26,6 +27,9 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -61,6 +65,9 @@ public class Wechat { //同步监听器 private volatile SyncMonitor syncMonitor; + //退出事件处理器 + private List exitEventHandlers; + public Wechat(CookieStore cookieStore) { this.cookieStore = cookieStore; this.httpClient = buildHttpClient(cookieStore); @@ -74,6 +81,21 @@ public class Wechat { this.webWeixinApi = webWeixinApi; } + public void addExitEventHandler(ExitEventHandler handler) { + if (exitEventHandlers == null) { + exitEventHandlers = new ArrayList<>(); + } + + exitEventHandlers.add(handler); + } + + public void addExitEventHandler(Collection handlers) { + if (exitEventHandlers == null) { + exitEventHandlers = new ArrayList<>(); + } + exitEventHandlers.addAll(handlers); + } + private HttpClient buildHttpClient(CookieStore cookieStore) { ConnectionKeepAliveStrategy keepAliveStrategy = new DefaultConnectionKeepAliveStrategy() { @Override @@ -469,7 +491,7 @@ public class Wechat { statusNotify(time); isOnline = true; - syncMonitor = new SyncMonitor(); + syncMonitor = new SyncMonitor(this); syncMonitor.start(); } finally { isOnlineLock.unlock(); @@ -501,10 +523,25 @@ public class Wechat { } } + /** + * 判断在线状态 + * + * @return + */ + public boolean isOnline() { + return isOnline; + } + /** * 微信同步监听器(心跳) */ private class SyncMonitor extends Thread { + private Wechat wechat; + + public SyncMonitor(Wechat wechat) { + this.wechat = wechat; + } + @Override public void run() { int time = PropertiesUtil.getIntValue("wechat4j.syncCheck.retry.time", 5); @@ -567,12 +604,46 @@ public class Wechat { */ private void processExitEvent(ExitTypeEnum type, Throwable t) { try { + if (exitEventHandlers == null) { + return; + } + for (ExitEventHandler handler : exitEventHandlers) { + if (handler != null) { + processExitEvent(type, t, handler); + } + } } catch (Exception e) { logger.error("Exit event process error.", e); } } + private void processExitEvent(ExitTypeEnum type, Throwable t, ExitEventHandler handler) { + try { + switch (type) { + case ERROR_EXIT: + handler.forError(wechat); + break; + case REMOTE_EXIT: + handler.forRemote(wechat); + break; + case LOCAL_EXIT: + handler.forLocal(wechat); + break; + default: + break; + } + } catch (Exception e) { + logger.error("Exit event \"" + type.name() + "\" process error.", e); + } + + try { + handler.forAll(wechat, type, t); + } catch (Exception e) { + logger.error("ForAll Exit event process error.", e); + } + } + /** * 处理selector值 * @@ -825,8 +896,4 @@ public class Wechat { return null; } - - public void test() { - - } } diff --git a/src/main/java/com/hotlcc/wechat4j/handler/ExitEventHandler.java b/src/main/java/com/hotlcc/wechat4j/handler/ExitEventHandler.java new file mode 100644 index 0000000..17a75a1 --- /dev/null +++ b/src/main/java/com/hotlcc/wechat4j/handler/ExitEventHandler.java @@ -0,0 +1,39 @@ +package com.hotlcc.wechat4j.handler; + +import com.hotlcc.wechat4j.Wechat; +import com.hotlcc.wechat4j.enums.ExitTypeEnum; + +/** + * 退出事件处理器 + */ +public interface ExitEventHandler { + /** + * 针对所有类型的退出事件 + * + * @param wechat + * @param type + * @param t + */ + void forAll(Wechat wechat, ExitTypeEnum type, Throwable t); + + /** + * 针对错误导致的退出事件 + * + * @param wechat + */ + void forError(Wechat wechat); + + /** + * 针对远程人为导致的退出事件 + * + * @param wechat + */ + void forRemote(Wechat wechat); + + /** + * 针对本地任务导致的退出事件 + * + * @param wechat + */ + void forLocal(Wechat wechat); +} diff --git a/src/test/java/TestClass2.java b/src/test/java/TestClass2.java index 61a3c97..8efb235 100644 --- a/src/test/java/TestClass2.java +++ b/src/test/java/TestClass2.java @@ -8,7 +8,6 @@ public class TestClass2 { Wechat wechat = new Wechat(); wechat.setWebWeixinApi(api); wechat.autoLogin(); - wechat.test(); CommonUtil.threadSleep(1000 * 60 * 10); wechat.logout(); }