Class: FreeMessageQueue::ClientQueue

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

Overview

Here you can find the client side api for the free message queue. This api is build using the net/http facilitys

Some sample usage of the client api:

require "fmq"

# queue adress
QA = "http://localhost/webserver_agent/messages"
queue = FreeMessageQueue::ClientQueue.new(QA)

# pick one message
message = queue.poll()
puts " == URGENT MESSSAGE == " if message.option["Priority"] == "high"
puts message.payload

# put an urgent message on the queue e.g.in yaml
payload = "message:
  title: server don't answer a ping request
  date_time: 2008-06-01 20:19:28
  server: 10.10.30.62
"

message = FreeMessageQueue::Message.new(payload, "application/yaml")
queue.put(message)

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ClientQueue

create a connection to a queue (by url)



50
51
52
# File 'lib/fmq/client.rb', line 50

def initialize(url)
  @url = URI.parse(url)
end

Instance Method Details

#bytesObject

return the size of the queue in bytes



98
99
100
# File 'lib/fmq/client.rb', line 98

def bytes
  head["QUEUE_BYTES"].to_i
end

#pollObject Also known as: get

this returns one message from the queue as a string



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fmq/client.rb', line 55

def poll()
  res = Net::HTTP.start(@url.host, @url.port) do |http|
    http.get(@url.path)
  end
  
  message = Message.new(res.body, res["CONTENT-TYPE"])
  
  res.each_key do |option_name|
    if option_name.upcase.match(/MESSAGE_([a-zA-Z][a-zA-Z0-9_\-]*)/)
      message.option[$1] = res[option_name]
    end
  end
  
  return message
end

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

this puts one message to the queue



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fmq/client.rb', line 74

def put(message)
  header = {}
  header["CONTENT-TYPE"] = message.content_type
  
  # send all options of the message back to the client
  if message.respond_to?(:option) && message.option.size > 0
    for option_name in message.option.keys
      header["MESSAGE_#{option_name}"] = message.option[option_name].to_s
    end
  end
  
  Net::HTTP.start(@url.host, @url.port) do |http|
    http.post(@url.path, message.payload, header)
  end
end

#sizeObject

return the size (number of messages) of the queue



93
94
95
# File 'lib/fmq/client.rb', line 93

def size
  head["QUEUE_SIZE"].to_i
end