How can I handle a duplicate durable Stomp subscriber?

1k Views Asked by At

I have multiple Ruby processes that start up and try to connect to a topic via a durable subscriber using Stomp.

The first process succeeds, and reads messages (yay).

Subsequent processes fail, and repeatedly try to reconnect.

How can my processes discover that the durable subscriber is already connected, and quit trying to connect?

Possible imaginary code snippet:

begin
  stomp_client.subscribe()
rescue ClientAlreadySubscribedException
  puts "No problem, let's keep doing our other code"
end

Environment:

  • Ruby 1.9.3
  • stompgem 1.3.2

Code:

require 'stomp'

# Connect with durable subscription
hash = {
  hosts: [
    { host: "localhost", port: 61613, ssl: false }
  ],
  connect_headers: {
    :"client-id" => "durableRubyTest"
  }
}
stomp_client = Stomp::Client.new( hash )

stomp_client.subscribe "/topic/durable.test.dev",
    {"activemq.subscriptionName" => "devtest" } do |msg|
  puts "Message! "
  puts msg.inspect
end
puts "Connected to stomp, waiting for messages..."
stomp_client.join
1

There are 1 best solutions below

3
On BEST ANSWER

Duplicate durable subscribers should receive an ERROR frame that indicates the problem. If you receive an ERROR frame after the subscribe you can handle the problem there.