• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating a task to run every hour

 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For something like this, why not use Quartz?

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

WP
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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...
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Steve. Very good tips. I'll take your example and start working on version 2.0.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic