ThreadPool Executor 不在 GAE 中执行线程

ThreadPool Executor not executing threads in GAE

我正在尝试在 Google App Engine 中使用执行器框架。波纹管是我正在尝试 运行.

的代码
Thread thread = ThreadManager.createBackgroundThread(new Runnable(){
            public void run(){
                          try{
                                  LOGGER.info( "Checking background thread");                            
                                  Thread.sleep(10);
                              }
                          catch (InterruptedException ex){
                                           throw new RuntimeException("Exception:", ex);
                              }
                         }
                    });
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(10, ThreadManager.backgroundThreadFactory());
executor.scheduleAtFixedRate(thread, 0, 30, TimeUnit.MINUTES);

但这不会启动线程。但是如果我使用 thread.start() 它会正常工作。我检查了 Whitelisted Classes,它确实提供了 Executor 类。那么我哪里做错了?

赛卡特

您应该始终尽量避免在 App Engine 上创建线程,因为它具有分布式和动态的特性,往往会产生真正的 bad/unexpected 结果。

在您的情况下,多个实例将生成多个(本地)线程,多次发送相同的通知。另外,请记住,GAE 前端实例有一个 1 分钟的请求限制,因此在该时间之后服务器将终止该请求。

幸运的是,App Engine 为这种情况提供了 Cron service

Cron 服务将允许您在给定时间或每个给定时间段将作业安排到 运行。当 cron 被触发时,GAE 将调用配置的 URL 以便您可以执行您的过程,在您的情况下发送通知。

例如:(来自link提供的)

 <cron>
    <url>/weeklyreport</url>
    <description>Mail out a weekly report</description>
    <schedule>every monday 08:30</schedule>
    <timezone>America/New_York</timezone>
  </cron>

每周一@8:30 将向 /weeklyreport 发出 HTTP 请求。