简体   繁体   中英

ThreadPool Executor not executing threads in GAE

I am trying to use executor framework in Google App engine. Bellow is the code that I am trying to run.

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);

But this doesn't start the thread. But if I use thread.start() it works properly. I have checked Whitelisted Classes and it does provide Executor classes. So where I am doing it wrong ?

Saikat,

You should always try to avoid creating threads on App Engine, because of it's distributed and dynamic nature it tends to have really bad/unexpected results.

In your case multiple instances will spawn multiple (local) threads sending many times the same notification. Also, bear in mind GAE front end instances have a 1 minute request limit , so after that time the server will kill that request.

Fortunately App Engine provides the Cron service for exactly this situations.

The Cron Service will allow you to schedule a job to run at a given time or every given period. When the cron is triggered GAE will call a configured URL so you can do your process, in your case send notifications.

Eg:(from the link provided)

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

will make an HTTP request to /weeklyreport every monday @8:30.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM