Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)



Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)

附录文章4简单介绍了如何启动一个后台线程任务,Android Priority Job Queue (Job Manager)有一个重要的机制在附录文章4还没有体现:线程任务失败后重试、重启线程任务。这个机制在某些开发场景中非常常用,假设从网络中请求数据,但不幸的是网络出现故障,导致在线程体onRun内的代码失败,问题是失败该如何处理?这个时候,常规的做法是自己增加一堆状态字或者返回值加以控制,但是这样无疑使得开发者陷入繁琐的线程状态机的维护中来。
Android Priority Job Queue (Job Manager)完整的解决上上述问题:容错重试回滚任务机制。具体体现在onRun和shouldReRunOnThrowable,如果一个线程任务在onRun里面抛出异常失败,那么将自动进入shouldReRunOnThrowable,然后在shouldReRunOnThrowable会决定是否重试RetryConstraint.RETRY,或者干脆放弃任务RetryConstraint.CANCEL,这里面的重试或者放弃逻辑策略由开发者决定,比如一般的,可以由重试次数决定线程是否还有必要执行,假设一个任务重试了3次仍然不成功,那么就放弃(返RetryConstraint.CANCEL)。一旦放弃后,代码将进入onCanel内,在这里面处理善后事宜,这个机制有些类似Java的try-catch机制。
在附录文章4的基础上,仅仅修改MyJob.java代码演示上述机制:

package zhangphil.app;

import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log; import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.Params;
import com.birbit.android.jobqueue.RetryConstraint; /**
* Created by Phil on 2016/10/9.
*/
public class MyJob extends Job { private String tag; public MyJob(String tag) {
super(new Params(500).requireNetwork().persist().groupBy(tag));
this.tag = tag;
Log.d(tag, "初始化");
} @Override
public void onAdded() {
Log.d(tag, "添加任务");
} //在这里面放置耗时的后台线程化任务
@Override
public void onRun() throws Throwable {
Log.d(tag, "开始运行..."); SystemClock.sleep(2000); //此处抛出异常后,jobqueue将立即自动进入shouldReRunOnThrowable
throw new Exception(); //Log.d(tag, "完成");
} @Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
// An error occurred in onRun.
// Return value determines whether this job should retry or cancel. You can further
// specify a backoff strategy or change the job's priority. You can also apply the
// delay to the whole group to preserve jobs' running order. Log.d(tag, "runCount: " + runCount + " maxRunCount: " + maxRunCount); //如果重试了3次仍未成功,那么就放弃执行任务
if (runCount == 3)
return RetryConstraint.CANCEL; //这样的返回将导致onRun方法的再次执行,重试,重试将导致代码再次进入onRun。同时,runCount自加1
return RetryConstraint.RETRY;
} //如果重试超过限定次数,将onCancel.
//如果用户主动放弃删掉这个任务,也一样进入onCancel
@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
Log.d(tag, "cancelReason:" + cancelReason);
}
}

代码运行后Logcat完整输出:

