GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Help understanding jpoller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help understanding jpoller" Watch "Help understanding jpoller" New topic
Author

Help understanding jpoller

Danny Krinkle
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Hi all,

I had a requirement to produce a utility that monitors a directory for an incoming file, then copies that file to another directory. I wrote a (very) basic class to do this, but then found jpoller.

In the hope of learning something new, I decided to replace my basic app with something more robust, however I did have trouble interpreting the docs and can't help feeling Ive kind of fudged my way to getting this working. (I wont sleep until Im sure)

The documentation example states :-
DirectoryPoller poller = ...
//all other code blah!
poller.start(); // Start


But I just cant get my head round how/where the start() method is defined. I've had difficulty finding other examples and in my code have had to use :-
DirectoryPoller poller = ...
//all other code blah!
poller.run(); // Start


I wouldnt say Im new to using threads and really am posting this as a last resort. I just wondered if there is anybody else who is using jpoller who can point me in the right direction?

Kind Regards

Danny
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
Danny from your link it says DirectoryPoller is a Thread, and the way that threads work is you call start method and it calls the run method. Well technically it calls start0() first, which is declared as "private native void start0();", and that calls run(), but that's not important.

If you call the run method, sure, it will run, but it will run in the thread you called it from, I'm assuming that's your main method.
If you want it to run in a separate thread, then don't call the run method directly and stick to start().
If start() in your case didn't call run at all, that would be a strange bug indeed, but lets take a step back there first.
Danny Krinkle
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Hi Taariq,

Thanks for your reply, much appreciated.

I knew there was something not quite right but where I'm getting lost is :-

The class in question (DirectoryPoller) does not have a start() method.
so


DirectoryMonitor dm = new DirectoryMonitor();
dm.start();


will not compile and I cannot create a new Thread with

DirectoryMonitor dm = new DirectoryMonitor();
Thread t = new Thread(poller);
t.start();


Should I be extendeding the class and implementing rRunnable? Im just not sure how this last part should fit together.

Cheers

Danny
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
You're welcome Danny.

DirectoryPoller has a start method because it extends BaseSignalSourceThread which extends Thread, and Thread has start().

DirectoryMonitor I don't know about, is this one of your classes?

The download contains a source directory, can you compile the source file Test in the package org.sadun.util.polling.test?

That class uses DirectoryPoller and seems to be doing exactly what you want to do, so maybe get it running and copy and paste the bits you need.
Danny Krinkle
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Sorry Taariq,

My mistake. DirectoryMonitor should read DirectoryPoller.

I still cannot see the start method in the javadoc for DirectoryPoller (i've even searched the source file). If I pull up the source file, every method is present except for the start method. I've even tried a few previous versions and have the same issue.

Thanks again for taking the time to look.

Cheers

Danny
Danny Krinkle
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Doh!!.

I now realise there is a library of Utilities that needs to go alongside this.

I hit the documentation for the Directory Poller first and never saw this.

All is sorted and I have learned valuable lessons. I can now sleep!! javascript: x()
sleep

Cheers

Dan
[ June 18, 2008: Message edited by: Danny Krinkle ]
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
Nice one!
cheers
 
GeeCON Prague 2014
 
subject: Help understanding jpoller