Example index

Scheduling UM Timers

Ultra Messaging has a feature where developers can schedule timer function callbacks via the API, where the desired function is executed in a desired number of milliseconds from when it was scheduled. The callback itself must be created by the developer, and it is executed by the UM context thread or via a UM Event Queue, if an Event Queue is passed in via the API.

In this example, the application will schedule a timer callback to execute in 1 second. The main thread that scheduled the timer will wait for the timer to execute, then clean up the application and exit

There is one program source file:

Program explanation: ScheduleTimers.java

Initializing the Context

Standard context creation code. Creating and scheduling timers only required an LBM context, no sources or receivers are needed.

00016                  /*** Initialization: create necessary UM objects. ***/
00017                  try {
00018                          ctx = new LBMContext();
00019                  }
00020                  catch(LBMException ex)
00021                  {
00022                          System.Console.Error.WriteLine("Error initializing LBM objects: " + ex.Message);
00023                          System.Environment.Exit(1);
00024                  }

Creating a Callback

The callback class defined below is the application callback that will be invoked when the scheduled timer expires. This callback will be executed on the UM context thread (at least for the purpose of this example it will be executed on the context thread.

00050          public class TimerCallback 
00051          {
00052                  public void onExpiration(Object cbArg)
00053                  {
00054                      System.Console.Out.WriteLine("Timer executed. Set wait to 0 so application can cleanly exit");
00055                      wait = 0;
00056                  }
00057          }

Note that inside the callback, the global "wait" variable is set to 0, this allows the main thread to stop tight-looping and exit.

Scheduling the Timer Callback

Here the application is actually initializing and scheduling the callback timer, set to be executed in 1000 milliseconds. The return value should be saved by the application in the event that the application wishes to cancel the timer before it executes. Special care must be taken when cancelling timer callbacks, for more information please see

00026                  TimerCallback cb = new TimerCallback();
00027                  LBMTimer timer = new LBMTimer(ctx,1000,(LBMTimerCallback)cb.onExpiration,null);

Wait for Timer

For the purpose of this example application, it is using the global variable "wait" as a semaphore essentially. Once the timer executes, wait will be set to 0 and the application can cleanly exit by deleting the context.

00029                  while (wait == 1)
00030                  {
00031                          try {
00032                                  System.Threading.Thread.Sleep(500);
00033                          } 
00034                          catch (Exception eex)
00035                          {
00036                              System.Console.Error.WriteLine("Error System.Threading.Thread.Sleep() exception:  " + eex.Message);
00037                              System.Environment.Exit(1);
00038                          }
00039                  }