File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Scheduler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Scheduler" Watch "Thread Scheduler" New topic
Author

Thread Scheduler

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hi there,

I have a task here and needed some inputs from you guys.
This task needs to be done using jdk1.4 only in weblogic 9 server

I have written a small thread which runs as a start up class once i a start my weblogic server.

Example:

I am making this thread poll the database every 5 min to check is there are any invalid recrods and if they are found This thread deletes it and sends email to user.
This works fine.

Now I have to also add one more new task to this in the same class.
I have to also send emails every midnight about the records in the database.

Here is where i am facing problem.

suppose i start my weblogic server at 5.00 pm then my program needs to calculate the midnight time and trigger exactly at midnight and there after run every midnight. simultaneously the polling mechanism i wrote should run as i have implemented every 5 min.

How can i achieve this in the same class? Do I need to start a seprate thread?
I came across a article which says timer task and timer in jdk1.4 should not be used in a managed environment like weblogic container as it runs a thread which is out of the scope of weblogic.

How do i go about this?








Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Nilesh Raje wrote:I came across a article which says timer task and timer in jdk1.4 should not be used in a managed environment like weblogic container as it runs a thread which is out of the scope of weblogic.


Any thread YOU create, if it is a Timer/TimerTask or Thread t = new Thread() is not managed by the web container. Some people (me included) don't like to do this. The JVM for the application container should be used for the web application(s) only and are best managed by app server. That is my opinion, anyway.


So I usually suggest to have your web container trigger an external application to start/stop at the correct times. For example, you polling/record management application doesn't really have much to do with the web app, so why put it in weblogic at all. Have a standalone service which polls the DB every 5 minutes and, at midnight sends an email. Weblogic would start the service when weblogic itself starts up and end the polling service when weblogic shuts down. In this case you could either use Timer/TimerTask or your OS' scheduling software to run the polling job.

If, on the other hand, you really want to run the task inside weblogic, then you are going to have to make a Thread, and this Thread will be outside weblogic's management in any case, so using a Timer/TimerTask is no different than your current solution in that regards. Timer/TimerTask does give you a lot more benefit in how the scheduled task occurs, so I would suggest the Timer/TimerTask solution.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

If, on the other hand, you really want to run the task inside weblogic, then you are going to have to make a Thread, and this Thread will be outside weblogic's management in any case, so using a Timer/TimerTask is no different than your current solution in that regards. Timer/TimerTask does give you a lot more benefit in how the scheduled task occurs, so I would suggest the Timer/TimerTask solution.


To be fair, this is actually common -- sometimes, it is just not possible to avoid starting another thread.

BTW, you also never mentioned what is the application type -- servlet? EJB? MBean? Web Service? Some of those types have start and stop methods (callbacks), that can be used to help manage the threads.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

This task needs to be done using jdk1.4 only in weblogic 9 server


BTW, I don't think that this is a valid configuration, as I believe that Weblogic 9 requires Java 5.

Henry
Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Well the configuration is valid and set up as per requirement.

Weblogic uses jdk1.5 but my application needs to use compile time JDK1.4 and runtime JRE1.5 as in this thread I am taking a handle to a EJB which is again compiled in jdk1.4 and run in jdk1.5 runtime.

Anyways I dont have any issues with configurtion cause thats how it has to be.

I could have scheduled this using unix cron but the requirement is to startup up with the server only. This has to start up with weblogic.

Well coming back to the question i raised. I have used Quartz for this.I found this pretty handy.
For more info here is the url http://www.opensymphony.com/
Timer and Timer task are not good options as they have a overhead and performance hit. I found several articles for this.

Here is what i have come up with now.

I have 2 classes which do 2 different tasks And the scheduled them in one class.


This class is for task 1.



This class is for task 2.



This is my main scheduler.



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Scheduler
 
Similar Threads
Timer starts twice when it should start only once
Thread and Timer
Ending a thread from jsp
MaximumThroughput- MltiThreading - Need help
Timer starts twice when it should start only once