Firebase jobdispatcher 未在指定窗口内触发

2024-01-29

我正在实施 Firebase Jobdispatcher,触发时间指定在 10 到 20 秒之间。这是我安排工作的代码:

public static void scheduleCompatibleJob(Context context) {
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
    Job myJob = dispatcher.newJobBuilder()
            .setService(DataTrackerJobService.class) // the JobService that will be called
            .setTag("api_trigger")
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.executionWindow(10, 20)) // 10 to 20 seconds
            .setReplaceCurrent(true)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .build();

    dispatcher.mustSchedule(myJob);
}

以及服务等级:

public class DataTrackerJobService extends JobService {
@Override
public boolean onStartJob(final JobParameters job) {
    Log.e("start job", "started " + job);
    (new Handler()).postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.e("handler", "run");
            jobFinished(job, true);
        }
    }, 2000);
    return true;
}

@Override
public boolean onStopJob(JobParameters job) {
    Log.e("stop job", "stopped " + job);
    return false;
}

}

作业调度程序正在运行,但 logcat 中的时间不正确。随着工作的每次重新安排,时间差距不断扩大,而且从来没有在10到20秒之间。

06-07 11:19:16.429 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@f4250de4 06-07 11:19:18.432 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:20:16.436 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@f16ceb31 06-07 11:20:18.438 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:21:58.519 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@f4c635cd 06-07 11:22:00.520 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:23:40.602 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@f15f8e29 06-07 11:23:42.605 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:25:52.642 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@f48c1045 06-07 11:25:54.644 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:28:52.652 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@f3b49821 06-07 11:28:54.655 26174-26174/com.example.jobdispatcher E/处理程序:运行 06-07 11:32:04.688 26174-26174/com.example.jobdispatcher E/start 作业:已启动 com.firebase.jobdispatcher.JobInspiration@e7f3c1bd 06-07 11:32:06.690 26174-26174/com.example.jobdispatcher E/处理程序:运行

请检查 logcat 中的时间。指导我我在哪里出错了,或者它应该只以这种方式工作吗?基本上我想在 24 小时的时间间隔内实现它,但我想知道这是否有效,然后一次作业将在触发时间中指定的时间加倍后被调用。


如果您查看 Trigger 类的源代码,您会发现您不确定您的作业是否会在给定时间运行。

/**
 * Creates a new ExecutionWindow based on the provided time interval.
 *
 * @param windowStart The earliest time (in seconds) the job should be
 *                    considered eligible to run. Calculated from when the
 *                    job was scheduled (for new jobs) or last run (for
 *                    recurring jobs).
 * @param windowEnd   The latest time (in seconds) the job should be run in
 *                    an ideal world. Calculated in the same way as
 *                    {@code windowStart}.
 * @throws IllegalArgumentException if the provided parameters are too
 *                                  restrictive.
 */
public static JobTrigger.ExecutionWindowTrigger executionWindow(int windowStart, int windowEnd) {
    if (windowStart < 0) {
        throw new IllegalArgumentException("Window start can't be less than 0");
    } else if (windowEnd < windowStart) {
        throw new IllegalArgumentException("Window end can't be less than window start");
    }

    return new JobTrigger.ExecutionWindowTrigger(windowStart, windowEnd);
}

还应该注意的是,只有当上一个重复工作完成后,才会开始下一个重复工作。因此,当你的工作时间长且成本高时,下一次执行时间可能会出乎意料。对于耗时的作业,您应该扩展 SimpleJobService。

为了创建重复任务,我使用 util 方法来创建适当的触发器:

public class JobDispatcherUtils {
    public static JobTrigger periodicTrigger(int frequency, int tolerance) {
        return Trigger.executionWindow(frequency - tolerance, frequency);
    }
}

Usage:

class DataTrackerJobService extends SimpleJobService {
    // ...
}

