Simple Task Scheduler

S

soup_or_power

Hi
I searched this group with the keyword "scheduler" and found several
threads. None of them meet my requirements as follows:
a) open a file of jobs with timestamps
b) construct a java class for each job older than current time; the
side effect of the constructor is to send an email
c) remove the job from the file
d) sleep for 1 sec and go to (a)

I know how to create a java class with the above steps in the main. I
can execute the java class with nohup on linux and make it almost like
a daemon.

However I am not sure about the overhead (the linux machine is
overloaded). Ideally I would like to check job file modification time;
if it is more recent than the saved modification time open the file
and do a bunch of stuff. I don't know how to do this. I think the File
class some stuff.

Also I would like to use the Scheduler class. But I couldn't find any
examples on how to use it.

Can someon please drop me some hints?

Thanks
 
T

TechBookReport

Hi
I searched this group with the keyword "scheduler" and found several
threads. None of them meet my requirements as follows:
a) open a file of jobs with timestamps
b) construct a java class for each job older than current time; the
side effect of the constructor is to send an email
c) remove the job from the file
d) sleep for 1 sec and go to (a)

I know how to create a java class with the above steps in the main. I
can execute the java class with nohup on linux and make it almost like
a daemon.

However I am not sure about the overhead (the linux machine is
overloaded). Ideally I would like to check job file modification time;
if it is more recent than the saved modification time open the file
and do a bunch of stuff. I don't know how to do this. I think the File
class some stuff.

Also I would like to use the Scheduler class. But I couldn't find any
examples on how to use it.

Can someon please drop me some hints?

Thanks
Have you looked at the Quartz scheduler framework
(http://www.opensymphony.com/quartz/)? Could be useful and save you
having to re-invent the wheel.

HTH
 
S

soup_or_power

Have you looked at the Quartz scheduler framework
(http://www.opensymphony.com/quartz/)?Could be useful and save you
having to re-invent the wheel.

HTH

That URL didn't work. I went to Source Forge and looked at the
documentation. I however couldn't find any downloads. The message says
the quartz home page has moved. Any thoughts on how to get a tutorial
and jar files?
Thanks
 
S

soup_or_power

You should have removed extra ")?" from the url. Tryhttp://www.opensymphony.com/quartz/

Thank you. It seems Quartz is more of an overhead on a linux system.
It may be ideal for a wintel architecture. What I was looking for
ideally is:

a) using API insert a job into the scheduler to fire at a specified
timestamp
b) receive confirmation from scheduler
c) execute other code

To give an analogy, one can using Runtime create a batch job (at
command) in Linux and let the linux take it from there.

Any scheduler out there that can handle the above scenario?

Thanks
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Thank you. It seems Quartz is more of an overhead on a linux system.
It may be ideal for a wintel architecture.

Quartz is completely platform independent - it is the same
at Linux and Windows. In best Jav atradition.
What I was looking for
ideally is:

a) using API insert a job into the scheduler to fire at a specified
timestamp
b) receive confirmation from scheduler
c) execute other code

To give an analogy, one can using Runtime create a batch job (at
command) in Linux and let the linux take it from there.

Any scheduler out there that can handle the above scenario?

So you are looking for a platform specific solution to interface
the platforms scheduler specifically for the Linux platform ?

Sounds as if Runtime.getRuntime().exec() is the best way to go. The
alternative must be JNI.

Arne
 
L

Lew

b) construct a java class for each job older than current time; the
side effect of the constructor is to send an email

It's a really bad idea to have side effects from a constructor. By its nature,
something called from a constructor is called from an incomplete object.
Things aren't always what you expect when an object is incomplete. Bugs happen
when constructors do more than construct.

It is better to construct an object from whatever method is responding to the
demand to handle a scheduled event, then have that fully-constructed object
send the email.

If you do want something to happen as a concomitant to construction, after ver
careful consideration, perhaps you could post an event or a message that gets
picked up and handled by some other, fully-constructed object that sends the
email.

-- Lew
 
S

soup_or_power

Quartz is completely platform independent - it is the same
at Linux and Windows. In best Jav atradition.





So you are looking for a platform specific solution to interface
the platforms scheduler specifically for the Linux platform ?

Sounds as if Runtime.getRuntime().exec() is the best way to go. The
alternative must be JNI.

Arne

Thanks for the feedback. My problem is the requests for scheduler come
in at run time. A FIFO or a queue implementation is required to handle
the scheduler requests. I found several implementations where the
authors/architects are using a static list of tasks to be scheduled. I
went quickly through the Quartz tutorial and felt it was not meant for
me at this time. Do you agree with me?

Regards
 
S

