Class: Ap4r::Mongrel::Ap4rSubscribeMessageHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/ap4r/mongrel.rb

Overview

This class is an experimental implementation of RESTified message API. Subscribe to queue:

using HTTP POST
a message is subscribed as HTTP body

options are sent as HTTP header which header name is "X-AP4R"
(now not supported)

url consists of prefix ("/subscribes") and queue name

response body is now return value of ReliableMsg#inspct
In the future, it will be possible to assign the format by the request header X-AP4R

=== Rrequest example ===
POST /subscribes/queue.test HTTP/1.1

=== Response example ===
HTTP/1.1 200 Ok
Content-Type: text/plain
Date: The, 11 Dec 2007 17:17:11 GMT

#<ReliableMsg::Message:0x320ec90 @headers={:priority=>0, :created=>1197628231, :expires=>nil, :delivery=>:best_effort, :id=>\"856016b0-8c5d-012a-79f3-0016cb9ad524\", :max_deliveries=>5}, @object=\"hoge\", @id=\"856016b0-8c5d-012a-79f3-0016cb9ad524\">

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Ap4rSubscribeMessageHandler

Returns a new instance of Ap4rSubscribeMessageHandler.



225
226
227
228
# File 'lib/ap4r/mongrel.rb', line 225

def initialize(options)
  @tick = Time.now
  @queues = {}
end

Instance Method Details

#log_threads_waiting_for(event) ⇒ Object



277
278
279
280
281
# File 'lib/ap4r/mongrel.rb', line 277

def log_threads_waiting_for(event)
  if Time.now - @tick > 10
    @tick = Time.now
  end
end

#make_header(x_ap4r_header) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ap4r/mongrel.rb', line 262

def make_header(x_ap4r_header)
  header = {}
  if x_ap4r_header
    x_ap4r_header.split(',').map do |e|
      key, value = e.strip.split('=')
      if %w(dispatch_mode target_method delivery).include?(key)
        header[key.to_sym] = value.to_sym
      else
        header[key.to_sym] = value
      end
    end
  end
  header
end

#process(request, response) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ap4r/mongrel.rb', line 230

def process(request, response)
  if response.socket.closed?
    return
  end

  queue_name = request.params[::Mongrel::Const::PATH_INFO][1..-1]
  # header = make_header(request.params["HTTP_X_AP4R"]) # Todo: assign as constant 2007/11/27 by kiwamu

  if "POST".include? request.params[::Mongrel::Const::REQUEST_METHOD]
    begin
      q = if @queues.key? queue_name
            @queues[queue_name]
          else
            @queues[queue_name] = ::ReliableMsg::Queue.new(queue_name)
          end
      mes = q.get

      response.start(200) do |head, out|
        head['Content-Type'] = 'text/plain'
        out.write mes.inspect
      end
    rescue
      response.start(500) do |head, out|
        head['Content-Type'] = 'text/plain'
        out.write "Failed to get message for #{queue_name}"
      end
    end
  else
    raise "HTTP method is not POST..."
  end
end

#reload!Object

Does the internal reload for Rails. It might work for most cases, but sometimes you get exceptions. In that case just do a real restart.



285
286
287
288
289
290
# File 'lib/ap4r/mongrel.rb', line 285

def reload!
  begin
    #TODO not implemented 2007/04/09 by shino
    raise "not yet implemented!"
  end
end