【发布时间】:2014-07-05 04:02:14
【问题描述】:
我有多个 Ruby 进程启动并尝试使用 Stomp 通过持久订阅者连接到主题。
第一个进程成功,并读取消息(耶)。
后续进程失败,并反复尝试重新连接。
我的进程如何发现持久订阅者已经连接,然后退出尝试连接?
可能的虚码sn-p:
begin
stomp_client.subscribe()
rescue ClientAlreadySubscribedException
puts "No problem, let's keep doing our other code"
end
环境:
- Ruby 1.9.3
- stompgem 1.3.2
代码:
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
【问题讨论】:
-
您可以使用
Stomp::Connection而不是Stomp::Client,它会为您提供错误消息。为避免“反复尝试重新连接”问题,您可以在 config_hash 中使用max_reconnect_attempts选项
标签: activemq stomp durable-subscription