Skip to content

Instantly share code, notes, and snippets.

@daninfpj
Created October 5, 2017 17:16
Show Gist options
  • Save daninfpj/04e70a38395fe3bb0a238935285131a0 to your computer and use it in GitHub Desktop.
Save daninfpj/04e70a38395fe3bb0a238935285131a0 to your computer and use it in GitHub Desktop.
ActiveMQ Consumer Ruby
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
</transportConnectors>
#!/usr/bin/env ruby
require 'bundler/setup'
require 'stp'
require 'stp/consumer'
Stp::Consumer.new({
stomp_host: ENV['STOMP_HOST'] || 'localhost',
stomp_port: ENV['STOMP_PORT'] || 61613,
abonos_queue: ENV['ABONOS_QUEUE'] || '/queue/abonos',
ordenes_queue: ENV['ORDENES_QUEUE'] || '/queue/ordenes'
}).run
require 'stomp'
module Stp
class Consumer
def initialize(options)
@options = options
@abonos_queue = options[:abonos_queue]
@ordenes_queue = options[:ordenes_queue]
end
def run
@client = Stomp::Client.new(
hosts: [{
host: @options[:stomp_host],
port: @options[:stomp_port]
}],
connect_headers: { 'client-id': 'stp-ruby' }
)
Stp.logger.info 'Connected to ActiveMQ'
@client.subscribe(@abonos_queue, { ack: 'client' }) do |message|
Stp.logger.debug "Message received on #{@abonos_queue}"
Stp.logger.debug message.body
@client.acknowledge(message)
end
Stp.logger.info "Subscribed to #{@abonos_queue}"
@client.subscribe(@ordenes_queue, { ack: 'client' }) do |message|
Stp.logger.debug "Message received on #{@ordenes_queue}"
Stp.logger.debug message.body
@client.acknowledge(message)
end
Stp.logger.info "Subscribed to #{@ordenes_queue}"
begin
@client.join
rescue StandardError => e
Stp.logger.error e.message
rescue Interrupt
rescue SignalException
ensure
@client.close
Stp.logger.info 'Connection closed'
end
end
end
end
spec.bindir = "bin"
spec.executables = ["consumer"]
spec.add_runtime_dependency "stomp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment