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(TimeZ one.getTim eZone("EST "));
cal.set(Calendar.DAY_OF_WE EK, cal.get(Calendar.DAY_OF_WE EK));
cal.set(Calendar.HOUR_OF_D AY, 11);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECO ND, 0);
Date stTime = cal.getTime();
if (!stTime.before(new Date()))
new Timer(true).scheduleAtFixe dRate(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...
My requirement is to schedule the timer at 11 AM and repeat it after every minute.
Here is my code.
Calendar cal = Calendar.getInstance(TimeZ
cal.set(Calendar.DAY_OF_WE
cal.set(Calendar.HOUR_OF_D
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECO
Date stTime = cal.getTime();
if (!stTime.before(new Date()))
new Timer(true).scheduleAtFixe
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...
Why are you setting that time zone on the Calendar?
... and what does the following print?
:
System.out.println(System.getProperty("user.timezone"));
why not set it in scheduled tasks @ 11am every morning with the current code you have ?
ASKER
System.out.println(System. getPropert y("user.ti mezone"));
The above statement is printing 'America/New_York'
The above statement is printing 'America/New_York'
ASKER
Hi plusone3055,
Sorry. I did not get your solution.
thanks...
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"));
What OS are you using btw?
ASKER
I am using Windows XP.
What happens if I use this
Calendar cal = Calendar.getInstance(TimeZ one.getTim eZone("EST "));
just wanted to know.
Thanks....
What happens if I use this
Calendar cal = Calendar.getInstance(TimeZ
just wanted to know.
Thanks....
ASKER
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
>>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
ASKER
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...
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
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
tzs2.png
ASKER
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(TimeZ one.getTim eZone(Syst em.getProp erty("user .timezone" )));
instead of EST?
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(TimeZ
instead of EST?
My recommendation would be to use CronTrigger from the Quartz (org.quartz) library and use Cron syntax to define firing times
ASKER
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(TimeZ one.getTim eZone(Syst em.getProp erty("user .timezone" )));
is redundant.
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
> 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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)