Module: AMQP

Included in:
MQ, MQ::Exchange, MQ::Header, MQ::Queue
Defined in:
lib/amqp/spec.rb,
lib/amqp.rb,
lib/amqp/spec.rb,
lib/amqp/frame.rb,
lib/amqp/buffer.rb,
lib/amqp/client.rb,
lib/amqp/server.rb,
lib/amqp/version.rb,
lib/amqp/protocol.rb

Overview

:stopdoc: this file was autogenerated on Thu Jul 09 15:17:33 -0700 2009 using amqp-0.8.json (mtime: Sat Jan 03 08:58:13 -0600 2009)

DO NOT EDIT! (edit protocol/codegen.rb instead, and run ‘rake codegen`)

Defined Under Namespace

Modules: BasicClient, Client, Protocol, Server Classes: Buffer, Error, Frame

Constant Summary collapse

HEADER =
"AMQP".freeze
VERSION_MAJOR =
8
VERSION_MINOR =
0
PORT =
5672
RESPONSES =
{
  200 => :REPLY_SUCCESS,
  310 => :NOT_DELIVERED,
  311 => :CONTENT_TOO_LARGE,
  312 => :NO_ROUTE,
  313 => :NO_CONSUMERS,
  403 => :ACCESS_REFUSED,
  404 => :NOT_FOUND,
  405 => :RESOURCE_LOCKED,
  406 => :PRECONDITION_FAILED,
  320 => :CONNECTION_FORCED,
  402 => :INVALID_PATH,
}
FIELDS =
[
  :bit,
  :long,
  :longlong,
  :longstr,
  :octet,
  :short,
  :shortstr,
  :table,
  :timestamp,
]
VERSION =
'0.6.7.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.closingObject (readonly) Also known as: closing?

Returns the value of attribute closing.



12
13
14
# File 'lib/amqp.rb', line 12

def closing
  @closing
end

.connObject (readonly) Also known as: connection

Returns the value of attribute conn.



12
13
14
# File 'lib/amqp.rb', line 12

def conn
  @conn
end

.loggingObject

Returns the value of attribute logging.



11
12
13
# File 'lib/amqp.rb', line 11

def logging
  @logging
end

Class Method Details

.clientObject



49
50
51
# File 'lib/amqp/client.rb', line 49

def self.client
  @client ||= BasicClient
end

.client=(mod) ⇒ Object



53
54
55
56
# File 'lib/amqp/client.rb', line 53

def self.client= mod
  mod.__send__ :include, AMQP
  @client = mod
end

.connect(*args) ⇒ Object



17
18
19
# File 'lib/amqp.rb', line 17

def self.connect *args
  Client.connect *args
end

.fork(workers) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/amqp.rb', line 129

def self.fork workers
  EM.fork(workers) do
    # clean up globals in the fork
    Thread.current[:mq] = nil
    AMQP.instance_variable_set('@conn', nil)

    yield
  end
end

.settingsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/amqp.rb', line 21

def self.settings
  @settings ||= {
    # server address
    :host => '127.0.0.1',
    :port => PORT,

    # login details
    :user => 'guest',
    :pass => 'guest',
    :vhost => '/',

    # connection timeout
    :timeout => nil,

    # logging
    :logging => false,

    # ssl
    :ssl => false
  }
end

.start(*args, &blk) ⇒ Object Also known as: run

Must be called to startup the connection to the AMQP server.

The method takes several arguments and an optional block.

This takes any option that is also accepted by EventMachine::connect. Additionally, there are several AMQP-specific options.

  • :user => String (default ‘guest’)

The username as defined by the AMQP server.

  • :pass => String (default ‘guest’)

The password for the associated :user as defined by the AMQP server.

  • :vhost => String (default ‘/’)

The virtual host as defined by the AMQP server.

  • :timeout => Numeric (default nil)

Measured in seconds.

  • :logging => true | false (default false)

Toggle the extremely verbose logging of all protocol communications between the client and the server. Extremely useful for debugging.

AMQP.start do
  # default is to connect to localhost:5672

  # define queues, exchanges and bindings here.
  # also define all subscriptions and/or publishers
  # here.

  # this block never exits unless EM.stop_event_loop
  # is called.
end

Most code will use the MQ api. Any calls to MQ.direct / MQ.fanout / MQ.topic / MQ.queue will implicitly call #start. In those cases, it is sufficient to put your code inside of an EventMachine.run block. See the code examples in MQ for details.



78
79
80
81
82
83
84
# File 'lib/amqp.rb', line 78

def self.start *args, &blk
  EM.run {
    @conn ||= connect *args
    @conn.callback(&blk) if blk
    @conn
  }
end

.stopObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/amqp.rb', line 118

def self.stop
  if @conn and not @closing
    @closing = true
    @conn.close{
      yield if block_given?
      @conn = nil
      @closing = false
    }
  end
end

.vstart(*args, &blk) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/amqp.rb', line 86

def self.vstart *args, &blk
  opts = Hash[*args.collect]
  opts[:hosts].shuffle.each do |host_port|
    host, port = host_port.split ":"
    opts[:host] = host
    opts[:port] = port if port
    
    begin
      puts "AMQP.run : Try connect to server #{host_port}"
      #start opts, &blk
      
      #EM.error_handler { |e|
      #  puts "Error raised during event loop: #{e.message}"
      #  EM.stop_event_loop
      #}
      EM.run {
        _start opts, &blk
      }
    rescue => e
      puts "AMQP.err : #{e}"
      @conn = nil
    else
      break
    end
  end
  @conn
end