public static void scheduleCompatibleJob(Context context) {
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
    Job myJob = dispatcher.newJobBuilder()
            .setService(DataTrackerJobService.class) // the JobService that will be called
            .setTag("api_trigger")
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(JobDispatcherUtils.periodicTrigger(20, 1)) // repeated every 20 seconds with 1 second of tollerance
            .setReplaceCurrent(true)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .build();

    dispatcher.mustSchedule(myJob);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firebase jobdispatcher 未在指定窗口内触发 的相关文章

随机推荐

  • 如何孵化PolyCollection实例?

    是否可以孵化 PolyCollection 实例 我想要从 fill Betweenx 返回一个 PolyCollection import matplotlib mlab as mlab from matplotlib pyplot im
  • Java,解析我知道为空的 JSON 对象

    我有一个 JSON 对象数组 为了解析这些数组并存储简单的数据类型值 我必须假设键名称并相应地存储它们 我还知道有时键的值将为空 例子 promotion null 我将如何解析这个 如果我尝试访问值为 null 的键 则会收到 JSONE
  • 异常 java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

    我正在尝试将 spring 和 Hibernate 与 mysql 集成 我创建了一个简单的 java 项目和一个包含 3 个类的包 一个应用程序 context xml 文件和一个用于映射的 hbm xml 但执行后 我得到这个错误 Er
  • Ruby 递归函数

    我在从递归函数返回值时遇到问题 def ask question question print question answer STDIN gets chomp ask question question if answer empty r
  • 来自 Google Play 的 SSL 警告

    收到来自 google play 的警告 我如何处理 WebViewClient onReceivedSslError 处理程序的不安全实现的 SSL 错误处理程序漏洞 请尽快解决此漏洞并增加升级后的APK的版本号 为了正确处理SSL证书验
  • Redux / RTK:为一个切片创建增强器?

    在我的 Redux RTK 存储中的一个切片中 要使该切片完成其工作 我所需要做的就是使用以下命令创建一个实体适配器createEntityAdapter https redux toolkit js org api createEntit
  • 类变量、范围解析运算符和不同版本的 PHP

    我在 codepad org 中尝试了以下代码 class test const TEST testing 123 function test testing TEST echo self testing class new test 它返
  • 如何编写自定义 POCO 串行器/解串器?

    我想为 FIX 消息编写一个自定义 NET 序列化器 反序列化器 与 XML 不同 基本上该消息的编码为
  • 使用用户名和密码登录后如何抓取网站

    我编写了一个网络爬虫 可以使用关键字抓取网站 但我想登录到我指定的网站并按关键字过滤信息 如何实现这一点 我发布了到目前为止我已经完成的代码 public class DB public Connection conn null publi
  • 如何在 React.JS 中添加 ClassName 并删除 onScroll 事件?

    我正在尝试制作一个粘性标题 可以根据他在页面上的位置更改其背景颜色 为此 我尝试将 className active 添加到我的样式组件 StyledHeader 中 当滚动位置 Y 高于 400 像素时 它将出现 低于 400 像素时消失
  • Selenium/Chrome/ChromeDriver 问题阻止 VPS 上的爬网(DevToolsActivePort 文件不存在)

    我购买了第一个 VPS 它运行 CentOS 7 64 位 在我今天开始使用这个 VPS 之前 我对 CentOS 7 的经验绝对为零 所以请对我宽容一点 当尝试使用 Scrapy 和 Selenium 抓取一些动态生成的内容时 脚本最终失
  • 重新发送 DocuSign 电子邮件

    是否有 API 端点允许我重新向收件人发送电子邮件 有时 用户可能无法收到或丢失包含签名链接的 DocuSign 电子邮件 我希望能够根据需要再次发送这些电子邮件 您可以使用 修改收件人 请求来触发向特定收件人重新发送电子邮件通知 PUT
  • Android 应用程序仅在 Eclipse 调试时因 SIGABRT Signal 6 崩溃

    我有一个应用程序可以在没有附加调试器的设备上完美运行 但是 我在Eclipse中调试时遇到了问题 当主线程挂起大约 10 秒或更长时间 例如遇到断点后 主线程会抛出 SIGABRT 显然来自 libc 我能想到的唯一解释是 主线程上的消息队
  • LINQ except 如何工作? [复制]

    这个问题在这里已经有答案了 可能的重复 LINQ 查找两个列表中的差异 https stackoverflow com questions 2404301 linq find differences in two lists 我想找到两个系
  • 抽象类设计:为什么不定义公共构造函数?

    看这里 抽象类设计 http msdn microsoft com en us library ms229047 aspx http msdn microsoft com en us library ms229047 aspx It say
  • HTML 表格,某些列自动调整,其他列固定宽度

    我正在尝试创建一个符合以下要求的表 表格宽度必须定义为 0 浏览器应根据列宽计算宽度 这是为了容纳列调整大小插件 某些列可能具有固定宽度 例如 50px 没有固定宽度的列必须自动适应内容 我创建了一个小例子 http jsfiddle ne
  • Rails:around_* 回调

    我已阅读以下文档http api rubyonrails org classes ActiveRecord Callbacks html http api rubyonrails org classes ActiveRecord Callb
  • 如何判断是否不再需要 git stash?

    是否可以判断是否已经应用了存储 因此不再需要 而无需执行git stash apply 假设我只使用一个分支 这可以通过使用来防止pop而不是apply当应用隐藏时 因此每次应用时都将其清除 但是 我有时使用 git stash 来保存正在
  • Apache:将 XAMPP/PHP 句柄从 application/x-httpd-php 更改为 application/x-httpd-php5

    我的目标是不再需要拥有 htaccess 文件的本地副本和实时副本 而是能够对本地 实时配置使用相同的单个 htaccess 文件 这将迫使我更好地理解配置服务器 我的本地服务器是我计算机上的 XAMPP 而我的实时服务器是共享 Web 主
  • Firebase jobdispatcher 未在指定窗口内触发

    我正在实施 Firebase Jobdispatcher 触发时间指定在 10 到 20 秒之间 这是我安排工作的代码 public static void scheduleCompatibleJob Context context Fir