Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Priority Job Queue (Job Manager):線程任務的容錯重啟機制(二)

Android Priority Job Queue (Job Manager):線程任務的容錯重啟機制(二)

編輯:關於Android編程

附錄文章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/zhangphiljob: [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/zhangphiljob: [priority_mq] looking for next message at time 1476085899495109636
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085899495109636
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:39.488 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [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/zhangphiljob: 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/zhangphiljob: 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/zhangphiljob: 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/zhangphiljob: nothing has been poked. are we above load factor? true
10-10 15:51:39.528 16856-17353/zhangphil.app D/zhangphiljob: 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/zhangphiljob: [priority_mq] looking for next message at time 1476085899551798802
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085899551798802
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: joq idle. running:? true
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphiljob: flushing messages at time 1476085899553556250
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message null
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphiljob: consumer manager on idle
10-10 15:51:39.548 16856-17358/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [consumer_3] will wait on the lock forever
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: Job queue idle. next job at: -9223372036854775808
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: 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/zhangphiljob: [priority_mq] did on idle post a message? true
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085899555766615
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085899555766615
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] consuming message of type CONSTRAINT_CHANGE
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: 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/zhangphiljob: 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/zhangphiljob: nothing has been poked. are we above load factor? false
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: flushing messages at time 1476085899559063386
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:39.548 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [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/zhangphiljob: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085899571643802
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085899571643802
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: joq idle. running:? true
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphiljob: flushing messages at time 1476085899571733438
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphiljob: running job JobHolder
10-10 15:51:39.568 16856-17358/zhangphil.app D/zhangphiljob: 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/zhangphiljob: Job queue idle. next job at: null
10-10 15:51:39.568 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: 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/zhangphiljob: safeRunResult for zhangphil.app.MyJob@9eb605a : false. re run:true. cancelled: false
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphiljob: [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/zhangphiljob: flushing messages at time 1476085901581361354
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message null
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085901581436927
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphiljob: consumer manager on idle
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085901581436927
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:41.578 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] consuming message of type RUN_JOB_RESULT
10-10 15:51:41.578 16856-17358/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [consumer_3] will wait on the lock forever
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: reinsert job result true
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085901603381718
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085901603381718
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:41.598 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085901615635572
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085901615635572
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphiljob: flushing messages at time 1476085901615705104
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:41.608 16856-17353/zhangphil.app D/zhangphiljob: joq idle. running:? true
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphiljob: running job JobHolder
10-10 15:51:41.608 16856-17358/zhangphil.app D/zhangphiljob: 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/zhangphiljob: Job queue idle. next job at: null
10-10 15:51:41.618 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] did on idle post a message? false
10-10 15:51:43.608 16856-17358/zhangphil.app E/zhangphiljob: 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/zhangphiljob: safeRunResult for zhangphil.app.MyJob@1ef2826 : false. re run:true. cancelled: false
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphiljob: [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/zhangphiljob: flushing messages at time 1476085903619928280
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message null
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085903620000572
10-10 15:51:43.608 16856-17358/zhangphil.app D/zhangphiljob: consumer manager on idle
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085903620000572
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:43.608 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] consuming message of type RUN_JOB_RESULT
10-10 15:51:43.618 16856-17358/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [consumer_3] will wait on the lock forever
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: reinsert job result true
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085903636682603
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085903636682603
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:43.628 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [consumer_3] post message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085903643573436
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085903643573436
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphiljob: flushing messages at time 1476085903643628228
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: joq idle. running:? true
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message com.birbit.android.jobqueue.messaging.message.RunJobMessage@725c413
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphiljob: running job JobHolder
10-10 15:51:43.638 16856-17358/zhangphil.app D/zhangphiljob: 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/zhangphiljob: Job queue idle. next job at: null
10-10 15:51:43.638 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] did on idle post a message? false
10-10 15:51:45.638 16856-17358/zhangphil.app E/zhangphiljob: 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/zhangphiljob: safeRunResult for zhangphil.app.MyJob@12572bd : false. re run:false. cancelled: false
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphiljob: [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/zhangphiljob: flushing messages at time 1476085905647085571
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphiljob: [consumer_3] remove message null
10-10 15:51:45.638 16856-17358/zhangphil.app D/zhangphiljob: consumer manager on idle
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085905647246300
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085905647246300
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:45.638 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [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/zhangphiljob: [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/zhangphiljob: [consumer_3] will wait on the lock forever
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085905666501300
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085905666501300
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:45.658 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: [priority_mq] consuming message of type JOB_CONSUMER_IDLE
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphiljob: keep alive: 1476086025647041248
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphiljob: Consumer idle, will kill? false . isRunning: true
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] looking for next message at time 1476085905680193019
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphiljob: flushing messages at time 1476085905680193019
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphiljob: [priority_mq] next delayed job null
10-10 15:51:45.668 16856-17353/zhangphil.app D/zhangphiljob: [queue_RUN_JOB_RESULT_6] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphiljob: [queue_CONSTRAINT_CHANGE_5] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphiljob: [queue_ADD_JOB_2] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphiljob: [queue_JOB_CONSUMER_IDLE_4] remove message null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphiljob: joq idle. running:? true
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphiljob: Job queue idle. next job at: null
10-10 15:51:45.678 16856-17353/zhangphil.app D/zhangphiljob: [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/zhangphiljob: 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. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved