Link to home
Start Free TrialLog in
Avatar of archinpalli
archinpalli

asked on

Java Timer Task

Hi,

My requirement is to schedule the timer at 11 AM and repeat it after every minute.

Here is my code.

         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
         cal.set(Calendar.DAY_OF_WEEK, cal.get(Calendar.DAY_OF_WEEK));
         cal.set(Calendar.HOUR_OF_DAY, 11);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         cal.set(Calendar.MILLISECOND, 0);
         
         Date stTime = cal.getTime();
         
         if (!stTime.before(new Date()))
             new Timer(true).scheduleAtFixedRate(new ImportTimerUtil(), stTime, 60000);

The timer is scheduled when I click a button.

The problem is the timer is started immediately after clicking a button(clicked the button at 10:30 AM). It is starting after evey 1 minute delay. It is not supposed to start immediately. It is supposed to start at 11:00 AM and after every minute from then.

Thanks...
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why are you setting that time zone on the Calendar?
... and what does the following print?
:
System.out.println(System.getProperty("user.timezone"));

Open in new window

why not set it in scheduled tasks @ 11am every morning with the current code you have ?

Avatar of archinpalli
archinpalli

ASKER

System.out.println(System.getProperty("user.timezone"));
 
The above statement is printing 'America/New_York'
Hi plusone3055,

Sorry. I did not get your solution.

thanks...
If you specifically are interested in not starting before 11 am NY time, you should use


Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));

Open in new window

What OS are you using btw?
I am using Windows XP.

What happens if I use this

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));

just wanted to know.

Thanks....
Why the timer is not starting at 11:00 AM? Why is it starting immediately when I click the button?
Well, the 3-letter zone codes are discouraged by Sun in favour of city names.

>>My requirement is to schedule the timer at 11 AM and repeat it after every minute.

If you're doing that on a daily basis using Windows, you'll be well advised to have it running as a Windows, service, for which you can use the Tanukisoft wrapper, and/or  use a scheduling framework such as Quartz
I am using the timer to start the import process that will read data from a file and import data into database at the scheduled time.

I am testing on my local machine which is Windows XP. I will deploy it on the server.

Will there be any problem if I use timer and schedule? Your reply is greatly appreciated.

Thanks...
>>Why the timer is not starting at 11:00 AM?

Study the attached carefully. The pane on the left shows EST and the right shows America/New_York
tzs.png
Sorry, there's important info missing from the right pane. I've folded out unimportant stuff here:
tzs2.png
Hi CEHJ,

The timer is starting at the mentioned time. I do  not know why it started immediately earlier. Now it is working. But I got to know the below information which is useful to me.

Well, the 3-letter zone codes are discouraged by Sun in favour of city names.

Will there be any problem if I use timer and schedule instead of Quartz for scheduling?

Can I use
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(System.getProperty("user.timezone")));

instead of EST?




My recommendation would be to use CronTrigger from the Quartz (org.quartz) library and use Cron syntax to define firing times
My only concern regarding this is I already spent time on this and implemented using timer. Just wanted to know if I will face any problem in future with timer.
Quartz/CronTrigger is well established Timer library used in many products that have proven successful.
>>Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(System.getProperty("user.timezone")));

is redundant.
The important thing, whatever you use, is to ensure that the program is run as a service, or it can be easily interrupted
> Well, the 3-letter zone codes are discouraged by Sun in favour of city names.

they still work fine

> Will there be any problem if I use timer and schedule instead of Quartz for scheduling?

quartz wouldn't make any difference
>>they still work fine

They don't work fine. You obviously either haven't read or haven't understood the previous posts
we work on many legacy application that use them without problem.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)