Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Simple Task Scheduler

Thread view: 
soup_or_power@yahoo.com - 15 Mar 2007 16:20 GMT
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
TechBookReport - 15 Mar 2007 16:37 GMT
> Hi
> I searched this group with the keyword "scheduler" and found several
[quoted text clipped - 21 lines]
>
> 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

Signature

TechBookReport Java http://www.techbookreport.com/JavaIndex.html

soup_or_power@yahoo.com - 15 Mar 2007 17:10 GMT
> soup_or_po...@yahoo.com wrote:
> > Hi
[quoted text clipped - 33 lines]
>
> - Show quoted text -

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
ck - 15 Mar 2007 19:34 GMT
On Mar 15, 9:10 pm, "soup_or_po...@yahoo.com"
<soup_or_po...@yahoo.com> wrote:

> > soup_or_po...@yahoo.com wrote:
> > > Hi
[quoted text clipped - 39 lines]
> and jar files?
> Thanks

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

--
Ck
http://www.gfour.net
soup_or_power@yahoo.com - 15 Mar 2007 22:57 GMT
> On Mar 15, 9:10 pm, "soup_or_po...@yahoo.com"
>
[quoted text clipped - 50 lines]
>
> - Show quoted text -

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
Arne Vajhøj - 15 Mar 2007 23:49 GMT
> 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:
[quoted text clipped - 8 lines]
>
> 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
Lew - 16 Mar 2007 01:57 GMT
soup_or_power@yahoo.com wrote:
> 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
soup_or_power@yahoo.com - 16 Mar 2007 03:02 GMT
> soup_or_po...@yahoo.com wrote:
> > b) construct a java class for each job older than current time; the
[quoted text clipped - 15 lines]
>
> -- Lew

Many thanks for your kind remarks.
soup_or_power@yahoo.com - 16 Mar 2007 02:59 GMT
> soup_or_po...@yahoo.com wrote:
> > Thank you. It seems Quartz is more of an overhead on a linux system.
[quoted text clipped - 23 lines]
>
> 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
Arne Vajhøj - 19 Mar 2007 02:35 GMT
> 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
soup_or_power@yahoo.com - 21 Mar 2007 22:03 GMT
> soup_or_po...@yahoo.com wrote:
> > Thanks for the feedback. My problem is the requests for scheduler come
[quoted text clipped - 11 lines]
>
> 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);
       }
}
~
Lew - 21 Mar 2007 23:26 GMT
> 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
soup_or_power@yahoo.com - 25 Mar 2007 14:02 GMT
> soup_or_po...@yahoo.com wrote:
> > The setDaemon method was ignored. I want to eventually port
[quoted text clipped - 6 lines]
>
> -- Lew

Thanks, Lew. When I made a servlet immplement Runnable the java
compiler didn't throw any errors.
Lew - 25 Mar 2007 16:50 GMT
>> soup_or_po...@yahoo.com wrote:
>>> The setDaemon method was ignored. I want to eventually port
[quoted text clipped - 8 lines]
> 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
Arne Vajhøj - 09 Jun 2007 18:24 GMT
>>> soup_or_po...@yahoo.com wrote:
>>>> The setDaemon method was ignored. I want to eventually port
[quoted text clipped - 16 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.