How to correctly use ScheduledExecutorService?

Is it possible to use ScheduledExecutorService with Heroku

  • Is it possible to use ScheduledExecutorService with a webdyno or heroku shutdown the java process if no web call are made. That an example of what I want to use ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); final ScheduledFuture<?> timeHandle = scheduler.scheduleAtFixedRate(new TimePrinter(System.out), 0, 10, SECONDS); scheduler.schedule(new Runnable() { public void run() { } }, 60*60, SECONDS);

  • Answer:

    You can certainly use the ScheduledExecutorService on Heroku. There are no Java API limitations when running on Heroku. However, if you try to just shutdown the process, Heroku will attempt to start it back up. To handle scaling on Heroku you will need to call the Heroku management APIs.

xjodoin at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

It is correct that, if you only run a single dyno web app, the web process will be stopped if they are not receiving any traffic. A scheduler thread that runs inside a web process should only do work related to the process itself, e.g clear out internal caches, etc. If you stick to this use case, you don't need the scheduler process if your whole web process has been idled out. If your scheduler does work outside of the web process, then we recommend that you factor it into a dedicated http://devcenter.heroku.com/articles/run-non-web-java-processes-on-heroku and scale it to exactly one dyno (effectively a singleton process). Heroku will make sure the process is running, restarting it if it crashes, etc. The worker/clock process should do an infinite loop with a sleep call so it wakes up every X seconds and performs work. You will be charged a full dyno hour per hour you're running a worker process like this which is of course not always economical. If you only want to schedule something every 10 minutes or less frequently, you can use the http://devcenter.heroku.com/articles/scheduler which will only incur costs for the actual time it runs, not for the sleep time.

Jesper J.

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.