Method: SMQueue::StompAdapter#get
- Defined in:
- lib/smqueue/adapters/stomp.rb
#get(headers = {}, &block) ⇒ Object
get message from queue
-
if block supplied, loop forever and yield(message) for each message received
default headers are:
:ack => "client"
:client_id => configuration.client_id
:subscription_name => configuration.subscription_name
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/smqueue/adapters/stomp.rb', line 250 def get(headers = {}, &block) self.connect SMQueue.dbg { [:smqueue, :get, headers].inspect } subscription_headers = {"ack" => "client", "activemq.prefetchSize" => 1 } if client_id = configuration.client_id subscription_headers["client_id"] = client_id end if sub_name = configuration.subscription_name subscription_headers["subscription_name"] = sub_name end # if a client_id is supplied, then user wants a durable subscription # N.B. client_id must be unique for broker subscription_headers.update(headers) #p [:subscription_headers_before, subscription_headers] subscription_headers = normalize_keys(subscription_headers) if configuration.durable and client_id = configuration.client_id || subscription_headers["client_id"] subscription_name = configuration.subscription_name || subscription_headers["subscription_name"] || client_id # activemq only subscription_headers["activemq.subscriptionName"] = subscription_name # JMS subscription_headers["durable-subscriber-name"] = subscription_name end #p [:subscription_headers_after, subscription_headers] destination = configuration.name SMQueue.dbg { [:smqueue, :get, :subscribing, destination, :subscription_headers, subscription_headers].inspect } connection.subscribe destination, subscription_headers = nil SMQueue.dbg { [:smqueue, :get, :subscription_headers, subscription_headers].inspect } begin # TODO: refactor this if block_given? SMQueue.dbg { [:smqueue, :get, :block_given].inspect } # todo: change to @running - (and set to false from exception handler) # also should check to see if anything left to receive on connection before bailing out while true SMQueue.dbg { [:smqueue, :get, :receive].inspect } # block until message ready = connection.receive SMQueue.dbg { [:smqueue, :get, :received, ].inspect } case .command when "ERROR" SMQueue.dbg { [:smqueue, :get, :ERROR, ].inspect } when "RECEIPT" SMQueue.dbg { [:smqueue, :get, :RECEIPT, ].inspect } else SMQueue.dbg { [:smqueue, :get, :yielding].inspect } if !(.headers["message-id"]) yield() end SMQueue.dbg { [:smqueue, :get, :message_seen, .headers["message-id"]].inspect } .headers["message-id"] SMQueue.dbg { [:smqueue, :get, :returned_from_yield_now_calling_ack].inspect } ack(subscription_headers, ) SMQueue.dbg { [:smqueue, :get, :returned_from_ack].inspect } end end else SMQueue.dbg { [:smqueue, :get, :single_shot].inspect } = connection.receive SMQueue.dbg { [:smqueue, :get, :received, ].inspect } if !(.command == "ERROR" or .command == "RECEIPT") SMQueue.dbg { [:smqueue, :get, :message_seen, .headers["message-id"]].inspect } .headers["message-id"] SMQueue.dbg { [:smqueue, :get, :ack, ].inspect } ack(subscription_headers, ) SMQueue.dbg { [:smqueue, :get, :returned_from_ack].inspect } end end rescue Object => e SMQueue.dbg { [:smqueue, :get, :exception, e].inspect } handle_error e, "Exception in SMQueue#get: #{e.}", caller ensure SMQueue.dbg { [:smqueue, :get, :ensure].inspect } SMQueue.dbg { [:smqueue, :unsubscribe, destination, subscription_headers].inspect } connection.unsubscribe destination, subscription_headers SMQueue.dbg { [:smqueue, :disconnect].inspect } connection.disconnect end SMQueue.dbg { [:smqueue, :get, :return].inspect } end |