10-10 15:51:39.488 16856-16856/zhangphil.app D/任务1: 初始化
10-10 15:51:39.488 16856-16856/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] post message com.birbit.android.jobqueue.messaging.message.AddJobMessage@b1a3ff0
10-10 15:51:39.488 16856-16856/zhangphil.app D/Activity: performCreate Call Injection manager
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085899495109636
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085899495109636
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message com.birbit.android.jobqueue.messaging.message.AddJobMessage@b1a3ff0
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type ADD_JOB
10-10 15:51:39.488 16856-16856/zhangphil.app I/InjectionManager: dispatchOnViewCreated > Target : zhangphil.app.MainActivity isFragment :false
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: added job class: MyJob priority: 500 delay: 0 group : 任务1 persistent: true
10-10 15:51:39.528 16856-17353/zhangphil.app D/任务1: 添加任务
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: considering adding a new consumer. Should poke all waiting? false isRunning? true waiting workers? 0
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: check above load factor: totalCons:0 minCons:1 maxConsCount: 3, loadFactor 3 remainingJobs: 1 running holders: 0. isAbove:true
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: nothing has been poked. are we above load factor? true
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: adding another consumer
10-10 15:51:39.538 16856-16856/zhangphil.app D/SecWifiDisplayUtil: Metadata value : SecSettings2
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085899551798802
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085899551798802
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:39.548 16856-16856/zhangphil.app D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{8602777 I.E...... R.....ID 0,0-0,0}
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085899553556250
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] post message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: -9223372036854775808
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: add delayed message com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage@257dc4d at time -9223372036854775808
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? true
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085899555766615
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085899555766615
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] post message com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage@257dc4d
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage@257dc4d
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type CONSTRAINT_CHANGE
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: considering adding a new consumer. Should poke all waiting? true isRunning? true waiting workers? 0
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: check above load factor: totalCons:1 minCons:1 maxConsCount: 3, loadFactor 3 remainingJobs: 1 running holders: 0. isAbove:false
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: nothing has been poked. are we above load factor? false
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085899559063386
10-10 15:51:39.548 16856-17359/zhangphil.app D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085899559063386
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:39.558 16856-16856/zhangphil.app W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = zhangphil.app showWhenLocked = false
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085899571643802
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085899571643802
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085899571733438
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: running job JobHolder
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: running job MyJob
10-10 15:51:39.568 16856-17358/zhangphil.app D/任务1: 开始运行...
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false
10-10 15:51:39.588 16856-17359/zhangphil.app I/Adreno: QUALCOMM build : 0039a8c, I5bc0f577f9
Build Date : 08/03/16
OpenGL ES Shader Compiler Version: XE031.08.00.02
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.HB.1.1.1.C1.06.00.00.165.223
Remote Branch : NONE
Reconstruct Branch : NOTHING
10-10 15:51:39.588 16856-17359/zhangphil.app D/libEGL: eglInitialize EGLDisplay = 0x7f93bfa188
10-10 15:51:39.588 16856-17359/zhangphil.app I/OpenGLRenderer: Initialized EGL, version 1.4
10-10 15:51:39.618 16856-16856/zhangphil.app D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
10-10 15:51:39.628 16856-16856/zhangphil.app W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
10-10 15:51:39.668 16856-16856/zhangphil.app I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@87b6b1c time:249284264
10-10 15:51:41.568 16856-17358/zhangphil.app E/zhangphil job: error while executing job zhangphil.app.MyJob@9eb605a
java.lang.Exception
at zhangphil.app.MyJob.onRun(MyJob.java:38)
at com.birbit.android.jobqueue.Job.safeRun(Job.java:229)
at com.birbit.android.jobqueue.JobHolder.safeRun(JobHolder.java:132)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.handleRunJob(ConsumerManager.java:398)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.access$000(ConsumerManager.java:317)
at com.birbit.android.jobqueue.ConsumerManager$Consumer$2.handleMessage(ConsumerManager.java:345)
at com.birbit.android.jobqueue.messaging.SafeMessageQueue.consume(SafeMessageQueue.java:36)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.run(ConsumerManager.java:380)
at java.lang.Thread.run(Thread.java:818)
10-10 15:51:41.568 16856-17358/zhangphil.app D/任务1: runCount: 1 maxRunCount: 20
10-10 15:51:41.568 16856-17358/zhangphil.app D/zhangphil job: safeRunResult for zhangphil.app.MyJob@9eb605a : false. re run:true. cancelled: false
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] post message com.birbit.android.jobqueue.messaging.message.RunJobResultMessage@2fa28b
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085901581361354
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085901581436927
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085901581436927
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message com.birbit.android.jobqueue.messaging.message.RunJobResultMessage@2fa28b
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type RUN_JOB_RESULT
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] post message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: reinsert job result true
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085901603381718
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085901603381718
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085901615635572
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085901615635572
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085901615705104
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: running job JobHolder
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: running job MyJob
10-10 15:51:41.608 16856-17358/zhangphil.app D/任务1: 开始运行...
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: null
10-10 15:51:41.618 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false
10-10 15:51:43.608 16856-17358/zhangphil.app E/zhangphil job: error while executing job zhangphil.app.MyJob@1ef2826
java.lang.Exception
at zhangphil.app.MyJob.onRun(MyJob.java:38)
at com.birbit.android.jobqueue.Job.safeRun(Job.java:229)
at com.birbit.android.jobqueue.JobHolder.safeRun(JobHolder.java:132)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.handleRunJob(ConsumerManager.java:398)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.access$000(ConsumerManager.java:317)
at com.birbit.android.jobqueue.ConsumerManager$Consumer$2.handleMessage(ConsumerManager.java:345)
at com.birbit.android.jobqueue.messaging.SafeMessageQueue.consume(SafeMessageQueue.java:36)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.run(ConsumerManager.java:380)
at java.lang.Thread.run(Thread.java:818)
10-10 15:51:43.608 16856-17358/zhangphil.app D/任务1: runCount: 2 maxRunCount: 20
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: safeRunResult for zhangphil.app.MyJob@1ef2826 : false. re run:true. cancelled: false
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] post message com.birbit.android.jobqueue.messaging.message.RunJobResultMessage@2fa28b
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085903619928280
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085903620000572
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085903620000572
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message com.birbit.android.jobqueue.messaging.message.RunJobResultMessage@2fa28b
10-10 15:51:43.618 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type RUN_JOB_RESULT
10-10 15:51:43.618 16856-17358/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] post message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:43.618 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: reinsert job result true
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085903636682603
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085903636682603
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085903643573436
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085903643573436
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085903643628228
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: running job JobHolder
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: running job MyJob
10-10 15:51:43.638 16856-17358/zhangphil.app D/任务1: 开始运行...
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false
10-10 15:51:45.638 16856-17358/zhangphil.app E/zhangphil job: error while executing job zhangphil.app.MyJob@12572bd
java.lang.Exception
at zhangphil.app.MyJob.onRun(MyJob.java:38)
at com.birbit.android.jobqueue.Job.safeRun(Job.java:229)
at com.birbit.android.jobqueue.JobHolder.safeRun(JobHolder.java:132)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.handleRunJob(ConsumerManager.java:398)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.access$000(ConsumerManager.java:317)
at com.birbit.android.jobqueue.ConsumerManager$Consumer$2.handleMessage(ConsumerManager.java:345)
at com.birbit.android.jobqueue.messaging.SafeMessageQueue.consume(SafeMessageQueue.java:36)
at com.birbit.android.jobqueue.ConsumerManager$Consumer.run(ConsumerManager.java:380)
at java.lang.Thread.run(Thread.java:818)
10-10 15:51:45.638 16856-17358/zhangphil.app D/任务1: runCount: 3 maxRunCount: 20
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: safeRunResult for zhangphil.app.MyJob@12572bd : false. re run:false. cancelled: false
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] post message com.birbit.android.jobqueue.messaging.message.RunJobResultMessage@2fa28b
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 1476085905647085571
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085905647246300
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085905647246300
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message com.birbit.android.jobqueue.messaging.message.RunJobResultMessage@2fa28b
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type RUN_JOB_RESULT
10-10 15:51:45.638 16856-17353/zhangphil.app D/任务1: cancelReason:5
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] post message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085905666501300
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085905666501300
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage@a013fe4
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: keep alive: 1476086025647041248
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: Consumer idle, will kill? false . isRunning: true
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 1476085905680193019
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 1476085905680193019
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false

