Class: FreeMessageQueue::RoundRobinQueue

Inherits:
BaseQueue
  • Object
show all
Defined in:
lib/fmq/queues/round_robin.rb

Overview

This queue distributes the polls and puts to different queues. It uses a round robin, so each queue will be called in order every time. E.g. if you have three queues specified in the redirect_to statement and use the poll method it will poll the first queue. If you poll the second time the RoundRobinQueue will poll the second queue and if you poll the fourth time the RoundRobinQueue will again poll the first queue.

queue_manager = FreeMessageQueue::QueueManager.new(true) do
  setup_queue "/fmq_test/test1", FreeMessageQueue::RoundRobinQueue do |q|
    q.redirect_to ["/fmq_test/test1", "/fmq_test/test2"]
    # one can optionally configure what methods are allowed
    # by default it is [:put, :poll]
    q.allow :poll
  end
end

Constant Summary

Constants inherited from BaseQueue

BaseQueue::INFINITE

Instance Attribute Summary

Attributes inherited from BaseQueue

#bytes, #manager, #max_messages, #max_size, #size

Instance Method Summary collapse

Methods inherited from BaseQueue

#empty?

Constructor Details

#initialize(manager) ⇒ RoundRobinQueue

Returns a new instance of RoundRobinQueue.



36
37
38
39
40
# File 'lib/fmq/queues/round_robin.rb', line 36

def initialize(manager)
  super(manager)
  @allow = [:poll, :put]
  @queue_index = -1 # as starting point will be 0 later by using next_queue
end

Instance Method Details

#allow(val) ⇒ Object

CONFIGURATION OPTION allow actions :put or :poll. You can pass and array for both (*[:poll, :put]*) or just one :put



60
61
62
# File 'lib/fmq/queues/round_robin.rb', line 60

def allow(val)
  @allow = val
end

#pollObject



42
43
44
45
46
47
48
# File 'lib/fmq/queues/round_robin.rb', line 42

def poll
  if allowed? :poll
    manager.poll(@redirect_to[next_queue])
  else
    raise QueueException.new("[RoundRobinQueue] you can't poll from this queue", caller)
  end
end

#put(message) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/fmq/queues/round_robin.rb', line 50

def put(message)
  if allowed? :put
    manager.put(@redirect_to[next_queue], message)
  else
    raise QueueException.new("[RoundRobinQueue] you can't put to this queue", caller)
  end
end

#redirect_to(val) ⇒ Object

CONFIGURATION OPTION specifiy the queues that should be used (must be an array)



66
67
68
# File 'lib/fmq/queues/round_robin.rb', line 66

def redirect_to(val)
  @redirect_to = val
end