1

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 Ganguly
  • 113
  • 1
  • 6
  • I'll take a look, but in general you should be avoiding creating your own background tasks via threads on AppEngine. What problem are you trying to solve that can't be solved with Push Queues? If you must use do what you're doing, probably better to use GCE. But the details of your question are still valid so I am also curious. – Bill Prin Mar 23 '15 at 17:35
  • @BIllPrin I am trying to send notification to users at a certain time using scheduler. that's why I am using executor to run an ifinite thread to check and send notification to user. In javadoc of app engine it is said that we can use executor in app engine. That's why I was trying to implement it. – Saikat Ganguly Mar 24 '15 at 05:42
  • When I get a chance I will take a look and if you're right file a bug, sorry for delay. – Bill Prin Mar 25 '15 at 21:40

1 Answers1

0

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.

jirungaray
  • 1,674
  • 1
  • 11
  • 18