26
2017
09

Retry机制

1.前言

在日常开发中,有时在执行某些操作时,可能当时条件不满足,未能执行,但是后续的一定时间内,条件会满足,这时我们需要继续执行操作。为了达到这种效果,我们就需要使用到retry机制,在后续的规定时间里,有间隔的去尝试执行操作。

2.具体流程

private Handler m_Handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_CLOSE_MESSAGE:
                closeMessage(msg.obj.toString());
                break;
            case MSG_OPEN_MESSAGE_SCOUTER_OFF:
                if (m_isMessageAllowedtoOpen) {
                    openRetryMessage(m_RetryRunable);
                }
                break;
            default:
                break;
            }
        };
};

//retry 6次,每次间隔5min
private OpenRetryMessageRunnable m_RetryRunable = new OpenRetryMessageRunnable("message", 5 * 60 * 1000, 6);

private void openRetryMessage(OpenRetryMessageRunnable runnable) {
        if(null != m_Handler){
            m_Handler.removeCallbacks(runnable);
            runnable.resetTask();
            m_Handler.post(runnable);
        }
}

private class OpenRetryMessageRunnable implements Runnable {
        private String m_MessageID;
        private int interval = 0;
        private int times = 0;
        private int defInterval=0;
        private int degTimes=0;

        public OpenRetryMessageRunnable(String messageID, int interval, int times) {
            m_MessageID = messageID;
            this.interval = interval;
            this.times = times;
            this.defInterval=interval;
            this.degTimes=times;
        }

        public void resetTask(){
            this.interval=defInterval;
            this.times = degTimes;
        }

        @Override
        public void run() {
            try {
                Log.d(TAG, "OpenRetryMessageRunnable:m_MessageID=" + m_MessageID);
                boolean openResult = false;
                IRemoteSysService m_Service = myApp.getInstance().getRemoteSysService();

                if (null == m_Service) {
                    Log.d(TAG, "OpenRetryMessage failed m_Service==null");
                } else if (!m_Service.isMessageOpened(m_MessageID)) {
                    if ("message".equals(m_MessageID)) {
                        CONNECTION_STATUS connectStatus = MyController.getInstance().getCurrentConnectionStatus();
                        Log.d(TAG, "getCurrentConnectionStatus:connectStatus=" + connectStatus);
                        if (CONNECTION_STATUS.DISCONNECTED == connectStatus) {
                            openResult = m_Service.openMessage(m_MessageID);
                            if (openResult) {
                                m_isMessageAllowedtoOpen = false;
                            }
                        } else {
                            openResult = true;// end runnable
                        }
                    } else {
                        openResult = m_Service.openMessage(m_MessageID);
                    }
                } else {
                    Log.d(TAG, "OpenRetryMessage failed m_MessageID=" + m_MessageID);
                }

                if (!openResult) {
                    if (times > 0) {
                        m_Handler.postDelayed(this, interval);
                        times--;
                    }
                }
            }catch (RemoteException e) {
                Log.d(TAG, "OpenRetryMessageRunnable exception,m_MessageID=" + m_MessageID);
            }
        }
}
上一篇:工作随笔---WiFi开发(4)---WIFISavedList类 下一篇:数组的拷贝