Class: FreeMessageQueue::QueueManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fmq/queue_manager.rb

Overview

The queue manager is one of the core components of the system. This component manages the queues by pathname and checks on the corresponding constraints. Every queue that is created by this queue manager will get a reference (manager) for later use.

Constant Summary collapse

DEFAULT_QUEUE_CLASS =

this is the default queue class if no other is specified this class will be created when setting up a queue

FreeMessageQueue::SyncronizedQueue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auto_create_queues = true, &block) ⇒ QueueManager

setup the queue manager using the configuration from the configuration file (which is basically a hash)



66
67
68
69
70
71
# File 'lib/fmq/queue_manager.rb', line 66

def initialize(auto_create_queues = true, &block)
  @queues = {}
  @log = FreeMessageQueue.logger
  @auto_create_queues = auto_create_queues
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#auto_create_queues=(value) ⇒ Object (writeonly)

true to let the queue manager create a queue automaticly



58
59
60
# File 'lib/fmq/queue_manager.rb', line 58

def auto_create_queues=(value)
  @auto_create_queues = value
end

Instance Method Details

#auto_create_queues?Boolean

returns if the creation of queues should be done on demand (if someone sends a post to a queue)

Returns:

  • (Boolean)


75
76
77
# File 'lib/fmq/queue_manager.rb', line 75

def auto_create_queues?
  @auto_create_queues == true
end

#delete_queue(name) ⇒ Object

Delete the queue by name (path)



93
94
95
96
97
98
99
100
101
102
# File 'lib/fmq/queue_manager.rb', line 93

def delete_queue(name)
  if queue_exists? name
    @log.info("[QueueManager] Delete queue '#{name}' with #{queue(name).size} messages")
    queue(name).clear if queue(name).respond_to? :clear
    @queues.delete name
    true
  else
    raise QueueManagerException.new("[QueueManager] There is no queue '#{name}'", caller)
  end
end

#poll(name) ⇒ Object Also known as: get

This returns one message from the passed queue



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fmq/queue_manager.rb', line 105

def poll(name)
  if queue_exists? name
    @log.debug("[QueueManager] Poll from queue '#{name}' with #{queue(name).size} messages")
    if queue(name).respond_to? :poll
      queue_item = queue(name).poll
    else
      raise QueueManagerException.new("[QueueManager] You can't poll from queue '#{name}'", caller)
    end
  else
    raise QueueManagerException.new("[QueueManager] There is no queue '#{name}'", caller)
  end
end

#put(name, message) ⇒ Object Also known as: post

Puts a message (data) to the queue and checks if the constraints are vaild otherwise it will raise a QueueManagerException. If auto_create_queues is set to true the queue will be generated if there isn’t a queue with the passed name (path). Otherwise it will raise a QueueManagerException if the passed queue doesn’t exists.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fmq/queue_manager.rb', line 124

def put(name, message)
  # check for auto createing queues if they are not available
  unless queue_exists? name
    # only auto create queues if it is configured
    if auto_create_queues?
      setup_queue(name) 
    else
      raise QueueManagerException.new("[QueueManager] There is no queue '#{name}'", caller)
    end
  end
   
  @log.debug("[QueueManager] put message to queue '#{name}' with #{queue(name).size} messages")
  if queue(name).respond_to? :put
    queue(name).put(message)
  else
    raise QueueManagerException.new("[QueueManager] You can't put to queue '#{name}'", caller)
  end
end

#queue(name) ⇒ Object

returns the queue qith the passed name



156
157
158
# File 'lib/fmq/queue_manager.rb', line 156

def queue(name)
  return @queues[name]
end

#queue_exists?(name) ⇒ Boolean

Is the name (path) of the queue in use allready

Returns:

  • (Boolean)


151
152
153
# File 'lib/fmq/queue_manager.rb', line 151

def queue_exists?(name)
  !queue(name).nil?
end

#queuesObject

Returns the names (paths) of all queues managed by this queue manager



146
147
148
# File 'lib/fmq/queue_manager.rb', line 146

def queues
  @queues.keys
end

#setup_queue(path, queue_class = nil, &block) ⇒ Object

create a queue using a block. The block can be used to set configuration options for the queue



81
82
83
84
85
86
87
88
89
90
# File 'lib/fmq/queue_manager.rb', line 81

def setup_queue(path, queue_class = nil, &block)
  check_queue_name(path)
  queue_class ||= DEFAULT_QUEUE_CLASS
  queue_class = FreeMessageQueue::const_get(queue_class) if queue_class.class == String
  queue_object = queue_class.new(self)
  block.call(queue_object) if block_given?
  @queues[path] = queue_object
  @log.info("[QueueManager] Create queue '#{path}' {type: #{queue_class}, max_messages: #{queue_object.max_messages}, max_size: #{queue_object.max_size}}")
  return queue_object
end