From 506e4db7e186045209b0ba4a5a6e8b9b9e436533 Mon Sep 17 00:00:00 2001 From: endian11 <Dana_Lee1016@126.com> Date: 星期四, 12 十二月 2019 16:58:16 +0800 Subject: [PATCH] 删除IM_lib IMSClinetBOotstrap;app模块增加多线程Util和tcp; MessageProcessor用来收发消息,IMSCLientBootstrap初始化im_lib, --- app/src/main/java/safeluck/drive/evaluation/im/IMSConnectStatusListener.java | 26 ++ /dev/null | 61 ----- app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java | 65 +++++ app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java | 12 + app/src/main/java/safeluck/drive/evaluation/im/handler/IMessageHandler.java | 11 app/src/main/java/safeluck/drive/evaluation/im/IMSClientFactory.java | 2 app/src/main/java/safeluck/drive/evaluation/im/handler/AbstractMessageHandler.java | 15 + app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java | 104 ++++++++ app/src/main/java/safeluck/drive/evaluation/util/CThreadPoolExecutor.java | 332 +++++++++++++++++++++++++++ app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java | 43 +++ 10 files changed, 609 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java new file mode 100644 index 0000000..5457509 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java @@ -0,0 +1,104 @@ +package safeluck.drive.evaluation.im; + +import android.util.Log; + +import com.anyun.im_lib.interf.IMSClientInteface; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + + + +import java.util.Vector; + + + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:05:30 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public class IMSClientBootstrap { + + private static final String TAG = "IMSClientBootstrap"; + + private static final IMSClientBootstrap INSTANCE= new IMSClientBootstrap(); + private IMSClientInteface imsClient; + + /**鏍囪IMSClientBootstrap鏄惁宸茬粡鍒濆鍖�**/ + private boolean isActive; + + + private IMSClientBootstrap(){ + + } + + public static IMSClientBootstrap getInstance(){ + return INSTANCE; + } + + /** + * + * @param userId + * @param token + * @param hosts + * @param appStatus + */ + public synchronized void init(String userId,String token,String hosts,int appStatus){ + if (!isActive){ + Vector<String> serverUrlList = convertHosts(hosts); + if (serverUrlList == null || serverUrlList.size() ==0){ + Log.i(TAG, "init IMLibClientBootstrap error,ims hosts is null"); + return; + } + isActive = true; + Log.i(TAG, "init IMLibClientBootstrap ,server="+hosts); + if (null != imsClient){ + imsClient.close(); + } + //鍒濆鍖朓MSClientInteface + imsClient = IMSClientFactory.getIMSClient(); + updateAppStatus(appStatus); + imsClient.init(serverUrlList,new IMSEventListener(userId,token),new IMSConnectStatusListener()); + } + + } + + +public boolean isActive(){ + return isActive; +} + + public void updateAppStatus(int appStatus) { + if (imsClient == null){ + return; + } + imsClient.setAppStatus(appStatus); + } + + private Vector<String> convertHosts(String hosts) { + if (hosts != null && hosts.length() > 0) { + JsonArray hostArray = JsonParser.parseString(hosts).getAsJsonArray(); + if (null != hostArray && hostArray.size() > 0) { + Vector<String> serverUrlList = new Vector<String>(); + JsonObject host; + for (int i = 0; i < hostArray.size(); i++) { + host = JsonParser.parseString(hostArray.get(i).toString()).getAsJsonObject(); + serverUrlList.add(host.get("host") + " " + + host.get("port")); + } + return serverUrlList; + } + + + } + return null; + } + + public void sendMessage(String message){ + if (isActive){ + imsClient.sendMsg(message); + } + } +} diff --git a/im_lib/src/main/java/com/anyun/im_lib/IMSClientFactory.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSClientFactory.java similarity index 90% rename from im_lib/src/main/java/com/anyun/im_lib/IMSClientFactory.java rename to app/src/main/java/safeluck/drive/evaluation/im/IMSClientFactory.java index d3f69dc..b196d4e 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/IMSClientFactory.java +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSClientFactory.java @@ -1,4 +1,4 @@ -package com.anyun.im_lib; +package safeluck.drive.evaluation.im; import com.anyun.im_lib.interf.IMSClientInteface; import com.anyun.im_lib.netty.NettyTcpClient; diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMSConnectStatusListener.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSConnectStatusListener.java new file mode 100644 index 0000000..00d813d --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSConnectStatusListener.java @@ -0,0 +1,26 @@ +package safeluck.drive.evaluation.im; + +import com.anyun.im_lib.listener.IMSConnectStatusCallback; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:30:33 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public class IMSConnectStatusListener implements IMSConnectStatusCallback { + @Override + public void onConnecting() { + + } + + @Override + public void onConnected() { + + } + + @Override + public void onConnectFailed() { + + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java new file mode 100644 index 0000000..b35dd00 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java @@ -0,0 +1,65 @@ +package safeluck.drive.evaluation.im; + +import com.anyun.im_lib.listener.OnEventListener; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:12:40 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public class IMSEventListener implements OnEventListener { + + private String userId; + private String token; + + public IMSEventListener(String userId, String token) { + this.userId = userId; + this.token = token; + } + + @Override + public void dispatchMsg(Object message) { + MessageProcessor.getInstance().receiveMsg((String)message); + } + + @Override + public boolean isNetWorkAvailable() { + return false; + } + + @Override + public int getConnectTimeout() { + return 0; + } + + @Override + public int getForegroundHeartbeatInterval() { + return 0; + } + + @Override + public int getBackgroundHeartbeatInterval() { + return 0; + } + + @Override + public int getServerSentReportMsgType() { + return 0; + } + + @Override + public int getResendCount() { + return 0; + } + + @Override + public int getResendInterval() { + return 0; + } + + @Override + public int getReConnectInterval() { + return 0; + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java b/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java new file mode 100644 index 0000000..edc2c6c --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java @@ -0,0 +1,12 @@ +package safeluck.drive.evaluation.im; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:14:57 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public interface IMessageProcessor { + void receiveMsg(String message); + void sendMessage(String msg); +} diff --git a/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java b/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java new file mode 100644 index 0000000..2e2e17f --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java @@ -0,0 +1,43 @@ +package safeluck.drive.evaluation.im; + +import android.util.Log; + +import safeluck.drive.evaluation.util.CThreadPoolExecutor; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:14:33 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public class MessageProcessor implements IMessageProcessor { + + private static final String TAG = MessageProcessor.class.getSimpleName(); + + private MessageProcessor(){ + + } + + private static class MessageProcessorInstance{ + private static final IMessageProcessor INSTANCE = new MessageProcessor(); + } + + public static IMessageProcessor getInstance(){ + return MessageProcessorInstance.INSTANCE; + } + + @Override + public void receiveMsg(String message) { + Log.i(TAG, "receiveMsg: "+message); + } + + @Override + public void sendMessage(final String msg) { + CThreadPoolExecutor.runInBackground(new Runnable() { + @Override + public void run() { + IMSClientBootstrap.getInstance().sendMessage(msg); + } + }); + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/im/handler/AbstractMessageHandler.java b/app/src/main/java/safeluck/drive/evaluation/im/handler/AbstractMessageHandler.java new file mode 100644 index 0000000..4755ea6 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/handler/AbstractMessageHandler.java @@ -0,0 +1,15 @@ +package safeluck.drive.evaluation.im.handler; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:09:02 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public abstract class AbstractMessageHandler implements IMessageHandler { + @Override + public void execute(String msg) { + action(msg); + } + protected abstract void action(String msg); +} diff --git a/app/src/main/java/safeluck/drive/evaluation/im/handler/IMessageHandler.java b/app/src/main/java/safeluck/drive/evaluation/im/handler/IMessageHandler.java new file mode 100644 index 0000000..020efff --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/im/handler/IMessageHandler.java @@ -0,0 +1,11 @@ +package safeluck.drive.evaluation.im.handler; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:04:29 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public interface IMessageHandler { + void execute(String msg); +} diff --git a/app/src/main/java/safeluck/drive/evaluation/util/CThreadPoolExecutor.java b/app/src/main/java/safeluck/drive/evaluation/util/CThreadPoolExecutor.java new file mode 100644 index 0000000..5600b3c --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/util/CThreadPoolExecutor.java @@ -0,0 +1,332 @@ +package safeluck.drive.evaluation.util; + + +import android.os.Handler; +import android.os.Looper; +import android.util.Log; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * MyApplication2 + * Created by lzw on 2019/12/12. 16:22:49 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + * + * * + * * <p>@ClassName: CThreadPoolExecutor.java</p> + * * <b> + * * <p>@Description: 鑷畾涔夊浐瀹氬ぇ灏忕殑绾跨▼姹� + * * 姣忔鎻愪氦涓�涓换鍔″氨鍒涘缓涓�涓嚎绋嬶紝鐩村埌绾跨▼杈惧埌绾跨▼姹犵殑鏈�澶уぇ灏忋�� + * * 绾跨▼姹犵殑澶у皬涓�鏃﹁揪鍒版渶澶у�煎氨浼氫繚鎸佷笉鍙橈紝濡傛灉鏌愪釜绾跨▼鍥犱负鎵ц寮傚父鑰岀粨鏉燂紝閭d箞绾跨▼姹犱細琛ュ厖涓�涓柊绾跨▼銆� + * * <p> + * * 鍚堢悊鍒╃敤绾跨▼姹犺兘澶熷甫鏉ヤ笁涓ソ澶勶細 + * * 绗竴锛氶檷浣庤祫婧愭秷鑰椼�傞�氳繃閲嶅鍒╃敤宸插垱寤虹殑绾跨▼闄嶄綆绾跨▼鍒涘缓鍜岄攢姣侀�犳垚鐨勬秷鑰椼�� + * * 绗簩锛氭彁楂樺搷搴旈�熷害銆傚綋浠诲姟鍒拌揪鏃讹紝浠诲姟鍙互涓嶉渶瑕佺瓑鍒扮嚎绋嬪垱寤哄氨鑳界珛鍗虫墽琛屻�� + * * 绗笁锛氭彁楂樼嚎绋嬬殑鍙鐞嗘�с�傜嚎绋嬫槸绋�缂鸿祫婧愶紝濡傛灉鏃犻檺鍒剁殑鍒涘缓锛屼笉浠呬細娑堣�楃郴缁熻祫婧愶紝杩樹細闄嶄綆绯荤粺鐨勭ǔ瀹氭�э紝浣跨敤绾跨▼姹犲彲浠ヨ繘琛岀粺涓�鐨勫垎閰嶏紝璋冧紭鍜岀洃鎺с�� + * * 鎴戜滑鍙互閫氳繃ThreadPoolExecutor鏉ュ垱寤轰竴涓嚎绋嬫睜锛� + * * new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, milliseconds,runnableTaskQueue, handler); + * * <p> + * * corePoolSize锛堢嚎绋嬫睜鐨勫熀鏈ぇ灏忥級锛� + * * 褰撴彁浜や竴涓换鍔″埌绾跨▼姹犳椂锛岀嚎绋嬫睜浼氬垱寤轰竴涓嚎绋嬫潵鎵ц浠诲姟锛� + * * 鍗充娇鍏朵粬绌洪棽鐨勫熀鏈嚎绋嬭兘澶熸墽琛屾柊浠诲姟涔熶細鍒涘缓绾跨▼锛岀瓑鍒伴渶瑕佹墽琛岀殑浠诲姟鏁板ぇ浜庣嚎绋嬫睜鍩烘湰澶у皬鏃跺氨涓嶅啀鍒涘缓銆� + * * 濡傛灉璋冪敤浜嗙嚎绋嬫睜鐨刾restartAllCoreThreads鏂规硶锛岀嚎绋嬫睜浼氭彁鍓嶅垱寤哄苟鍚姩鎵�鏈夊熀鏈嚎绋嬨�� + * * <p> + * * runnableTaskQueue锛堜换鍔¢槦鍒楋級锛氱敤浜庝繚瀛樼瓑寰呮墽琛岀殑浠诲姟鐨勯樆濉為槦鍒椼�� 鍙互閫夋嫨浠ヤ笅鍑犱釜闃诲闃熷垪銆� + * * ArrayBlockingQueue锛氭槸涓�涓熀浜庢暟缁勭粨鏋勭殑鏈夌晫闃诲闃熷垪锛屾闃熷垪鎸� FIFO锛堝厛杩涘厛鍑猴級鍘熷垯瀵瑰厓绱犺繘琛屾帓搴忋�� + * * <p> + * * LinkedBlockingQueue锛氫竴涓熀浜庨摼琛ㄧ粨鏋勭殑闃诲闃熷垪锛屾闃熷垪鎸塅IFO 锛堝厛杩涘厛鍑猴級 鎺掑簭鍏冪礌锛屽悶鍚愰噺閫氬父瑕侀珮浜嶢rrayBlockingQueue銆� + * * 闈欐�佸伐鍘傛柟娉旹xecutors.newFixedThreadPool()浣跨敤浜嗚繖涓槦鍒椼�� + * * <p> + * * SynchronousQueue锛氫竴涓笉瀛樺偍鍏冪礌鐨勯樆濉為槦鍒椼�傛瘡涓彃鍏ユ搷浣滃繀椤荤瓑鍒板彟涓�涓嚎绋嬭皟鐢ㄧЩ闄ゆ搷浣滐紝鍚﹀垯鎻掑叆鎿嶄綔涓�鐩村浜庨樆濉炵姸鎬侊紝 + * * 鍚炲悙閲忛�氬父瑕侀珮浜嶭inkedBlockingQueue锛岄潤鎬佸伐鍘傛柟娉旹xecutors.newCachedThreadPool浣跨敤浜嗚繖涓槦鍒椼�� + * * <p> + * * PriorityBlockingQueue锛氫竴涓叿鏈変紭鍏堢骇鐨勬棤闄愰樆濉為槦鍒椼�� + * * <p> + * * maximumPoolSize锛堢嚎绋嬫睜鏈�澶уぇ灏忥級锛� + * * 绾跨▼姹犲厑璁稿垱寤虹殑鏈�澶х嚎绋嬫暟銆傚鏋滈槦鍒楁弧浜嗭紝骞朵笖宸插垱寤虹殑绾跨▼鏁板皬浜庢渶澶х嚎绋嬫暟锛屽垯绾跨▼姹犱細鍐嶅垱寤烘柊鐨勭嚎绋嬫墽琛屼换鍔°�傚�煎緱娉ㄦ剰鐨勬槸濡傛灉浣跨敤浜嗘棤鐣岀殑浠诲姟闃熷垪杩欎釜鍙傛暟灏辨病浠�涔堟晥鏋溿�� + * * <p> + * * ThreadFactory锛� + * * 鐢ㄤ簬璁剧疆鍒涘缓绾跨▼鐨勫伐鍘傦紝鍙互閫氳繃绾跨▼宸ュ巶缁欐瘡涓垱寤哄嚭鏉ョ殑绾跨▼璁剧疆鏇存湁鎰忎箟鐨勫悕瀛椼�� + * * <p> + * * RejectedExecutionHandler锛堥ケ鍜岀瓥鐣ワ級锛氬綋闃熷垪鍜岀嚎绋嬫睜閮芥弧浜嗭紝璇存槑绾跨▼姹犲浜庨ケ鍜岀姸鎬侊紝閭d箞蹇呴』閲囧彇涓�绉嶇瓥鐣ュ鐞嗘彁浜ょ殑鏂颁换鍔°�� + * * 杩欎釜绛栫暐榛樿鎯呭喌涓嬫槸AbortPolicy锛岃〃绀烘棤娉曞鐞嗘柊浠诲姟鏃舵姏鍑哄紓甯搞�備互涓嬫槸JDK1.5鎻愪緵鐨勫洓绉嶇瓥鐣ャ�� + * * AbortPolicy锛氱洿鎺ユ姏鍑哄紓甯搞�� + * * CallerRunsPolicy锛氬彧鐢ㄨ皟鐢ㄨ�呮墍鍦ㄧ嚎绋嬫潵杩愯浠诲姟銆� + * * DiscardOldestPolicy锛氫涪寮冮槦鍒楅噷鏈�杩戠殑涓�涓换鍔★紝骞舵墽琛屽綋鍓嶄换鍔°�� + * * DiscardPolicy锛氫笉澶勭悊锛屼涪寮冩帀銆� + * * 褰撶劧涔熷彲浠ユ牴鎹簲鐢ㄥ満鏅渶瑕佹潵瀹炵幇RejectedExecutionHandler鎺ュ彛鑷畾涔夌瓥鐣ャ�傚璁板綍鏃ュ織鎴栨寔涔呭寲涓嶈兘澶勭悊鐨勪换鍔°�� + * * <p> + * * keepAliveTime锛堢嚎绋嬫椿鍔ㄤ繚鎸佹椂闂达級锛氱嚎绋嬫睜鐨勫伐浣滅嚎绋嬬┖闂插悗锛屼繚鎸佸瓨娲荤殑鏃堕棿銆� + * * 鎵�浠ュ鏋滀换鍔″緢澶氾紝骞朵笖姣忎釜浠诲姟鎵ц鐨勬椂闂存瘮杈冪煭锛屽彲浠ヨ皟澶ц繖涓椂闂达紝鎻愰珮绾跨▼鐨勫埄鐢ㄧ巼銆� + * * <p> + * * TimeUnit锛堢嚎绋嬫椿鍔ㄤ繚鎸佹椂闂寸殑鍗曚綅锛夛細鍙�夌殑鍗曚綅鏈夊ぉ锛圖AYS锛夛紝灏忔椂锛圚OURS锛夛紝鍒嗛挓锛圡INUTES锛夛紝 + * * 姣(MILLISECONDS)锛屽井绉�(MICROSECONDS, 鍗冨垎涔嬩竴姣)鍜屾寰(NANOSECONDS, 鍗冨垎涔嬩竴寰)銆�</p> + * * </b> + * * <p>@author: FreddyChen</p> + * * <p>@date: 2019/2/3 15:35</p> + * * <p>@email: chenshichao@outlook.com</p> + * * + * * @see http://www.infoq.com/cn/articles/java-threadPool + * + */ +public class CThreadPoolExecutor { + private static final String TAG = CThreadPoolExecutor.class.getSimpleName(); + private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();// CPU涓暟 + // private static final int CORE_POOL_SIZE = CPU_COUNT + 1;// 绾跨▼姹犱腑鏍稿績绾跨▼鐨勬暟閲� +// private static final int MAXIMUM_POOL_SIZE = 2 * CPU_COUNT + 1;// 绾跨▼姹犱腑鏈�澶х嚎绋嬫暟閲� + private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));// 绾跨▼姹犱腑鏍稿績绾跨▼鐨勬暟閲� + private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;// 绾跨▼姹犱腑鏈�澶х嚎绋嬫暟閲� + private static final long KEEP_ALIVE_TIME = 30L;// 闈炴牳蹇冪嚎绋嬬殑瓒呮椂鏃堕暱锛屽綋绯荤粺涓潪鏍稿績绾跨▼闂茬疆鏃堕棿瓒呰繃keepAliveTime涔嬪悗锛屽垯浼氳鍥炴敹銆傚鏋淭hreadPoolExecutor鐨刟llowCoreThreadTimeOut灞炴�ц缃负true锛屽垯璇ュ弬鏁颁篃琛ㄧず鏍稿績绾跨▼鐨勮秴鏃舵椂闀� + private static final int WAIT_COUNT = 128; // 鏈�澶氭帓闃熶釜鏁帮紝杩欓噷鎺у埗绾跨▼鍒涘缓鐨勯鐜� + + private static ThreadPoolExecutor pool = createThreadPoolExecutor(); + + private static ThreadPoolExecutor createThreadPoolExecutor() { + if (pool == null) { + pool = new ThreadPoolExecutor( + CORE_POOL_SIZE, + MAXIMUM_POOL_SIZE, + KEEP_ALIVE_TIME, + TimeUnit.SECONDS, + new LinkedBlockingQueue<Runnable>(WAIT_COUNT), + new CThreadFactory("CThreadPool", Thread.NORM_PRIORITY - 2), + new CHandlerException()); + } + + return pool; + } + + public static class CThreadFactory implements ThreadFactory { + private AtomicInteger counter = new AtomicInteger(1); + private String prefix = ""; + private int priority = Thread.NORM_PRIORITY; + + public CThreadFactory(String prefix, int priority) { + this.prefix = prefix; + this.priority = priority; + } + + public CThreadFactory(String prefix) { + this.prefix = prefix; + } + + public Thread newThread(Runnable r) { + Thread executor = new Thread(r, prefix + " #" + counter.getAndIncrement()); + executor.setDaemon(true); + executor.setPriority(priority); + return executor; + } + } + + /** + * 鎶涘純褰撳墠鐨勪换鍔� + */ + private static class CHandlerException extends ThreadPoolExecutor.AbortPolicy { + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { + Log.d(TAG, "rejectedExecution:" + r); + Log.e(TAG, logAllThreadStackTrace().toString()); + // Tips.showForce("浠诲姟琚嫆缁�", 5000); + if (!pool.isShutdown()) { + pool.shutdown(); + pool = null; + } + + pool = createThreadPoolExecutor(); + } + } + + private static ExecutorService jobsForUI = Executors.newFixedThreadPool( + CORE_POOL_SIZE, new CThreadFactory("CJobsForUI", Thread.NORM_PRIORITY - 1)); + + /** + * 鍚姩涓�涓秷鑰楃嚎绋嬶紝甯搁┗鍚庡彴 + * + * @param r + */ + public static void startConsumer(final Runnable r, final String name) { + runInBackground(new Runnable() { + public void run() { + new CThreadFactory(name, Thread.NORM_PRIORITY - 3).newThread(r).start(); + } + }); + } + + /** + * 鎻愪氦鍒板叾浠栫嚎绋嬪幓璺戯紝闇�瑕佸彇鏁版嵁鐨勬椂鍊欎細绛夊緟浠诲姟瀹屾垚鍐嶇户缁� + * + * @param task + * @return + */ + public static <T> Future<T> submitTask(Callable<T> task) { + return jobsForUI.submit(task); + } + + /** + * 寮哄埗娓呯悊浠诲姟 + * + * @param task + * @return + */ + public static <T> void cancelTask(Future<T> task) { + if (task != null) { + task.cancel(true); + } + } + + /** + * 浠� Future 涓幏鍙栧�硷紝濡傛灉鍙戠敓寮傚父锛屾墦鏃ュ織 + * + * @param future + * @param tag + * @param name + * @return + */ + public static <T> T getFromTask(Future<T> future, String tag, String name) { + try { + return future.get(); + } catch (Exception e) { + Log.e(tag, (name != null ? name + ": " : "") + e.toString()); + } + return null; + } + + public static void runInBackground(Runnable runnable) { + if (pool == null) { + createThreadPoolExecutor(); + } + + pool.execute(runnable); + // Future future = pool.submit(runnable); + // try { + // future.get(); + // } catch (InterruptedException e) { + // e.printStackTrace(); + // } catch (ExecutionException e) { + // e.printStackTrace(); + // } + } + + private static Thread mainThread; + private static Handler mainHandler; + + static { + Looper mainLooper = Looper.getMainLooper(); + mainThread = mainLooper.getThread(); + mainHandler = new Handler(mainLooper); + } + + public static boolean isOnMainThread() { + return mainThread == Thread.currentThread(); + } + + public static void runOnMainThread(Runnable r) { + if (isOnMainThread()) { + r.run(); + } else { + mainHandler.post(r); + } + } + + public static void runOnMainThread(Runnable r, long delayMillis) { + if (delayMillis <= 0) { + runOnMainThread(r); + } else { + mainHandler.postDelayed(r, delayMillis); + } + } + + // 鐢ㄤ簬璁板綍鍚庡彴绛夊緟鐨凴unnable锛岀涓�涓弬鏁板闈㈢殑Runnable锛岀浜屼釜鍙傛暟鏄瓑寰呬腑鐨凴unnable + private static HashMap<Runnable, Runnable> mapToMainHandler = new HashMap<Runnable, Runnable>(); + + public static void runInBackground(final Runnable runnable, long delayMillis) { + if (delayMillis <= 0) { + runInBackground(runnable); + } else { + Runnable mainRunnable = new Runnable() { + + @Override + public void run() { + mapToMainHandler.remove(runnable); + pool.execute(runnable); + } + }; + + mapToMainHandler.put(runnable, mainRunnable); + mainHandler.postDelayed(mainRunnable, delayMillis); + } + } + + /** + * 瀵箁unOnMainThread鐨勶紝绉婚櫎Runnable + * + * @param r + */ + public static void removeCallbackOnMainThread(Runnable r) { + mainHandler.removeCallbacks(r); + } + + public static void removeCallbackInBackground(Runnable runnable) { + Runnable mainRunnable = mapToMainHandler.get(runnable); + if (mainRunnable != null) { + mainHandler.removeCallbacks(mainRunnable); + } + } + + public static void logStatus() { + StringBuilder sb = new StringBuilder(); + sb.append("getActiveCount"); + sb.append(pool.getActiveCount()); + sb.append("\ngetTaskCount"); + sb.append(pool.getTaskCount()); + sb.append("\ngetCompletedTaskCount"); + sb.append(pool.getCompletedTaskCount()); + Log.d(TAG, sb.toString()); + } + + public static StringBuilder logAllThreadStackTrace() { + StringBuilder builder = new StringBuilder(); + Map<Thread, StackTraceElement[]> liveThreads = Thread.getAllStackTraces(); + for (Iterator<Thread> i = liveThreads.keySet().iterator(); i.hasNext(); ) { + Thread key = i.next(); + builder.append("Thread ").append(key.getName()) + .append("\n"); + StackTraceElement[] trace = liveThreads.get(key); + for (int j = 0; j < trace.length; j++) { + builder.append("\tat ").append(trace[j]).append("\n"); + } + } + return builder; + } + + public static void main(String[] args) { + for (int i = 0; i < 10000; i++) { + final int index = i; + System.out.println("index=" + index); + CThreadPoolExecutor.runInBackground(new Runnable() { + @Override + public void run() { + System.out.println("姝e湪杩愯绗琜" + (index + 1) + "]涓嚎绋�."); + } + }); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} diff --git a/im_lib/src/main/java/com/anyun/im_lib/im/IMSClientBootstrap.java b/im_lib/src/main/java/com/anyun/im_lib/im/IMSClientBootstrap.java deleted file mode 100644 index d4d001c..0000000 --- a/im_lib/src/main/java/com/anyun/im_lib/im/IMSClientBootstrap.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.anyun.im_lib.im; - -import android.util.Log; - -import com.anyun.im_lib.IMSClientFactory; -import com.anyun.im_lib.interf.IMSClientInteface; - -import java.util.Vector; - -import static android.content.ContentValues.TAG; - -/** - * MyApplication2 - * Created by lzw on 2019/12/2. 11:56:55 - * 閭锛�632393724@qq.com - * All Rights Saved! Chongqing AnYun Tech co. LTD - */ -public class IMSClientBootstrap { - - private static final IMSClientBootstrap INSTANCE= new IMSClientBootstrap(); - private IMSClientInteface imsClient; - - /**鏍囪IMSClientBootstrap鏄惁宸茬粡鍒濆鍖�**/ - private boolean isAlive; - - /** - * - * @param userId - * @param token - * @param hosts - * @param appStatus - */ - public synchronized void init(String userId,String token,String hosts,int appStatus){ - if (!isAlive){ - Vector<String> serverUrlList = convertHosts(hosts); - if (serverUrlList == null || serverUrlList.size() ==0){ - Log.i(TAG, "init IMLibClientBootstrap error,ims hosts is null"); - return; - } - isAlive = true; - Log.i(TAG, "init IMLibClientBootstrap ,server="+hosts); - } - if (null != imsClient){ - imsClient.close(); - } - //鍒濆鍖朓MSClientInteface - imsClient = IMSClientFactory.getIMSClient(); - updateAppStatus(appStatus); -// imsClient.init(serverUrlList,new OnEventListener(userId,token),new IMSConnectStatusCallback()); - } - - - - private void updateAppStatus(int appStatus) { - } - - private Vector<String> convertHosts(String hosts) { - //TODO - return null; - } -} -- Gitblit v1.8.0