我学习NATS在Ruby和希望创建从标准输入一个简单的“猫”。要么我得到所有的线路,并且循环不会退出,或者什么也不发送。红宝石NATS简单的发布
!/usr/bin/env ruby
require "nats/client"
queue = ARGV.shift
NATS.start do
STDIN.each_line do |line|
puts "will send: #{line}"
NATS.publish(queue, line)
end
NATS.stop
end
puts "... #{__LINE__}"
exit 0
和我一起跑:
# cat cat.rb | ./cat.rb myqueue
启用NATS.stop,什么也不显示在队列中,但它正确地显示每一行,它会尝试发送,并在程序退出:
# cat cat.rb | ./cat.rb myqueue
will send: #!/usr/bin/env ruby
will send:
will send: require "nats/client"
will send:
will send: queue = ARGV.shift
will send:
will send: NATS.start do
will send: STDIN.each_line do |line|
will send: puts "will send: #{line}"
will send: NATS.publish(queue, line)
will send: end
will send:
will send: NATS.stop
will send: end
will send:
will send: puts "... #{__LINE__}"
will send: exit 0
will send:
... 16
#
而且随着NATS.stop注释掉,所有的线都发送到队列,但程序不会退出:
# cat cat.rb | ./cat.rb myqueue
will send: #!/usr/bin/env ruby
will send:
will send: require "nats/client"
will send:
will send: queue = ARGV.shift
will send:
will send: NATS.start do
will send: STDIN.each_line do |line|
will send: puts "will send: #{line}"
will send: NATS.publish(queue, line)
will send: end
will send:
will send: # NATS.stop
will send: end
will send:
will send: puts "... #{__LINE__}"
will send: exit 0
will send:
<and program sits here>
我缺少什么?仅仅从Ruby发送不涉及多线程问题的消息是不是更容易?
如果您在程序结束时启用了'NATS.stop',会发生什么情况?消息是否被传递呢? – Casper
其实我认为你需要在调用'stop'之前调用'NATS.flush'来确保处理所有消息。 – Casper