Class: Qpid::Messaging::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/qpid_messaging/duration.rb

Overview

A Duration represents a period of time in milliseconds

Named Durations

The following named Durations are available as symbols:

FOREVER

The maximum integer value for the platform. Effectively this will wait forever.

IMMEDIATE

An alias for 0 milliseconds.

SECOND

An alias for 1,000 milliseconds.

MINUTE

And alias for 60,000 millisecons.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length) ⇒ Duration

Creates a Duration with the specified length, in milliseconds.

Options

  • length - The duration in milliseconds.

Examples

# creates a duration of 15 seconds
# REMEMBER: Duration deals in milliseconds
delay = Qpid::Messaging::Duration.new 15000


57
58
59
# File 'lib/qpid_messaging/duration.rb', line 57

def initialize length
  @duration_impl = Cqpid::Duration.new length
end

Class Method Details

.add_item(key, value) ⇒ Object

:nodoc:



109
110
111
112
# File 'lib/qpid_messaging/duration.rb', line 109

def self.add_item(key, value) # :nodoc:
  @hash ||= {}
  @hash[key] = Duration.new value
end

.const_missing(key) ⇒ Object

:nodoc:



114
115
116
# File 'lib/qpid_messaging/duration.rb', line 114

def self.const_missing(key) # :nodoc:
  @hash[key]
end

Instance Method Details

#*(factor) ⇒ Object

Multiplies the duration of the Duration and returns a new instance.

Raises exceptions on a negative factor. Returns Qpid::Messaging::Duration::IMMEDIATE when the factor is 0.

Examples

# return a duration that is 2 minutes (120,000 ms)
twominutes = Qpid::Messaging::Duration::MINUTE * 2


103
104
105
106
107
# File 'lib/qpid_messaging/duration.rb', line 103

def *(factor)
  raise TypeError.new "Factors must be non-zero positive values" if factor < 0
  return Qpid::Messaging::Duration::IMMEDIATE if factor.zero?
  Qpid::Messaging::Duration.new((self.milliseconds * factor).floor)
end

#duration_implObject

:nodoc:



61
62
63
# File 'lib/qpid_messaging/duration.rb', line 61

def duration_impl # :nodoc:
  @duration_impl
end

#millisecondsObject

Returns the period of time in milliseconds.

Examples

# doubling growth in waiting for messages in a loop
do loop
  set the base duration waiting length
  timeout = Qpid::Messaging::Duration::SECOND
  msg = nil
  # loop until we receive a message
  while msg.nil?
    puts "Waiting #{timeout.milliseconds}ms"
    msg = recv.get timeout
    # if nothing was received, double the duration
    if msg.nil?
      # double out timeout
      timeout = timeout * 2
    else
      # do something with the message
      puts "Received: #{msg.content}"
    end
  end
end


89
90
91
# File 'lib/qpid_messaging/duration.rb', line 89

def milliseconds
  @duration_impl.getMilliseconds
end