Retry机制_python retry

(1) 2024-08-26 16:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
Retry机制_python 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); } } }

方法二:

 private int count = 0; private Timer m_timer = null; private TimerTask m_task = null; //retry 3次,每次间隔8s private void startTimer(){ m_timer = new Timer(); m_task = new TimerTask() { @Override public void run() { if(isUpdating()){ ++count; if(count == 3){ Log.d(TAG, "startTimer >>> count=3!" ); //do something removeTimer(); } }else{ if(OPEN_MESSAGE_SUCCESS == openMessage("message")){ Log.d(TAG, "startTimer >>> message has opened!" ); } removeTimer(); } } }; if (m_task != null && m_timer != null) { m_timer.schedule(m_task, 8000, 8000); } } private void removeTimer(){ if (m_task != null) { m_task.cancel(); m_task = null; } if (m_timer != null) { m_timer.cancel(); m_timer = null; } if(count != 0){ count = 0; } } //使用此retry时,只需调用startTimer()即可

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复