Paul Sturm > Tibco-Rv-1.15 > Tibco::Rv::Timer

Download:
Tibco-Rv-1.15.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.04   Source  

NAME ^

Tibco::Rv::Timer - Tibco Timer event object

SYNOPSIS ^

   my ( $timer ) = $rv->createTimer( interval => 10, sub
   {
      print "Timer event happened!\n";
   } );

   $timer->interval( 5 );

   $timer->onEvent( );

DESCRIPTION ^

A Tibco::Rv::Timer fires an event after every specified interval. It is a subclass of Tibco::Rv::Event, so Event methods are available to Timers (documentation on Event methods are reproduced here for convenience).

CONSTRUCTOR ^

$timer = new Tibco::Rv::Timer( %args )
   %args:
      queue => $queue,
      interval => $interval,
      callback => sub { ... }

Creates a Tibco::Rv::Timer. If not specified, queue defaults to the Default Queue, interval defaults to 1, and callback defaults to:

   sub { print "Timer fired\n" }

Create a Timer to cause an event to fire after every $interval seconds. The $interval can specify fractions of a second. When $queue dispatches such an event, it triggers the given callback.

METHODS ^

$interval = $timer->interval

Returns the number of seconds in the interval between firings of the Timer's event.

$timer->interval( $interval ) (or $timer->resetTimerInterval( $interval ))

Set the interval at which the next Timer event will be dispatched.

$queue = $timer->queue

Returns the queue on which this Timer's events will be dispatched.

$callback = $timer->callback

Returns the callback code reference.

$timer->onEvent

Trigger an event directly. The event will be processed as if it was triggered via the event queue.

$timer->DESTROY

Cancels the Timer. Called automatically when $timer goes out of scope. Calling DESTROY more than once has no effect.

OVERRIDING EVENT CALLBACK ^

As an alternative to passing in a callback function to the constructor, there is another way to handle events. You can subclass Tibco::Rv::Timer and override the onEvent method, as follows:

   package MyTimer;
   use base qw/ Tibco::Rv::Timer /;

   sub new
   {
      my ( $proto, %args ) = @_;
      my ( $self ) = $proto->SUPER::new( %args );
      # your initialization code
      return $self;
   }

   sub onEvent
   {
      my ( $self ) = @_;
      # process event here
      # $self->queue, $self->interval are available
   }

   # any other implementation code for your class

   1;

The Tibco::Rv::Event onEvent method simply calls the callback, so overriding onEvent allows you to process the event however you want, and you can just not use the callback.

The advantages of this method of handling events are: it is more object-oriented; you have access to the queue and interval via the $self accessor methods; and, you can have more elaborate processing of timed events without having to shove it all into one callback.

You can use your subclassed Timer as follows:

   use Tibco::Rv;
   use MyTimer;

   my ( $rv ) = new Tibco::Rv;
   my ( $myTimer ) = new MyTimer( interval => 4 );
   $rv->start;

AUTHOR ^

Paul Sturm <sturm@branewave.com>

syntax highlighting: