Solved

Scheduling a class to run

Posted on 2002-06-21
12
202 Views
Last Modified: 2011-09-20
Hi,
 I have created a java class which checks for txt files in a server folder and delete's them if they exist (Note I am doing this because only the encrypted version of the file should remain in this folder).
 I want to run this class about every 5 or 10 mnutes (one or the other - haven't decided yet).
 Therefore I presume I will need to add a scheduler to the Windows 200 server on which this runs which starts the java class running every 10 minutes.
 I wondered, how do i create this scheduler (never done this type of thing before), and also can I check if the class is already running (in order to stop it trying to run twice at the same time).

Any help would be greatly appreciated.

 

0
Comment
Question by:joannape
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 1

Accepted Solution

by:
googlyralph earned 75 total points
ID: 7098329
Hi,

well, there may be a way of doing this from the Microsoft Java API's, there are things like directX, and registry access. You might be able to write a class that you manually add to the windows scheduler that runs whenever you set it to. Finally, you could write a class, that implements Runnable, and set it to "sleep" for X seconds, X being the number of secodns delay between each run, then when the thread wakes up it does whatever actions are required.

GR.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7098358
Control Panel/Sheduled Tasks
0
 

Expert Comment

by:chintamanidhotre
ID: 7098445
Hi joannape,

u can create a batch file for running the java .class file and add that batch file to the scheduler.
0
 
LVL 1

Expert Comment

by:googlyralph
ID: 7098467
What about adding a registry key in "run services" with a value of

"javaw com.package.Class param param2"

This will run the java program at system start up, and it will stay in memory running all the time. There is no issue of multiple coppies, as there is only one running.

in hte class you would have somehting like


TIME_TO_SLEEP = 3600;

try{
   deleteFile();
   sleep(TIME_TO_SLEEP);
}catch(interupptedException ie){

}

There is no reason why TIME_TO_SLEEP couldnt be read from a config file etc.

GR.
0
 
LVL 3

Expert Comment

by:daitt
ID: 7098589
I agree with googlyralph, we can schedule it in java, why need Windows Scheduler.
chintamanidhotre: Please don't propose an answer if your idea is not the first solution posted here and not completely answers the question. You can post a comment instead.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7098596
I could say - why to create a stuff that you don't need -you have a windows scheduler :))
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:daitt
ID: 7098654
as googlyralph said, if we program it ourselves, we won't have the problem of multiple processes doing the same thing. We still can use Windows Scheduler to start the program (or we can setup a service with JavaService or something like that).
But if the task is just deleting txt files in a single folder, I don't think it can take more than 1 minutes. If it's true, using Windows Scheduler is much simpler.
0
 
LVL 1

Expert Comment

by:googlyralph
ID: 7098673
if its *just* deleteing temp files then writing a batch file:

echo y | del c:\pathtofiles\*.txt > null

and running this form the Windows Scheduler would be simple, but i assume it is being written in a programming language becuase it has to be slightly more intelligent than deleting some files with a specific extension (though DOS can be fairly clever if you know how to use it).

(note BE CAREFUL WITH THIS CODE, it wont ask to delete, and it wont tell you it is going to / has done)

GR.
0
 
LVL 1

Expert Comment

by:johnchan2000
ID: 7104258
joannape,
  You shouldn't have to write any additional code to do this.  If this is for the unix enironment then you can schedule the execution of your program via the usage of the CRON command -- do a man on cron and crontab for more info.  If this is for the MSWindows environment, you can do the same thing with the at command.  The Windows help should provide you plenty of examples on how to run this.

Regards,
John
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7167454
Hi,

You can use the java.util.Timer, so you need not depened on the platform you run.
Change your code to extends TimerTask
put your execution in the run() method schedule your task
using schedule(...) method.

see adition information in API

Best regards
Nir
0
 
LVL 5

Expert Comment

by:vemul
ID: 7888049
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- split points between googlyralph, Venci75 and johnchan2000

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer

0
 
LVL 1

Expert Comment

by:Computer101
ID: 7959700
Venci75 and johnchan2000, look for your question in this topic atrea

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 139
Java asynchronous logging 4 50
object oriented programming comparison 5 52
arguments to jar 5 10
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now