Class: HornetQ::Client::ServerPattern

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

Overview

Create a Server following the ServerPattern for receiving requests and replying to arbitrary queues Create an instance of this class per thread

Instance Method Summary collapse

Constructor Details

#initialize(session, request_queue, timeout) ⇒ ServerPattern

Returns a new instance of ServerPattern.



6
7
8
9
10
11
# File 'lib/hornetq/client/server_pattern.rb', line 6

def initialize(session, request_queue, timeout)
  @session = session
  @consumer = session.create_consumer(request_queue)
  @producer = session.create_producer
  @timeout = timeout
end

Instance Method Details

#closeObject

Close out resources



42
43
44
45
# File 'lib/hornetq/client/server_pattern.rb', line 42

def close
  @consumer.close if @consumer
  @producer.close if @producer
end

#reply(request_message, reply_message) ⇒ Object

Send a reply to the received request message

request: is the message received
reply:   is the message to send to the client


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hornetq/client/server_pattern.rb', line 28

def reply(request_message, reply_message)
  if request_message.request?
    # Reply should have same durability as request
    reply_message.durable = request_message.durable?
    reply_to = request_message.getSimpleStringProperty(Java::OrgHornetqCoreClientImpl::ClientMessageImpl::REPLYTO_HEADER_NAME);
    # Send request message id back in reply message for correlation purposes
    reply_message.user_id = request_message.user_id
    @producer.send(reply_to, reply_message)
    #puts "Sent reply to #{reply_to.to_s}: #{reply_message.inspect}"
  end
  request_message.acknowledge
end

#run(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hornetq/client/server_pattern.rb', line 13

def run(&block)
  while request_message = @consumer.receive(@timeout)
    # Block should return a message reply object, pass in request
    # TODO: ensure..
    reply_message = block.call(request_message)
  
    # Send a reply?
    reply(request_message, reply_message) if request_message.request?
    request_message.acknowledge
  end
end