There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Cropper wrote:Hi,
I have been having a play around with scheduling a java class file with a few different technologies and I am struggling to get them working in an acceptable way.
First attempt
Was to set up a CRON job (via cPanel on my web host) to run my .class file - although that didn't work as it was not a binary file which could be executed.
Was to set up scheduling within my application using the TimerTask and Timer API's using the following code
While this did achieve the required result, it had a draw back. Being that if the URL was accessed (which was mapped to the Servlet) twice then two instances were created and the scheduled task which used to run every 10 minutes (for example) would now run twice within that 10 minute period.
The only way I could think of to get around this was to place the 'start timer' call on a URL which would have to be accessed each time the web application is restarted (quite frequently!). So back to the drawing board.
So I am a little lost on where to go from here.
fred rosenberger wrote:a cron job doesn't have to be a binary. I have scheduled many, many perl scripts (all of which are plain text) that run all the time.
What i would guess you need to do is not run the .class file but the full java command..something like
00,15,30,45 * * * * java yourClassName
Michael Cropper wrote:Thanks for the quick reply.
Following on from the first point about CRON jobs, if I were to use the CRON job "java -cp /my/file/directory/structure MyClass" would that work? I am not too familiar with compiling / running files from the command line as I use an IDE.
Michael Cropper wrote:
The script I run will have to be a Java file since that is the extent of my knowledge, plus the logic contains DB access and other Java API's to do some funky stuff. I have tried a huge variety of combinations with the CRON job at the moment and I believe it is an issue with either a) how I am calling the script or b) the actual script being called.
Note that Java programs are not "scripts". If you use that term, especially in this context, itcan lead to confusion.
Michael Cropper wrote:What would be the correct term? Personally I see the terms as inter-changable based on how I use them.
Michael Cropper wrote:Thanks for the information. I have to say that what you have said is beyond my level of expertise. Are there any recommended reading / resources that you can point me in to get up to speed.l
If you have other jars that are needed, then you need to include them in the -cp, separated by the appropriate delimiter ( colon ( : ) on Linux, semicolon ( ; ) on Windows).
Michael Cropper wrote:
If you have other jars that are needed, then you need to include them in the -cp, separated by the appropriate delimiter ( colon ( : ) on Linux, semicolon ( ; ) on Windows).
Looking through my source code there would be two .jar files which would need to be included in the CRON job which are;
- mail.jar (for JavaMail API)
- mysql-connector-java-5.1.17-bin.jar (for DB connections)
So would that translate to having a CRON command similar to the following
java -cp /my/dir/struct/ MyPackage.MyClass mail.jar : mysql-connector-java-5.1.17-bin.jar
Is that along the right lines?
And is the MyClass correct to contain all the logic within the main method so that it runs automatically?
Do all of the other imports within a class work, since the program is calling other class files to accomplish certain tasks.
SCJP
Visit my download page
Michael Cropper wrote: I am not too familiar with compiling / running files from the command line as I use an IDE. ...
Personally I see the terms as inter-changable based on how I use them.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Don't get me started about those stupid light bulbs. |