Back to Basic Time Service Examples Page
This example makes use of the Time Service to determine the current time and to wait for a time interval. It illustrates the use of several Time Service operations.
The objects defined in the Time Service are UTO objects (which represent a moment in time), TIO objects (which represent a time interval) and the time server itself, which is used to create UTOs and TIOs.
From this example, you will learn how to:
main() creates an instance of this class, initialises it by calling
the init method, and runs the example by calling the
go method.
init() obtains a reference to the time service and to the ORB.
go() runs the example by calling the printCurrentTime
method, the doTimedWorkDuringInterval method and the
doTimedWorkUntilTime method.
printCurrentTime() queries the Time Service for the current time. It
then displays the individual components of this
time, and uses the time utility class to convert the time into a Java Date
object, which it then displays in natural language.
doTimedWorkDuringInterval() creates an interval representing time
between the present and a number of seconds into the future, and repeatedly
calls the doWork method while the current time falls within that
interval.
doTimedWorkUntilTime() creates a time object representing
a moment a number of seconds in the future, and repeatedly calls the
doWork
method until that moment has passed. This method is merely a different expression
of doTimedWorkDuringInterval.
doWork() displays the number of seconds elapsed
in the current minute of (system) time, sleeps for half a second and then
displays the (new) number of seconds elapsed of the current minute. This
indicates the entry and exit times of the method.
To run the example, first make sure that the OpenFusion Time Service is running on your network. Then run the TimeExample class using the command:
run com.prismt.cos.CosTime.examples.Time.TimeExample