<p>In podcast #15, Jeff mentioned he twittered about how to run a regular event in the background as if it was a normal function - unfortunately I can't seem to find that through twitter. Now I need to do a similar thing and are going to throw the question to the masses.</p>

<p>My current plan is when the first user (probably me) enters the site it starts a background thread that waits until the alloted time (hourly on the hour) and then kicks off the event blocking the others (I am a Windows programmer by trade so I think in terms of events and WaitOnMultipleObjects) until it completes.</p>

<p>How did Jeff do it in Asp.Net and is his method applicable to the Java web-app world?</p>

[{'answer_id': 48297, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 2, 'selected': False, 'text': '<p>Jeff\'s mechanism was to create some sort of cached object which ASP.Net would automatically recreate at some sort of interval - It seemed to be an ASP.Net specific solution, so probably won\'t help you (or me) much in Java world.</p>\n\n<p>See <a href="https://stackoverflow.fogbugz.com/default.asp?W13117" rel="nofollow noreferrer">https://stackoverflow.fogbugz.com/default.asp?W13117</a></p>\n\n<blockquote>\n  <p>Atwood: Well, I originally asked on Twitter, because I just wanted something light weight.  I really didn\'t want to like write a windows service.  I felt like that was out of band code. Plus the code that actually does the work is a web page in fact, because to me that is a logical unit of work on a website is a web page.  So, it really is like we are calling back into the web site, it\'s just like another request in the website, so I viewed it as something that should stay inline, and the little approach that we came up that was recommended to me on Twitter was to essentially to add something to the application cache with a fixed expiration, then you have a call back so when that expires it calls a certain function which does the work then you add it back in to the cache with the same expiration.  So, it\'s a little bit, maybe "ghetto" is the right word.</p>\n</blockquote>\n\n<p>My approach has always been to have to OS (i.e. Cron or the Windows task scheduler) load a specific URL at some interval, and then setup a page at that URL to check it\'s queue, and perform whatever tasks were required, but I\'d be interested to hear if there\'s a better way.</p>\n\n<p>From the transcript, it looks like FogBugz uses the windows service loading a URL approach also.</p>\n\n<blockquote>\n  <p>Spolsky: So we have this special page called heartbeat.asp.  And that page, whenever you hit it, and anybody can hit it at anytime: doesn\'t hurt.  But when that page runs it checks a queue of waiting tasks to see if there\'s anything that needs to be done.  And if there\'s anything that needs to be done, it does one thing and then looks in that queue again and if there\'s anything else to be done it returns a plus, and the entire web page that it returns is just a single character with a plus in it.  And if there\'s nothing else to be done, the queue is now empty, it returns a minus.  So, anybody can call this and hit it as many times, you can load up heartbeat.asp in your web browser you hit Ctrl-R Ctrl-R Ctrl-R Ctrl-R until you start getting minuses instead of pluses.  And when you\'ve done that FogBugz will have completed all of its maintenance work that it needs to do.  So that\'s the first part, and the second part is a very, very simple Windows service which runs, and its whole job is to call heartbeat.asp and if it gets a plus, call it again soon, and if it gets a minus call it again, but not for a while.  So basically there\'s this Windows service that\'s always running, that has a very, very, very simple task of just hitting a URL, and looking to see if it gets a plus or a minus and, and then scheduling when it runs again based on whether it got a plus or a minus.  And obviously you can do any kind of variation you want on this theme, like for example, uh you could actually, instead of returning just a plus or minus you could say "Okay call me back in 60 seconds" or "Call me back right away I have more work to be done."  And that\'s how it works... so that maintenance service it just runs, you know, it\'s like, you know, a half page of code that runs that maintenance service, and it never has to change, and it doesn\'t have any of the logic in there, it just contains the tickling that causes these web pages to get called with a certain guaranteed frequency.  And inside that web page at heartbeat.asp there\'s code that maintains a queue of tasks that need to be done and looks at how much time has elapsed and does, you know, late-night maintenance and every seven days delete all the older messages that have been marked as spam and all kinds of just maintenance background tasks.  And uh, that\'s how that does that.</p>\n</blockquote>\n'}, {'answer_id': 48300, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 1, 'selected': False, 'text': '<p>Here is how they do it on StackOverflow.com:</p>\n<p><a href="https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/">https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/</a></p>\n'}, {'answer_id': 48332, 'author': 'Zizzencs', 'author_id': 686, 'author_profile': 'https://Stackoverflow.com/users/686', 'pm_score': 5, 'selected': True, 'text': '<p>I think developing a custom solution for running background tasks doesn\'t always worth, so I recommend to use the <a href="http://www.quartz-scheduler.org/" rel="nofollow noreferrer">Quartz Scheduler</a> in Java.</p>\n\n<p>In your situation (need to run background tasks in a web application) you could use the ServletContextListener included in the distribution to <a href="http://www.quartz-scheduler.org/api/2.2.1/org/quartz/ee/servlet/QuartzInitializerListener.html" rel="nofollow noreferrer">initialize the engine at the startup of your web container</a>.</p>\n\n<p>After that you have a number of possibilities to start (trigger) your background tasks (jobs), e.g. you can use Calendars or cron-like expressions. In your situation most probably you should settle with <a href="http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronTrigger.html" rel="nofollow noreferrer">SimpleTrigger</a> that lets you run jobs in fixed, regular intervals.</p>\n\n<p>The jobs themselves can be described easily too in Quartz, however you haven\'t provided any details about what you need to run, so I can\'t provide a suggestion in that area.</p>\n'}, {'answer_id': 48335, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>As mentioned, Quartz is one standard solution. If you don\'t care about clustering or persistence of background tasks across restarts, you can use the built in  ThreadPool support (in Java 5,6). If you use a <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow noreferrer" title="Scheduled Executor Service">ScheduledExecutorService</a> you can put Runnables into the background thread pool that wait a specific amount of time before executing. </p>\n\n<p>If you do care about clustering and/or persistence, you can use JMS queues for asynchronous execution, though you will still need some way of delaying background tasks (you can use Quartz or the ScheduledExecutorService to do this).</p>\n'}, {'answer_id': 48537, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 2, 'selected': False, 'text': '<p>We use <a href="http://www.jarretttaylor.com/java/jt-cron.html" rel="nofollow noreferrer">jtcron</a> for our scheduled background tasks.\nIt works well, and if you understand cron it should make sense to you.</p>\n'}]
['https://Stackoverflow.com/questions/48293', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/342/']