- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
每次我的请求失败时,我都试图通过使用 handler.postDelayed(...)
安排一个线程来实现指数退避以重试失败的 http 调用。问题是我是从 IntentService 执行此操作的,该服务在安排第一个线程后死亡,因此处理程序无法调用自身。我收到以下错误:
java.lang.IllegalStateException: Handler (android.os.Handler) {2f31b19b} sending message to a Handler on a dead thread
我的 IntentService 类:
@Override
protected void onHandleIntent(Intent intent) {
......
Handler handler = new Handler();
HttpRunnable httpRunnable = new HttpRunnable(info, handler);
handler.postDelayed(httpRunnable, 0);
}
我的自定义 Runnable:
public class HttpRunnable implements Runnable {
private String info;
private static final String TAG = "HttpRunnable";
Handler handler = null;
int maxTries = 10;
int retryCount = 0;
int retryDelay = 1000; // Set the first delay here which will increase exponentially with each retry
public HttpRunnable(String info, Handler handler) {
this.info = info;
this.handler = handler;
}
@Override
public void run() {
try {
// Call my class which takes care of the http call
ApiBridge.getInstance().makeHttpCall(info);
} catch (Exception e) {
Log.d(TAG, e.toString());
if (maxTries > retryCount) {
Log.d(TAG,"%nRetrying in " + retryDelay / 1000 + " seconds");
retryCount++;
handler.postDelayed(this, retryDelay);
retryDelay = retryDelay * 2;
}
}
}
}
有没有办法让我的处理程序保持 Activity 状态?使用指数退避来安排我的 HTTP 重试的最佳/最干净的方法是什么?
最佳答案
使用 IntentService
的主要优点是它可以在 onHandleIntent(Intent intent)
方法中为您处理所有后台线程。在这种情况下,您没有理由自己管理处理程序。
您可以使用 AlarmManager
来安排向您的服务发送 Intent 。您将在要交付的 Intent 中保留重试信息。
我是这样想的:
public class YourService extends IntentService {
private static final String EXTRA_FAILED_ATTEMPTS = "com.your.package.EXTRA_FAILED_ATTEMPTS";
private static final String EXTRA_LAST_DELAY = "com.your.package.EXTRA_LAST_DELAY";
private static final int MAX_RETRIES = 10;
private static final int RETRY_DELAY = 1000;
public YourService() {
super("YourService");
}
@Override
protected final void onHandleIntent(Intent intent) {
// Your other code obtaining your info string.
try {
// Make your http call.
ApiBridge.getInstance().makeHttpCall(info);
} catch (Exception e) {
// Get the number of previously failed attempts, and add one.
int failedAttempts = intent.getIntExtra(EXTRA_FAILED_ATTEMPTS, 0) + 1;
// if we have failed less than the max retries, reschedule the intent
if (failedAttempts < MAX_RETRIES) {
// calculate the next delay
int lastDelay = intent.getIntExtra(EXTRA_LAST_DELAY, 0);
int thisDelay;
if (lastDelay == 0) {
thisDelay = RETRY_DELAY;
} else {
thisDelay = lastDelay * 2;
}
// update the intent with the latest retry info
intent.putExtra(EXTRA_FAILED_ATTEMPTS, failedAttempts);
intent.putExtra(EXTRA_LAST_DELAY, thisDelay);
// get the alarm manager
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// make the pending intent
PendingIntent pendingIntent = PendingIntent
.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// schedule the intent for future delivery
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + thisDelay, pendingIntent);
}
}
}
}
这只是让您正在使用的 IntentService 在后台处理调用,然后安排在每次失败时重新发送 Intent,向其添加重试次数和重试时长等额外信息上次重试延迟是。
注意:如果您尝试向此服务发送多个 Intent ,但不止一个失败并且必须使用 AlarmManager
重新安排,如果 Intent 被认为相等,则只会传送最新的 Intent 到 Intent.filterEquals(Intent intent)
。如果您的 Intent 除了附加到它的额外内容之外是相同的,这将是一个问题,并且您必须在创建您的 PendingIntent
时为每个要重新安排的 Intent 使用唯一的 requestCode。沿着这些线的东西:
int requestCode = getNextRequestCode();
PendingIntent pendingIntent = PendingIntent
.getService(getApplicationContext(), requestCode, intent, 0);
我想您可以使用您的共享首选项来存储一个请求代码,每次您必须安排重试时该代码都会递增。
关于java - 从 IntentService 调度递归处理程序以重试 http 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650069/
我有一个带有一些功能的perl对象。每个功能从主程序中调用一次。我想并行运行某些功能以节省时间。由于某些功能取决于先前功能的结果,因此我无法将它们全部一起运行。 我想到了这样的事情: 对于每个函数,保
首先,我的代码在这里: import schedule # see https://github.com/dbader/schedule import crawler def job(): p
从 11 月 1 日开始,我必须使用quartz调度程序每4个月安排一次任务。我使用 cronExpression 来实现同样的目的。但 cronExpression 每年都会重置。所以我的任务将在
我有以下代码块,它调用两个请求,但略有延迟。 final ActorRef actor1 = getContext().actorOf( ActorClass.prop
考虑到 Linux 的情况,我们为每个用户堆栈都有一个内核堆栈,据我所知,每当发生上下文切换时,我们都会切换到当前进程的内核模式。 这里我们保存当前进程的当前状态,寄存器,程序数据等,然后调度器(不确
我有将东西移植到 OpenBSD 的奇怪爱好。我知道它有 pthreads 问题,但在 2013 年 5 月发布版本之前我不会升级。我使用的是 5.0,我对 pthreads 还很陌生。我已经学习了
给定一组任务: T1(20,100) T2(30,250) T3(100,400) (execution time, deadline=peroid) 现在我想将截止日期限制为 Di = f * Pi
使用 Django 开发一个小型日程安排 Web 应用程序,在该应用程序中,人们被分配特定的时间与他们的上级会面。员工存储为模型,与表示时间范围和他们有空的星期几的模型具有 OneToMany 关系。
我想了解贪婪算法调度问题的工作原理。 所以我一直在阅读和谷歌搜索一段时间,因为我无法理解贪心算法调度问题。 我们有 n 个作业要安排在单个资源上。作业 (i) 有一个请求的开始时间 s(i) 和结束时
这是流行的 El Goog 问题的变体。 考虑以下调度问题:有 n 个作业,i = 1..n。有 1 台 super 计算机和无限的 PC。每个作业都需要先经过 super 计算机的预处理,然后再在P
假设我有一个需要运行多次的蜘蛛 class My_spider(Scrapy.spider): #spider def 我想做这样的事 while True: runner = Cra
我已将 podAntiAffinity 添加到我的 DeploymentConfig 模板中。 但是,pod 被安排在我预计会被规则排除的节点上。 我如何查看 kubernetes 调度程序的日志以了
我已经使用 React - Redux - Typescript 堆栈有一段时间了,到目前为止我很喜欢它。但是,由于我对 Redux 很陌生,所以我一直在想这个特定的话题。 调度 Redux 操作(和
我想按照预定的计划(例如,周一至周五,美国东部时间晚上 9 点至 5 点)运行单个 Azure 实例以减少账单,并且想知道最好的方法是什么。 问题的两个部分: 能否使用服务管理 API [1] 按预定
假设最小模块安装(为了简单起见),Drupal 的 index.php 中两个顶级功能的核心“职责”是什么? ? drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); me
我正在尝试使用 Racket(以前称为 PLT Scheme)连接 URL 调度。我查看了教程和服务器文档。我不知道如何将请求路由到相同的 servlet。 具体例子: #lang 方案 (需要网络服
我想在 Airflow (v1.9.0) 上运行计划。 我的DAG需要在每个月底运行,但我不知道如何编写设置。 my_dag = DAG(dag_id=DAG_ID, cat
我正在尝试在“httpTrigger”类型函数的 function.json 中设置计划字段,但计时器功能似乎未运行。我的目标是拥有一个甚至可以在需要时进行调度和手动启动的功能,而不必仅为了调度而添加
我正在尝试制定每周、每月的 Airflow 计划,但不起作用。有人可以报告可能发生的情况吗?如果我每周、每月进行安排,它就会保持静止,就好像它被关闭一样。没有错误信息,只是不执行。我发送了一个代码示例
我希望每两周自动更新一次我的表格。我希望我的函数能够被 firebase 调用。 这可能吗? 我正在使用 Angular 2 Typescript 和 Firebase。 最佳答案 仅通过fireba
我是一名优秀的程序员,十分优秀!