Class: RabbitMQClient

Inherits:
Object
  • Object
show all
Includes:
ObjectSpace
Defined in:
lib/rabbitmq_client.rb

Defined Under Namespace

Classes: Exchange, Queue, QueueConsumer, RabbitMQClientError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RabbitMQClient

Instance Methods



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rabbitmq_client.rb', line 153

def initialize(options={})
  # server address
  @host = options[:host] || '127.0.0.1'
  @port = options[:port] || 5672
  
  # login details
  @username = options[:username] || 'guest'
  @password = options[:password] || 'guest'
  @vhost = options[:vhost] || '/'
  
  # queues and exchanges
  @queues = {}
  @exchanges = {}
  
  connect unless options[:no_auto_connect]
  # Disconnect before the object is destroyed
  define_finalizer(self, lambda {|id| self.disconnect if self.connected? })
  self
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



149
150
151
# File 'lib/rabbitmq_client.rb', line 149

def channel
  @channel
end

#connectionObject (readonly)

Returns the value of attribute connection.



150
151
152
# File 'lib/rabbitmq_client.rb', line 150

def connection
  @connection
end

Instance Method Details

#connectObject



173
174
175
176
177
178
179
180
181
182
# File 'lib/rabbitmq_client.rb', line 173

def connect
  params = ConnectionParameters.new
  params.set_username(@username)
  params.set_password(@password)
  params.set_virtual_host(@vhost)
  params.set_requested_heartbeat(0)
  conn_factory = ConnectionFactory.new(params)
  @connection = conn_factory.new_connection(@host, @port)
  @channel = @connection.create_channel
end

#connected?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/rabbitmq_client.rb', line 191

def connected?
  @connection != nil
end

#disconnectObject



184
185
186
187
188
189
# File 'lib/rabbitmq_client.rb', line 184

def disconnect
  @queues.values.each { |q| q.unbind }
  @channel.close
  @connection.close
  @connection = nil
end

#exchange(name, type = 'fanout', durable = false) ⇒ Object



199
200
201
# File 'lib/rabbitmq_client.rb', line 199

def exchange(name, type='fanout', durable=false)
  @exchanges[name] ||= Exchange.new(name, type, @channel, durable)
end

#queue(name, durable = false) ⇒ Object



195
196
197
# File 'lib/rabbitmq_client.rb', line 195

def queue(name, durable=false)
  @queues[name] ||= Queue.new(name, @channel, durable)
end