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

简介: Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)附录文章4简单介绍了如何启动一个后台线程任务,Android Priority Job Queue (Job Manager)有一个重要的机制在附录文章4还没有体现:线程任务失败后重试、重启线程任务。


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

相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
7天前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android多线程编程的重要性及其实现方法,涵盖了基本概念、常见线程类型(如主线程、工作线程)以及多种多线程实现方式(如`Thread`、`HandlerThread`、`Executors`、Kotlin协程等)。通过合理的多线程管理,可大幅提升应用性能和用户体验。
25 15
一个Android App最少有几个线程?实现多线程的方式有哪些?
|
9天前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android应用开发中的多线程编程,涵盖基本概念、常见实现方式及最佳实践。主要内容包括主线程与工作线程的作用、多线程的多种实现方法(如 `Thread`、`HandlerThread`、`Executors` 和 Kotlin 协程),以及如何避免内存泄漏和合理使用线程池。通过有效的多线程管理,可以显著提升应用性能和用户体验。
29 10
|
7天前
|
API Android开发 iOS开发
安卓与iOS开发中的线程管理对比
【9月更文挑战第12天】在移动应用的世界中,安卓和iOS平台各自拥有庞大的用户群体。开发者们在这两个平台上构建应用时,线程管理是他们必须面对的关键挑战之一。本文将深入探讨两大平台在线程管理方面的异同,通过直观的代码示例,揭示它们各自的设计理念和实现方式,帮助读者更好地理解如何在安卓与iOS开发中高效地处理多线程任务。
|
9天前
|
Java Android开发 开发者
安卓应用开发中的线程管理优化技巧
【9月更文挑战第10天】在安卓开发的海洋里,线程管理犹如航行的风帆,掌握好它,能让应用乘风破浪,反之则可能遭遇性能的暗礁。本文将通过浅显易懂的语言和生动的比喻,带你探索如何优雅地处理安卓中的线程问题,从基础的线程创建到高级的线程池运用,让你的应用运行更加流畅。
|
21天前
|
安全 网络安全 数据安全/隐私保护
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享安卓与iOS开发中的线程管理比较
【8月更文挑战第30天】本文将探讨网络安全与信息安全的重要性,并分享关于网络安全漏洞、加密技术和安全意识的知识。我们将了解常见的网络攻击类型和防御策略,以及如何通过加密技术和提高安全意识来保护个人和组织的信息安全。
|
Java Android开发 调度
|
5天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
2天前
|
Java Linux Android开发
深入理解Android开发:从基础到高级
【9月更文挑战第17天】本文将深入探讨Android开发的各个方面,包括应用开发、操作系统等。我们将通过代码示例来展示如何创建一个简单的Android应用,并解释其背后的原理。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和启示。
|
9天前
|
IDE 开发工具 Android开发
安卓与iOS开发对比:平台选择对项目成功的影响
【9月更文挑战第10天】在移动应用开发的世界中,选择正确的平台是至关重要的。本文将深入探讨安卓和iOS这两大主要移动操作系统的开发环境,通过比较它们的市场份额、开发工具、编程语言和用户群体等方面,为开发者提供一个清晰的指南。我们将分析这两个平台的优势和劣势,并讨论如何根据项目需求和目标受众来做出最佳选择。无论你是初学者还是有经验的开发者,这篇文章都将帮助你更好地理解每个平台的特性,并指导你做出明智的决策。