I want to publish messages to a direct exchange, so I have the following function:
def publish_message_to_direct(server_params, exchange, payload)
AMQP.start(server_params) do |connection|
channel = AMQP::Channel.new(connection)
exchange = channel.direct(exchange,:durable=>true)
exchange.publish(payload)
puts payload
connection.close do
EM.stop { exit }
end
end
end
After I call the function, it prints the payload to the terminal, but does not publish the message to the exchange. However, if I add a SIGTRAP like this then it publishes the message:
.
.
.
Signal.trap("INT") do
connection.close do
EM.stop { exit }
end
end
How do I get EM to stop without having to send it this signal? IOW, I just want it to publish the message and return.
Well, I just read the documentation and found the section on publishing one-off messages:
So the code as follows now works:
def publish_message_to_direct(server_params, exchange, payload)
AMQP.start(server_params) do |connection|
channel = AMQP::Channel.new(connection)
exchange = channel.direct(exchange,:durable=>true)
exchange.publish(payload, :nowait => false) do
puts payload
connection.close do
EM.stop
end
end
end
end
Apparently, the difference was adding the closing event inside a block passed to the publish function and using the option :nowait => false
. Hope this helps someone else with the same question.