soup_or_power

It's a really bad idea to have side effects from a constructor. By its nature,
something called from a constructor is called from an incomplete object.
Things aren't always what you expect when an object is incomplete. Bugs happen
when constructors do more than construct.

It is better to construct an object from whatever method is responding to the
demand to handle a scheduled event, then have that fully-constructed object
send the email.

If you do want something to happen as a concomitant to construction, after ver
careful consideration, perhaps you could post an event or a message that gets
picked up and handled by some other, fully-constructed object that sends the
email.

-- Lew

Many thanks for your kind remarks.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Thanks for the feedback. My problem is the requests for scheduler come
in at run time. A FIFO or a queue implementation is required to handle
the scheduler requests. I found several implementations where the
authors/architects are using a static list of tasks to be scheduled. I
went quickly through the Quartz tutorial and felt it was not meant for
me at this time. Do you agree with me?

Quartz will start job X at time T. Or X1 at T1 and X2 at T2.

If you need sequential jobs, then create a job with a queue
of jobs, tell Quartz to run the main job and let the main job
run the jobs in the queue sequentially.

Arne
 
S

soup_or_power

Quartz will start job X at time T. Or X1 at T1 and X2 at T2.

If you need sequential jobs, then create a job with a queue
of jobs, tell Quartz to run the main job and let the main job
run the jobs in the queue sequentially.

Arne

I downloaded the 1.6 build and it didn't work with the commons jar
provided in the zip. So I downloaded 1.5 and it had no errors at run
time. I am posting the the code I cooked up because it doesn't work as
intended. The setDaemon method was ignored. I want to eventually port
this code to a servlet in a Tomcat environment. Unless the thread can
be sent to background as daemon, I think there is no way to make it
fly. I tested the code on a RedHat Linux. I'd appreciate any comments.

Oh another thing, if I don't comment out the scheduler shutdown, the
trigger never fires!! What giveS?

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import java.sql.Date;
import java.util.*;
public class t extends Thread {
public void run() {
try{
SchedulerFactory schedFact = new
org.quartz.impl.StdSchedulerFactory();

Scheduler sched = schedFact.getScheduler();

sched.start();

JobDetail jobDetail = new JobDetail("myJob",
null,
HelloJob.class);
jobDetail.getJobDataMap().put("input", "hello there again");
long startTime = System.currentTimeMillis() + 10000L;

SimpleTrigger trigger = new SimpleTrigger("myTrigger",
null,
new Date(startTime),
null,
0,
0L);

trigger.setName("myTrigger");
sched.scheduleJob(jobDetail, trigger);
//sched.shutdown(true);
}catch(Exception e) {}
}
public static void main (String [] args) {
t ex = new t();
//t.run();
ex.setDaemon(true);
ex.setPriority(Thread.MAX_PRIORITY);
ex.start();
//System.exit(0);
}
}
~
 
L

Lew

The setDaemon method was ignored. I want to eventually port
this code to a servlet in a Tomcat environment. Unless the thread can
be sent to background as daemon, I think there is no way to make it
fly. I tested the code on a RedHat Linux. I'd appreciate any comments.

You aren't supposed to do any explicit thread spawning in a web application,
much less daemon threads. Good way to really hose things.

-- Lew
 
S

soup_or_power

You aren't supposed to do any explicit thread spawning in a web application,
much less daemon threads. Good way to really hose things.

-- Lew

Thanks, Lew. When I made a servlet immplement Runnable the java
compiler didn't throw any errors.
 
L

Lew

Thanks, Lew. When I made a servlet immplement Runnable the java
compiler didn't throw any errors.

There's no compile-time issue with such code, so it won't. You can implement
Runnable even in non-threaded code, in fact, but that is also irrelevant. The
issue is at runtime, when the Web container (Tomcat, JBoss, BEA, WebShere,
OAS, Glassfish, Sun App Server, ...) tries to manage the application's thread,
and the application is doing its own thread thing. Entanglement ensues.

It can be done, I suppose, but I wouldn't want to do it.

-- Lew
 
G

Guest

Lew said:
There's no compile-time issue with such code, so it won't. You can
implement Runnable even in non-threaded code, in fact, but that is also
irrelevant. The issue is at runtime, when the Web container (Tomcat,
JBoss, BEA, WebShere, OAS, Glassfish, Sun App Server, ...) tries to
manage the application's thread, and the application is doing its own
thread thing. Entanglement ensues.

It can be done, I suppose, but I wouldn't want to do it.

In a servlet only container as Tomcat and in pre-1.4 J2EE app
servers it is the only way of doing it.

(timer service was added in J2EE 1.4 spec)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top