Can I get a notification if clock changed/adjusted? - java

I have a program which relies on the clock-time to do some recurring tasks. How can I tell that clock was changed and I need to re-adjust my tasks?
EDIT: I am using a Date instance to see if time already passed or not.

Start a new thread which:
Fetches the current clock time and stores it in a variable V
Sleeps for X milliseconds
Adds X to V, compares it to the current clock time
If the values don't match to within a few milliseconds, assume the clock has changed, and trigger a reschedule
Rinse and repeat

Related

How can I capture time lag of an event passed through various components in a distributed system?

I have a group of components connected and placed in a distributed system. AN events gets injected into the starting components and gets passed through all components which are placed in different region containers and under different time zone. I want to capture time lag of
this event from the time of it being passed entering into the first component to going out from the last component and being notified to the subscribed client. Note, There should not be any negative time difference coming while measuring the lag when event flows from one component to other wherein bot two components are in different time zones.This is a strictly distributed system here.
Simply by getting the time difference to a fixed point.
Calendar.getInstance().getTimeInMillis() returns the ms to the 1.1.1970 UTC.
If you save the millseconds right before you begin your execution chain and compare (subtract the start time from the end time) that value is the elapsed time in ms.

AWS SWF Schedule an Activity

Using Java Flow Framework for SWF.
Any way to schedule an activity for a specific time in SWF?
Let's say, send run the "sendNotification" at 22nd Oct 2017 9:00 AM.
The examples allow to create timer with a delay, but not for the specified time.
Even though I can calculate the delay, based on the current time stamp and the target time when the event is needed to be fired, but how can I get the current time during workflow execution, since if you try to get the current system time in workflow decider, it creates non-determinism in workflow and it fails.
Date currentDate = new Date();
long sysTimeInSeconds = (currentDate.getTime() / 1000);
System.out.println("System Time : " + sysTimeInSeconds);
Promise<Void> timer = clock.createTimer((timeStampInSeconds - sysTimeInSeconds));
[ The above code is not allowed since we are using current time from system]
The current time of workflow clock, returns the start time of the workflow, which is of no use to me.
So how I can schedule an activity for a particular time ?
Use the WorkflowClock. It returns the correct deterministic current time which is the time of the decision being replayed.
Here is the line of code that updates clock to the current decision time.

What happens with running timers when a system time is changed?

What happens with running timers when a system time is changed?
I have an android application and I use handler.postDelayed(Runnable,interval) to post a Runnable to be called (the run() method) at the end of the interval.
The question I have is:
What happens if the underlying system time is changed externally?
My impression is that the posting still happens but at the time of system time change the countdown starts again... Can anybody shade some light here?
Does the behavior change if the time change is forwards or backwards?
First,you should know Handler is based on SystemClock.uptimeMillis().
Handlers sendMessageXXX() methods such as sendMessageDelayed、sendEmptyMessage all use the method below internal:
//calcute the milliseconds we hope to handle the message since the system was booted
sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis)
Then,the time interval value SystemClock.uptimeMillis() + delayMillis will be kept into Message's field when,and we put the message into the MessageQueue waiting for Looper to poll out it.
While the looper gets the next message from the queue,it will compare SystemClock.uptimeMillis() with msg.when to judge whether the message is ready.If the next message isnt ready,it will set a timeout to wake up until the msg is ready.
Second,you confuse SystemClock.uptimeMillis() with System.currentTimeMillis().Below is part of the documentation of SystemClock which explains the two concepts:
SystemClock.uptimeMillis() is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is suitable for interval timing when the interval does not span device sleep.
System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.
No it doesn't matter. If you dig around the code, the delay is provided by the following mechanism -
SystemClock.uptimeMillis() + delayMillis
So it is purely relative. And changing of system time has no effect on it

Performing a task everyday using TimerTask doesn't work for the next day.(with other problems)

I have a TimerTask that updates a few tables everyday at specific times(not one task per day, different tasks at different times in a day). I get a few time periods from a database and check if the current time is equal to one of the times in the database when the run() of TimerTask is called and perform the respective task for respective time(time from database which is equal to current time)
I think it's not working for the next day because I set the time(which I obtain from database) for the timer when the service starts. The next day when the timer is supposed to do the task and checks with the current time it checks with the previous day's time(the day service is started and time is set for TimerTask) and does not do the task. I should start the Service everyday to set the time(which is obtained from database to check with current time).
How do I start the service everyday at specific time so that the time to be checked with current time is set everyday.
I can try to schedule to do the tasks everyday 24 hours from the day service is started, but sometimes I have to change the start date and for that the service should be started again and I dont know if a service will run for 24 hours.
You should use Alarmanager for this. The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

Java timer task: Adjusting daylight saving time

I have a piece of code that has to be executed at a particular time every day. If I schedule it to be executed at 9PM everyday, then it has to work even during the switching of Day light saving.
Which Java API can be used to achieve this?
int ONE_DAY = 1000 * 60 * 60 * 24;
Timer timer = new Timer();
timer.schedule(myTimerTask, startTime, ONE_DAY); // startTime is 9PM of current day
I've used the above approach which will not take care of DST.
If you need to schedule based on calendrical values - rather than just elapsed time, basically - then you either need to wrap Timer in your own code, or use a library which has already been built for this purpose. In this case, I suspect the Quartz Scheduler is your best bet.
Given how complicated date/time can be, I'd generally recommend using a well-known library over rolling your own code. Note that this often doesn't mean that you can get away without thinking about complicated aspects of the problem - it just means that you should be able to express your requirements fairly simply. For example, in the context you're looking at, you should consider:
What time zone do you want the "9pm" to be expressed in? Is it the system default time zone? Some other specific one? Multiple different time zones for different tasks?
What do you want to happen if the scheduled time doesn't occur, or occurs twice on one day? You're likely to be okay with 9pm, but if you had (say) 1.30am in the UK time zone, when the clocks go forward into BST, that will be skipped for that day - and when the clocks go back into GMT, it will occur twice.
How do you want to handle the system clock being changed, either manually or automatically?
You can schedule the timer to run the task each hour and let the task decide when to actually run using Calendar

Categories

Resources