ThreadPool Executor不在GAE中执行线程

时间:2015-03-23 11:52:11

标签: java google-app-engine threadpoolexecutor gae-backends

我正在尝试在Google App引擎中使用执行器框架。贝娄是我试图运行的代码。

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类。那我在做错了什么?

1 个答案:

答案 0 :(得分:0)

Saikat,

你应该总是尽量避免在App Engine上创建线程,因为它具有分布式和动态性,它往往会产生非常糟糕/意外的结果。

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

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

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

例如:(来自提供的链接)

 <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请求。

相关问题