博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)
阅读量:5786 次
发布时间:2019-06-18

本文共 29147 字,大约阅读时间需要 97 分钟。



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@b1a3ff010-10 15:51:39.488 16856-16856/zhangphil.app D/Activity: performCreate Call Injection manager10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608589949510963610-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608589949510963610-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-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@b1a3ff010-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type ADD_JOB10-10 15:51:39.488 16856-16856/zhangphil.app I/InjectionManager: dispatchOnViewCreated > Target : zhangphil.app.MainActivity isFragment :false10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: added job class: MyJob priority: 500 delay: 0 group : 任务1 persistent: true10-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? 010-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:true10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: nothing has been poked. are we above load factor? true10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphil job: adding another consumer10-10 15:51:39.538 16856-16856/zhangphil.app D/SecWifiDisplayUtil: Metadata value : SecSettings210-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608589955179880210-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608589955179880210-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-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 null10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608589955355625010-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle10-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@a013fe410-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: -922337203685477580810-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 -922337203685477580810-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? true10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608589955576661510-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608589955576661510-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@257dc4d10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-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@257dc4d10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type CONSTRAINT_CHANGE10-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? 010-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:false10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: nothing has been poked. are we above load factor? false10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608589955906338610-10 15:51:39.548 16856-17359/zhangphil.app D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608589955906338610-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-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@a013fe410-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE10-10 15:51:39.558 16856-16856/zhangphil.app W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = zhangphil.app showWhenLocked = false10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c41310-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608589957164380210-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608589957164380210-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608589957173343810-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c41310-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: running job JobHolder10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphil job: running job MyJob10-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: null10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false10-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               : NOTHING10-10 15:51:39.588 16856-17359/zhangphil.app D/libEGL: eglInitialize EGLDisplay = 0x7f93bfa18810-10 15:51:39.588 16856-17359/zhangphil.app I/OpenGLRenderer: Initialized EGL, version 1.410-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=110-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:24928426410-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: 2010-10 15:51:41.568 16856-17358/zhangphil.app D/zhangphil job: safeRunResult for zhangphil.app.MyJob@9eb605a : false. re run:true. cancelled: false10-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@2fa28b10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608590158136135410-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590158143692710-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590158143692710-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-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@2fa28b10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type RUN_JOB_RESULT10-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@a013fe410-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: reinsert job result true10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590160338171810-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590160338171810-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-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@a013fe410-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c41310-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590161563557210-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590161563557210-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608590161570510410-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c41310-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: running job JobHolder10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphil job: running job MyJob10-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: null10-10 15:51:41.618 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false10-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: 2010-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: safeRunResult for zhangphil.app.MyJob@1ef2826 : false. re run:true. cancelled: false10-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@2fa28b10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608590361992828010-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590362000057210-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590362000057210-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-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@2fa28b10-10 15:51:43.618 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type RUN_JOB_RESULT10-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@a013fe410-10 15:51:43.618 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: reinsert job result true10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590363668260310-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590363668260310-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-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@a013fe410-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c41310-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590364357343610-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590364357343610-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608590364362822810-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c41310-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: running job JobHolder10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphil job: running job MyJob10-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: null10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] did on idle post a message? false10-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: 2010-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: safeRunResult for zhangphil.app.MyJob@12572bd : false. re run:false. cancelled: false10-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@2fa28b10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: flushing messages at time 147608590564708557110-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] remove message null10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: consumer manager on idle10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590564724630010-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590564724630010-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-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@2fa28b10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type RUN_JOB_RESULT10-10 15:51:45.638 16856-17353/zhangphil.app D/任务1: cancelReason:510-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@a013fe410-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphil job: [consumer_3] will wait on the lock forever10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590566650130010-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590566650130010-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-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@a013fe410-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] consuming message of type JOB_CONSUMER_IDLE10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: keep alive: 147608602564704124810-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: Consumer idle, will kill? false . isRunning: true10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] looking for next message at time 147608590568019301910-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: flushing messages at time 147608590568019301910-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: [priority_mq] next delayed job null10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphil job: [queue_RUN_JOB_RESULT_6] remove message null10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [queue_CONSTRAINT_CHANGE_5] remove message null10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [queue_ADD_JOB_2] remove message null10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: [queue_JOB_CONSUMER_IDLE_4] remove message null10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: joq idle. running:? true10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphil job: Job queue idle. next job at: null10-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: true10-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: 2010-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: 2010-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: 2010-10 15:51:45.638 16856-17353/zhangphil.app D/任务1: cancelReason:5

附录:

【1】《Java线程池:ExecutorService,Executors》链接地址:  
【2】《Java线程池及Future、Callable获得线程返回结果【Java线程池系列2】》链接地址:
【3】《Java线程池之FutureTask【Java线程池系列3】》链接地址:
【4】《Android Priority Job Queue (Job Manager)(一)》链接地址:

转载地址:http://fptyx.baihongyu.com/

你可能感兴趣的文章
Docker Data Center系列(二)- UCP安装指南
查看>>
Angular企业级开发(1)-AngularJS简介
查看>>
性能测试需求指标分析方法
查看>>
shiro的helloworld
查看>>
UDP,TCP的套接字编程的Python实现
查看>>
三月心情
查看>>
java语言实现树
查看>>
POJ 1015 Jury Compromise dp
查看>>
《Javascript高级程序设计》阅读记录(五):第六章 上
查看>>
Guava包学习-Multimap
查看>>
hdu 1159 Common Subsequence
查看>>
POJ 1385 Lifting the Stone【多边形重心】
查看>>
python etree解析xml
查看>>
shell中关于file的判断(转载)
查看>>
Android SDK NDK开发总结
查看>>
《Web全栈工程师的自我修养》
查看>>
PIE SDK地图鼠标事件监听
查看>>
Oracle冷备
查看>>
Hello Point——WebGL
查看>>
Javascript的this用法
查看>>