Solved

Scheduling a class to run

Posted on 2002-06-21
12
201 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 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

20 Experts available now in Live!

Get 1:1 Help Now