onyourmark
asked on
setting a timer in Java
I have Java code which I would like to run at certain times during the day. I have Windows 2008 Server but I might also be able to do this on a Linux server.
Most of the code is in a file I have attached. I want to add code to read from a database and write to a database (both databases will be remote and won't be on this server).
I am wondering what the best way to have this code run at a certain times. Should I user window's scheduler or chron or is there some way within the java code to have it run at certain times? Also, I can compile this code into a class file. Will the be sufficient to run it or do I have to put it in a jar file? Note, in order for the file to run it needs other classes . These classes are from an open source Java program called Weka. I was going to install it on the server as well. Will this work?
Thanks for you help in advance!
JavaWekaJ48TestTrainPred2.java
Most of the code is in a file I have attached. I want to add code to read from a database and write to a database (both databases will be remote and won't be on this server).
I am wondering what the best way to have this code run at a certain times. Should I user window's scheduler or chron or is there some way within the java code to have it run at certain times? Also, I can compile this code into a class file. Will the be sufficient to run it or do I have to put it in a jar file? Note, in order for the file to run it needs other classes . These classes are from an open source Java program called Weka. I was going to install it on the server as well. Will this work?
Thanks for you help in advance!
JavaWekaJ48TestTrainPred2.java
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this is where you canm download Quartz
http://quartz-scheduler.org/
http://quartz-scheduler.org/
this is an example of using Timer / TimeTask
http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html
http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html
This is ThreadPoolExecutor class API:
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
ASKER
So your recommendation is to use scheduler/chron I think.
The issues related to reading/writing database or using third ;party java classes are hardly relevant - you can scheduel your runs for any java program or java process any of these ways
Yes, if this is somethiong noot happening on every minute scale, I'd think that cron or windows scheduler will provide less overhead and will take care of your schedule not worse than from within java
ASKER
Thanks. OK, also what about compiling this code into a class file. Will that be sufficient to run it or do I have to put it in a jar file? Note, in order for the file to run it needs other classes . These classes are from an open source Java program called Weka. I was going to install it on the server as well. Will this work?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In general this is not important if you run your java program from a collection of classes or you pakage them into a jkar file bfore, the main thing that they should be present in the classpath and classpath should be correct; wheteher they are jarred or not is not relevant
ASKER
So I don't need to do more than make them into class files? Is it hard to get them to run using the scheduler?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your third partyu classes (iusually packaged into the jar file) need to be accessible in the cvlasspath
of course also
of course also
ASKER
Thanks so much!
You are always welcome.
or ExctutoThreadPool. You can also download and use java Quartz library