Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

Re: Watching a directory for new files

robert lazarski

2007-01-16

Replies:

You can use just one thread just fine. Timer does have some drawbacks.
Here's an example:

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
      // Do pings, starting now, with a 2 second delay
      ScheduledFuture <?> ping = ses.scheduleWithFixedDelay(new PingTask(),
        0L, 2000L, TimeUnit.MILLISECONDS);

HTH,
Robert

On 1/16/07, David Kerber <dckerber@(protected):
> Thanks for the suggestion. I don't need the extra capabilities or
> multiple threads for this app because of its simplicity and relatively
> low traffic rate, but I'll keep it in mind if I need to handle more
> traffic later on.
>
> Dave
>
>
> robert lazarski wrote:
>
> > If you are using java 5 or higher, consider using
> > ScheduledThreadPoolExecutor which is generally considered a
> > replacement for Timer.
> >
> > Robert
> >
> > On 1/16/07, David Kerber <dckerber@(protected):
> >
> >> The javax.management.Timer class was rather more complex than I liked
> >> for the simple stuff I needed, but while digging into it, I discovered
> >> the java.util.Timer class, which works great and is easy to implement,
> >> so thanks for putting me on the Timer track!
> >>
> >> Dave
> >>
> >>
> >> David Kerber wrote:
> >>
> >> > Mikolaj Rydzewski wrote:
> >> >
> >> >> David Kerber wrote:
> >> >>
> >> >>> Thanks for the suggestion. One question about this technique: can
> >> >>> I run the directory check loop directly in the contextInitialized
> >> >>> event of the Listener, or is it mandatory to create a new thread?
> >> >>
> >> >>
> >> >> You can't make an infinite (almost) loop during processing an event.
> >> >> Using threads is discouraged ;-)
> >> >>
> >> >> I'd suggest you to use javax.management.timer.Timer class instead.
> >> >> You will not start another thread yourself, you will be notified at
> >> >> specified intervals to check the directory, etc. Another option is to
> >> >> use quartz to schedule directory checking events.
> >> >>
> >> > That certainly sounds like a cleaner solution than a sleep loop.
> >> >
> >> > Dave
> >
>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@(protected)
> To unsubscribe, e-mail: users-unsubscribe@(protected)
> For additional commands, e-mail: users-help@(protected)
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@(protected)
To unsubscribe, e-mail: users-unsubscribe@(protected)
For additional commands, e-mail: users-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.