Solved

setting a timer in Java

Posted on 2012-03-14
16
224 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:onyourmark
  • 12
  • 4
16 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37719559
If you want to have it in pure java you can either use Timer and TimeTask classes
or ExctutoThreadPool. You can also download and use java Quartz library
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 37719565
Howevere if this is something you need to run rather rarely, say a few times a day - I'd prefer to use Windows scheduler on Winodws or a cron job in Unix - you'll not need to have a java process present all the time and have the OS or its functions to take care of the schedule
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719570
this is where you canm download Quartz
http://quartz-scheduler.org/
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 47

Expert Comment

by:for_yan
ID: 37719576
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719582
0
 

Author Comment

by:onyourmark
ID: 37719585
So your recommendation is to use scheduler/chron I think.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719588
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719591
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
0
 

Author Comment

by:onyourmark
ID: 37719598
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?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 37719599
I do have many java applications which I run couple of times a day for years both using Winodws scheduler on PC and using cron on Solaris or Linux - and I never had any problems with those
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719607
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
0
 

Author Comment

by:onyourmark
ID: 37719614
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?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37719616
Usually if you want to move your program form one computer to another, or to deliver it tyo the users, then jar files are more handy than folders with classes, but form the point of view of running java application it does not matter
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719621
Your third partyu classes (iusually packaged into the jar file) need to be accessible in the cvlasspath
of course also
0
 

Author Closing Comment

by:onyourmark
ID: 37719622
Thanks so much!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37719643
You are always welcome.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java jdbc batch example 8 41
SHA2 certs for IIS AND Java? 2 113
iterator/ListIterator approach 17 39
Tagging and Merging on Branch 1 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question