Class: OnStomp::Failover::Client

Inherits:
Object
  • Object
show all
Includes:
FailoverConfigurable, FailoverEvents, Interfaces::FrameMethods
Defined in:
lib/onstomp/failover/client.rb

Overview

A failover client that wraps multiple clients and maintains a connection to one of these clients. Frames are sent to the currently connected client. If the connection is lost, a failover client will automatically reconnect to another client in the pool, re-transmit any necessary frames and resume operation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interfaces::FrameMethods

#abort, #ack, #beat, #begin, #commit, #nack, #send, #subscribe, #unsubscribe

Methods included from FailoverEvents

#bind_client_event, create_client_event_method, #on_connection_closed, #on_connection_died, #on_connection_established, #on_connection_terminated, #trigger_failover_event, #trigger_failover_retry

Methods included from Interfaces::EventManager

#bind_event, #event_callbacks, included, #trigger_event

Methods included from FailoverConfigurable

included

Constructor Details

#initialize(uris, options = {}) ⇒ Client

Returns a new instance of Client.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/onstomp/failover/client.rb', line 37

def initialize(uris, options={})
  if uris.is_a?(Array)
    uris = "failover:(#{uris.map { |u| u.to_s }.join(',')})"
  end
  @client_mutex = Mutex.new
  @uri = URI.parse(uris)
  configure_configurable options
  create_client_pool
  @active_client = nil
  @connection = nil
  @frame_buffer = buffer.new self
  @disconnecting = false
  @client_ready = false
end

Instance Attribute Details

#active_clientObject (readonly)

Returns the value of attribute active_client.



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def active_client
  @active_client
end

#client_poolObject (readonly)

Returns the value of attribute client_pool.



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def client_pool
  @client_pool
end

#connectionObject (readonly)

Returns the value of attribute connection.



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def connection
  @connection
end

#frame_bufferObject (readonly)

Returns the value of attribute frame_buffer.



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def frame_buffer
  @frame_buffer
end

#uriObject (readonly)

Returns the value of attribute uri.



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def uri
  @uri
end

Instance Method Details

#bufferClass

The class to use when instantiating a new frame buffer. Defaults to Buffers::Written

Returns:

  • (Class)


20
# File 'lib/onstomp/failover/client.rb', line 20

attr_configurable_buffer :buffer

#connectself

Connects to one of the clients in the #client_pool

Returns:

  • (self)


67
68
69
70
71
72
73
# File 'lib/onstomp/failover/client.rb', line 67

def connect
  @disconnecting = false
  unless reconnect
    raise OnStomp::Failover::MaximumRetriesExceededError
  end
  self
end

#connected?true, ...

Returns true if there is an #active_client and it is connected.

Returns:

  • (true, false, nil)


55
56
57
# File 'lib/onstomp/failover/client.rb', line 55

def connected?
  active_client && active_client.connected?
end

#disconnect(*args, &block) ⇒ Object

Ensures that a connection is properly established, then invokes disconnect on the #active_client



77
78
79
80
81
82
# File 'lib/onstomp/failover/client.rb', line 77

def disconnect *args, &block
  return unless active_client
  @disconnecting = true
  Thread.pass until @client_ready
  active_client.disconnect *args, &block
end

#poolClass

The class to use when instantiating a new #client_pool. Defaults to Pools::RoundRobin

Returns:

  • (Class)


16
# File 'lib/onstomp/failover/client.rb', line 16

attr_configurable_pool :pool

#randomizetrue, false

Whether or not to randomize the #client_pool before connecting through any of its clients. Defaults to false

Returns:

  • (true, false)


33
# File 'lib/onstomp/failover/client.rb', line 33

attr_configurable_bool :randomize, :default => false

#retry_attemptsFixnum

The maximum number of times to retry connecting during a reconnect loop. A non-positive number will force the failover client to try to reconnect indefinitely. Defaults to 0

Returns:

  • (Fixnum)


29
# File 'lib/onstomp/failover/client.rb', line 29

attr_configurable_int :retry_attempts, :default => 0

#retry_delayFixnum

The delay in seconds to wait between connection retries. Defaults to 10.

Returns:

  • (Fixnum)


24
# File 'lib/onstomp/failover/client.rb', line 24

attr_configurable_int :retry_delay, :default => 10

#transmit(frame, cbs = {}) ⇒ OnStomp::Components::Frame?

Transmits a frame to the #active_client if one exists.

Returns:



61
62
63
# File 'lib/onstomp/failover/client.rb', line 61

def transmit frame, cbs={}
  active_client && active_client.transmit(frame, cbs)
end