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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rabbitmq_client.rb', line 140

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.



136
137
138
# File 'lib/rabbitmq_client.rb', line 136

def channel
  @channel
end

#connectionObject (readonly)

Returns the value of attribute connection.



137
138
139
# File 'lib/rabbitmq_client.rb', line 137

def connection
  @connection
end

Instance Method Details

#connectObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/rabbitmq_client.rb', line 160

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)


177
178
179
# File 'lib/rabbitmq_client.rb', line 177

def connected?
  @connection != nil
end

#disconnectObject



171
172
173
174
175
# File 'lib/rabbitmq_client.rb', line 171

def disconnect
  @channel.close
  @connection.close
  @connection = nil
end

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



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

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

#queue(name, durable = false) ⇒ Object



181
182
183
# File 'lib/rabbitmq_client.rb', line 181

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