Last Updated: February 25, 2016
·
2.087K
· revskill

Ruby and STOMP

A publisher and subscriber in Ruby using ApacheMQ as message queue.

publish.rb

require 'stomp'
@emailclient = Stomp::Connection.open '','',"localhost", 61613, true, {'client-id' => "hpustomp"}
@emailclient.publish('/topic/hpumail', {:to => email, :token => register_confirm.token, :reason => 'register'}.to_json, {:persistent => 'true'})

subscribe.rb

client_id = "hpustomp"
subscription_name = "hpustomp"

stomp_params = {
    :hosts => [
    {:host => "localhost", :port => 61613}
    ],
    :connect_headers => {'client-id' => client_id},
    }

client = Stomp::Client.new stomp_params
client.subscribe "/topic/hpumail", {"ack" => "client", "activemq.subscriptionName" =>   subscription_name} do |message|
logstr = "received: #{message.body} on #{message.headers['destination']}"
begin
    process(message.body) 
    client.acknowledge message
    rescue Exception => e
        puts "Error #{e}"
    end
end
client.join