[jboss-user] [Management, JMX/JBoss] - Re: org.jboss.varia.scheduler.Scheduler Problem ?

jaikiran do-not-reply at jboss.com
Thu Sep 4 08:14:25 EDT 2008


"FDomino" wrote :  but its strange
  | that it looks like that the schedulePeriod is just added.... and not maybe converted... 
  | 
  | <attribute name="SchedulePeriod">86400000</attribute>
  | 
  | 

Though i haven't completely seen how its behaving on your system (i mean at what "time" the timer is triggered), i think i know what the problem is.

The problem lies in the value being passed to SchedulePeriod. 86400000 milli seconds form 1 day. But on the day when the DST is applied, adding 86400000 to the time is not equivalent to adding 1 day to the date. Here's an example - I have access to a system hosted in US (Pacific Standard Time). This year (2008) the daylight saving time began on March 9 at 2 AM.  So in our example, lets start with the date as March 9, 1 AM. The example below, 

- first adds 86400000 milli seconds to March 9, 1 AM.  The intention is to get the output date as March 10, 1 AM.  However as you see in the output, that i have posted below, the output date is Mar 10, 2 AM. Which is not what i wanted.

- Later, instead of adding 86400000 milli seconds, i add 1 day using Calendar.DATE, to the original date  March 9, 1 AM. Again, the intention is to get output date as March 10, 1 AM. As shown in the output, this works. So because of the DST, 1 day is not equal to 86400000 milli seconds on March 9.

There's a way to figure out the offset in milliseconds because of the DST. So when adding the  86400000 milli seconds to increment by a day, you should take into account, this offset. See my comments in the code below which does this. When this offset is considered,  the output date is as expected March 10, 1 AM.


  | package org.myapp;
  | 
  | import java.text.DateFormat;
  | import java.text.ParseException;
  | import java.text.SimpleDateFormat;
  | import java.util.Calendar;
  | import java.util.Date;
  | import java.util.GregorianCalendar;
  | import java.util.Properties;
  | import java.util.TimeZone;
  | 
  | public class DateUtility {
  | 
  |     /**
  |      * @param args
  |      */
  |     public static void main(String[] args) {
  |         try {
  | 
  |         	Calendar calendar = new GregorianCalendar();
  |         	TimeZone tz = calendar.getTimeZone();
  |         	System.out.println("Timezone is " + tz.getDisplayName());
  |         	System.out.println("Amount of time to be added because of DST  " + tz.getDSTSavings() + " milli sec");
  |         	DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  | 
  |             Date originalDate = df1.parse("2008-03-09 01:00:00.000");
  |             calendar.setTime(originalDate);
  |             System.out.println("Original date is " + calendar.getTime());
  | 
  | 		// Jaikiran: Adding 86400000 milli seconds to increment by one day
  | 		// does NOT work
  | 		// Add 86400000 milli seconds and display the output
  |             calendar.add(Calendar.MILLISECOND,86400000);
  |             System.out.println("Date after adding 86400000 milli sec to original date is " + calendar.getTime());
  | 
  |             // Reset to original date
  |             calendar.setTime(originalDate);
  | 
  | 		// Jaikiran: Adding 1 day through Calendar.DATE works
  |             // Now add 1 day and display the output
  | 		calendar.add(Calendar.DATE,1);
  |             System.out.println("Date after adding 1 day to original date is " + calendar.getTime());
  | 
  |             // Reset to original date
  |             calendar.setTime(originalDate);
  | 
  | 		// Jaikiran: Take into consideration the DST offset and then add the milli seconds.
  | 		// This works.
  |             // Now add (86400000 milli seconds - tz.getDSTSavings() ) and display the output
  |             calendar.add(Calendar.MILLISECOND,86400000 - tz.getDSTSavings());
  |             System.out.println("Date after adding (86400000-" + tz.getDSTSavings() + ") = " + (86400000 - tz.getDSTSavings()) + " milli sec to original date is " + calendar.getTime());
  | 
  | 
  | 		} catch (Exception e) {
  | 			e.printStackTrace();
  | 		}
  | 	}
  | 
  | }
  | 

The output of this code:

Timezone is Pacific Standard Time
  | Amount of time to be added because of DST  3600000 milli sec
  | Original date is Sun Mar 09 01:00:00 PST 2008
  | Date after adding 86400000 milli sec to original date is Mon Mar 10 02:00:00 PDT 2008
  | Date after adding 1 day to original date is Mon Mar 10 01:00:00 PDT 2008
  | Date after adding (86400000-3600000) = 82800000 milli sec to original date is Mon Mar 10 01:00:00 PDT 2008
  | 


So to handle this efficiently, either 

- The org.jboss.varia.scheduler.Scheduler should be enhanced to accept a string value like "DAILY" for the SchedulePeriod attribute. This will shield the user from passing the correct milli seconds value for a day. 

OR

- The user who configures the scheduler xml, should be smart enough to know the effect of DST and then decide what value to pass to the SchedulePeriod.




View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4174272#4174272

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4174272



More information about the jboss-user mailing list