aspose file tools*
The moose likes Threads and Synchronization and the fly likes creating a task to run every hour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "creating a task to run every hour" Watch "creating a task to run every hour" New topic
Author

creating a task to run every hour

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1038
    
  13

This might be more suited to the Threads forum, but we'll start here. It involves a context listener, a web service, and threads, so it qualifies for about 3 different forums.

I've been working on getting weather forecast feeds from the National Weather Service. I've created this context listener:

This is the WeatherFetcher class:


There is another class that populates the beans (ForecastProcessor) and a jsp page, but those are unimportant for now. I don't know if I followed Best Practices for this app as it's my first ever attempt at using web services, but it works. Suggestions on a better design are welcome.

But the challenge I'm facing now is how to get this to update once an hour. I've been reading Java Threads, chapter 11, but it's not helping. Unfortunately, this book is heavily geared toward Swing apps which doesn't translate well to JEE. If I understand it correctly, to use the ScheduledThreadPoolExecutor class, I would need to make the WeatherFetcher class implement the Runnable interface, but that means the code has to be moved to the run() method where I can't get the Map that is currently returned.

What's the suggested way to set up something like this to run on a continuous timer loop? And if you tell me my architecture is all wrong and I need to rip the guts out of it and start over, that's okay. This is a learning experience. Just point me in the right direction, please.



"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

For something like this, why not use Quartz?

Set up a job to run every hour, and let it go.

WP
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

If you're using JEE 5 or above, it looks like the Timer service is the way you're supposed to do it.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1038
    
  13

@William, I considered Quartz or a cron job, but I want this to be usable in any of our plant locations with a minimum of setup. We don't have Quartz installed, so that's a dozen or more servers where it would have to be setup and managed. It seems like I should just be able to spawn a thread and then put it to sleep for an hour, but it's turning out to be a bit more tricky than that.

@Paul, thanks for the link. I knew about the old TimerTask, but not this one. I'll be reading that this morning.

<edit>It appears the Timer Service requires an EJB container, so that's out. Back to the drawing board...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Jk Robbins wrote:@William, I considered Quartz or a cron job, but I want this to be usable in any of our plant locations with a minimum of setup. We don't have Quartz installed, so that's a dozen or more servers where it would have to be setup and managed. It seems like I should just be able to spawn a thread and then put it to sleep for an hour, but it's turning out to be a bit more tricky than that.

@Paul, thanks for the link. I knew about the old TimerTask, but not this one. I'll be reading that this morning.

<edit>It appears the Timer Service requires an EJB container, so that's out. Back to the drawing board...


First some modifications to your code to make it easier to move to a Thread-based system. First, I would make a class which contains the logic for updating the weather. I would also use it as a holder for the data, so I would put just one value in the servlet context (rather than a number of objects all mapped to different strings.)

So the equivalent behavior to what you posted above would be like this


With that as a backbone, it would be easy to create a Runnable which does nothing but calls weather.updateWeather(), and schedule it to run once per hour in a ScheduledExecutorService. I would change the listener to:


Note that I used a contextDestroyed method to make sure the scheduler shuts down - that is important. Also note that this is all non-compiled or tested code, so think of it as conceptual rather than cut-paste-and run. Finally, it would be best if you could use a Thread that your ServletContainer creates for use in the Executor, if your container lets you, you could provide a ThreadFactory to the Executors.newSingleThreadScheduledExecutor() to with a ThreadFactory implementation which grabs the Thread from a pool your container provides. But as you only need one thread and you will use it infrequently this is low risk.


Steve
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1038
    
  13

Thanks, Steve. Very good tips. I'll take your example and start working on version 2.0.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a task to run every hour