- Android開發藝術探索
- 任玉剛
- 2432字
- 2019-01-10 17:37:38
2.5 Binder連接池
上面我們介紹了不同的IPC方式,我們知道,不同的IPC方式有不同的特點和適用場景,當然這個問題會在2.6節進行介紹,在本節中要再次介紹一下ADIL,原因是AIDL是一種最常用的進程間通信方式,是日常開發中涉及進程間通信時的首選,所以我們需要額外強調一下它。
如何使用AIDL在上面的一節中已經進行了介紹,這里在回顧一下大致流程:首先創建一個Service和一個AIDL接口,接著創建一個類繼承自AIDL接口中的Stub類并實現Stub中的抽象方法,在Service的onBind方法中返回這個類的對象,然后客戶端就可以綁定服務端Service,建立連接后就可以訪問遠程服務端的方法了。
上述過程就是典型的AIDL的使用流程。這本來也沒什么問題,但是現在考慮一種情況:公司的項目越來越龐大了,現在有10個不同的業務模塊都需要使用AIDL來進行進程間通信,那我們該怎么處理呢?也許你會說:“就按照AIDL的實現方式一個個來吧”,這是可以的,如果用這種方法,首先我們需要創建10個Service,這好像有點多啊!如果有100個地方需要用到AIDL呢,先創建100個Service?到這里,讀者應該明白問題所在了。隨著AIDL數量的增加,我們不能無限制地增加Service, Service是四大組件之一,本身就是一種系統資源。而且太多的Service會使得我們的應用看起來很重量級,因為正在運行的Service可以在應用詳情頁看到,當我們的應用詳情顯示有10個服務正在運行時,這看起來并不是什么好事。針對上述問題,我們需要減少Service的數量,將所有的AIDL放在同一個Service中去管理。
在這種模式下,整個工作機制是這樣的:每個業務模塊創建自己的AIDL接口并實現此接口,這個時候不同業務模塊之間是不能有耦合的,所有實現細節我們要單獨開來,然后向服務端提供自己的唯一標識和其對應的Binder對象;對于服務端來說,只需要一個Service就可以了,服務端提供一個queryBinder接口,這個接口能夠根據業務模塊的特征來返回相應的Binder對象給它們,不同的業務模塊拿到所需的Binder對象后就可以進行遠程方法調用了。由此可見,Binder連接池的主要作用就是將每個業務模塊的Binder請求統一轉發到遠程Service中去執行,從而避免了重復創建Service的過程,它的工作原理如圖2-10所示。