上述完整Logcat输出显示了代码在onRun里面抛出异常然后容错回滚机制发挥作用重启了任务,过滤“任务”后精简的关键Logcat输出:

10-10 15:51:39.488 16856-16856/zhangphil.app D/任务1: 初始化
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: added job class: MyJob priority: 500 delay: 0 group : 任务1 persistent: true
10-10 15:51:39.528 16856-17353/zhangphil.app D/任务1: 添加任务
10-10 15:51:39.568 16856-17358/zhangphil.app D/任务1: 开始运行...
10-10 15:51:41.568 16856-17358/zhangphil.app D/任务1: runCount: 1 maxRunCount: 20
10-10 15:51:41.608 16856-17358/zhangphil.app D/任务1: 开始运行...
10-10 15:51:43.608 16856-17358/zhangphil.app D/任务1: runCount: 2 maxRunCount: 20
10-10 15:51:43.638 16856-17358/zhangphil.app D/任务1: 开始运行...
10-10 15:51:45.638 16856-17358/zhangphil.app D/任务1: runCount: 3 maxRunCount: 20
10-10 15:51:45.638 16856-17353/zhangphil.app D/任务1: cancelReason:5

附录:
【1】《Java线程池:ExecutorService,Executors》链接地址:http://blog.csdn.net/zhangphil/article/details/43898637  
【2】《Java线程池及Future、Callable获得线程返回结果【Java线程池系列2】》链接地址:http://blog.csdn.net/zhangphil/article/details/49701219
【3】《Java线程池之FutureTask【Java线程池系列3】》链接地址:http://blog.csdn.net/zhangphil/article/details/49702751
【4】《Android Priority Job Queue (Job Manager)(一)》链接地址:http://blog.csdn.net/zhangphil/article/details/52777196

上一篇:css之单边阴影


下一篇:JS弹出浮层