背景: 公司有套 java 系统, 里面用的是 ActiveMQ 来做消息通知. 现用 rails 开发了另外一 web 服务. 然后 rails 里有个小功能是现在的 java 系统要使用的, 就想着用 ActiveMQ 来进行信息的交互.
实现方式: 首先 java 系统给 ActiveMQ 的 '/queus/java-rails' 发送一条消息, rails 利用 stomp 来 subscribe '/queus/java-rails' 的消息, 执行相关代码之后, 把结果发送给 ActiveMQ '/queus/rails-java'.
目前代码
class MyStomp
def initialize
@username = 'admin'
@password = 'admin'
@hostname = 'localhost'
@port = '61613'
end
def run
@client = Stomp::Client.new(@username, @password, @hostname, @port, true)
@client.subscribe("'/queus/java-rails", id: @client.uuid, 'ack' => 'auto') do |msg|
@client.publish("/queue/rails-java", "任务执行完了")
end
puts "Connected"
end
def shutdown
@client.close if @client
puts 'DisConnected'
end
end
# 启动订阅
client = MyStomp.new
begin
client.run
rescue SystemExit, Interrupt
client.shutdown
rescue Exception => e
client.shutdown
raise e
end
问题: 该怎么在 rails 内组织代码呢, 以及该如何启动该订阅?