圖2-10 Binder連接池的工作原理
通過上面的理論介紹,也許還有點不好理解,下面對Binder連接池的代碼實現做一下說明。首先,為了說明問題,我們提供了兩個AIDL接口(ISecurityCenter和ICompute)來模擬上面提到的多個業務模塊都要使用AIDL的情況,其中ISecurityCenter接口提供加解密功能,聲明如下:
interface ISecurityCenter { String encrypt(String content); String decrypt(String password); }
而ICompute接口提供計算加法的功能,聲明如下:
interface ICompute { int add(int a, int b); }
雖然說上面兩個接口的功能都比較簡單,但是用于分析Binder連接池的工作原理已經足夠了,讀者可以寫出更復雜的例子。接著看一下上面兩個AIDL接口的實現,也比較簡單,代碼如下:
public class SecurityCenterImpl extends ISecurityCenter.Stub { private static final char SECRET_CODE = '^'; @Override public String encrypt(String content) throws RemoteException { char[] chars = content.toCharArray(); for (int i = 0; i < chars.length; i++) { chars[i] ^= SECRET_CODE; } return new String(chars); } @Override public String decrypt(String password) throws RemoteException { return encrypt(password); } } public class ComputeImpl extends ICompute.Stub { @Override public int add(int a, int b) throws RemoteException { return a + b; } }
現在業務模塊的AIDL接口定義和實現都已經完成了,注意這里并沒有為每個模塊的AIDL單獨創建Service,接下來就是服務端和Binder連接池的工作了。
首先,為Binder連接池創建AIDL接口IBinderPool.aidl,代碼如下所示。
interface IBinderPool { /** * @param binderCode, the unique token of specific Binder<br/> * @return specific Binder who's token is binderCode. */ IBinder queryBinder(int binderCode); }
接著,為Binder連接池創建遠程Service并實現IBinderPool,下面是queryBinder的具體實現,可以看到請求轉發的實現方法,當Binder連接池連接上遠程服務時,會根據不同模塊的標識即binderCode返回不同的Binder對象,通過這個Binder對象所執行的操作全部發生在遠程服務端。
@Override public IBinder queryBinder(int binderCode) throws RemoteException { IBinder binder = null; switch (binderCode) { case BINDER_SECURITY_CENTER: { binder = new SecurityCenterImpl(); break; } case BINDER_COMPUTE: { binder = new ComputeImpl(); break; } default: break; } return binder; }
遠程Service的實現就比較簡單了,代碼如下所示。
public class BinderPoolService extends Service { private static final String TAG = "BinderPoolService"; private Binder mBinderPool = new BinderPool.BinderPoolImpl(); @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); return mBinderPool; } @Override public void onDestroy() { super.onDestroy(); } }
下面還剩下Binder連接池的具體實現,在它的內部首先它要去綁定遠程服務,綁定成功后,客戶端就可以通過它的queryBinder方法去獲取各自對應的Binder,拿到所需的Binder以后,不同業務模塊就可以進行各自的操作了,Binder連接池的代碼如下所示。
public class BinderPool { private static final String TAG = "BinderPool"; public static final int BINDER_NONE = -1; public static final int BINDER_COMPUTE = 0; public static final int BINDER_SECURITY_CENTER = 1; private Context mContext; private IBinderPool mBinderPool; private static volatile BinderPool sInstance; private CountDownLatch mConnectBinderPoolCountDownLatch; private BinderPool(Context context) { mContext = context.getApplicationContext(); connectBinderPoolService(); } public static BinderPool getInsance(Context context) { if (sInstance == null) { synchronized (BinderPool.class) { if (sInstance == null) { sInstance = new BinderPool(context); } } } return sInstance; } private synchronized void connectBinderPoolService() { mConnectBinderPoolCountDownLatch = new CountDownLatch(1); Intent service = new Intent(mContext, BinderPoolService.class); mContext.bindService(service, mBinderPoolConnection, Context.BIND_AUTO_CREATE); try { mConnectBinderPoolCountDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * query binder by binderCode from binder pool * * @param binderCode * the unique token of binder * @return binder who's token is binderCode<br> * return null when not found or BinderPoolService died. */ public IBinder queryBinder(int binderCode) { IBinder binder = null; try { if (mBinderPool ! = null) { binder = mBinderPool.queryBinder(binderCode); } } catch (RemoteException e) { e.printStackTrace(); } return binder; } private ServiceConnection mBinderPoolConnection = new ServiceConne- ction() { @Override public void onServiceDisconnected(ComponentName name) { // ignored. } @Override public void onServiceConnected(ComponentName name, IBinder service) { mBinderPool = IBinderPool.Stub.asInterface(service); try { mBinderPool.asBinder().linkToDeath(mBinderPoolDeathReci- pient, 0); } catch (RemoteException e) { e.printStackTrace(); } mConnectBinderPoolCountDownLatch.countDown(); } }; private IBinder.DeathRecipient mBinderPoolDeathRecipient = new IBinder. DeathRecipient() { @Override public void binderDied() { Log.w(TAG, "binder died."); mBinderPool.asBinder().unlinkToDeath(mBinderPoolDeathRecipient, 0); mBinderPool = null; connectBinderPoolService(); } }; public static class BinderPoolImpl extends IBinderPool.Stub { public BinderPoolImpl() { super(); } @Override public IBinder queryBinder(int binderCode) throws RemoteException { IBinder binder = null; switch (binderCode) { case BINDER_SECURITY_CENTER: { binder = new SecurityCenterImpl(); break; } case BINDER_COMPUTE: { binder = new ComputeImpl(); break; } default: break; } return binder; } } }
Binder連接池的具體實現就分析完了,它的好處是顯然易見的,針對上面的例子,我們只需要創建一個Service即可完成多個AIDL接口的工作,下面我們來驗證一下效果。新創建一個Activity,在線程中執行如下操作:
private void doWork() { BinderPool binderPool = BinderPool.getInsance(BinderPoolActivity. this); IBinder securityBinder = binderPool .queryBinder(BinderPool.BINDER_SECURITY_CENTER); ; mSecurityCenter = (ISecurityCenter) SecurityCenterImpl .asInterface(securityBinder); Log.d(TAG, "visit ISecurityCenter"); String msg = "helloworld-安卓"; System.out.println("content:" + msg); try { String password = mSecurityCenter.encrypt(msg); System.out.println("encrypt:" + password); System.out.println("decrypt:" + mSecurityCenter.decrypt(password)); } catch (RemoteException e) { e.printStackTrace(); } Log.d(TAG, "visit ICompute"); IBinder computeBinder = binderPool .queryBinder(BinderPool.BINDER_COMPUTE); mCompute = ComputeImpl.asInterface(computeBinder); try { System.out.println("3+5=" + mCompute.add(3, 5)); } catch (RemoteException e) { e.printStackTrace(); } }
在上述代碼中,我們先后調用了ISecurityCenter和ICompute這兩個AIDL接口中的方法,看一下log,很顯然,工作正常。
D/BinderPoolActivity(20270): visit ISecurityCenter I/System.out(20270): content:helloworld-安卓 I/System.out(20270): encrypt:6;221)1,2:s寗匍 I/System.out(20270): decrypt:helloworld-安卓 D/BinderPoolActivity(20270): visit ICompute I/System.out(20270): 3+5=8
這里需要額外說明一下,為什么要在線程中去執行呢?這是因為在Binder連接池的實現中,我們通過CountDownLatch將bindService這一異步操作轉換成了同步操作,這就意味著它有可能是耗時的,然后就是Binder方法的調用過程也可能是耗時的,因此不建議放在主線程去執行。注意到BinderPool是一個單例實現,因此在同一個進程中只會初始化一次,所以如果我們提前初始化BinderPool,那么可以優化程序的體驗,比如我們可以放在Application中提前對BinderPool進行初始化,雖然這不能保證當我們調用BinderPool時它一定是初始化好的,但是在大多數情況下,這種初始化工作(綁定遠程服務)的時間開銷(如果BinderPool沒有提前初始化完成的話)是可以接受的。另外,BinderPool中有斷線重連的機制,當遠程服務意外終止時,BinderPool會重新建立連接,這個時候如果業務模塊中的Binder調用出現了異常,也需要手動去重新獲取最新的Binder對象,這個是需要注意的。
有了BinderPool可以大大方便日常的開發工作,比如如果有一個新的業務模塊需要添加新的AIDL,那么在它實現了自己的AIDL接口后,只需要修改BinderPoolImpl中的queryBinder方法,給自己添加一個新的binderCode并返回對應的Binder對象即可,不需要做其他修改,也不需要創建新的Service。由此可見,BinderPool能夠極大地提高AIDL的開發效率,并且可以避免大量的Service創建,因此,建議在AIDL開發工作中引入BinderPool機制。