1

尽管我已经进行了足够多的搜索,但无法获得解决此问题的帮助。我对 Ruby 很陌生,所以如果我遗漏了一些非常基本的东西,请原谅我。

在 Windows 上执行以下代码时,我收到[]': can't convert Symbol into Integer (TypeError) from C:/Users/shhashmi/workspace/rabbitmqSender/sender.rb:36:in来自 C:/Users/shhashmi/workspace/rabbitmqSender/sender.rb:59:in 的错误“C:/Users/shhashmi/workspace/rabbitmqSender/sender.rb:36:in getItem” `'"

但是代码在 CentOS 上运行良好。

require "bunny"
require "net/http"

# Rabbit MQ
@host = "test.host"
@queue = "test.queue"

#@host = "localhost"
#@queue = "TEST"


# Put your target machine here
@target = "http://localhost:3000/"

def getItem
b = Bunny.new(:host=>@host, :port=>5672,)
# start a communication session with the amqp server
                b.start

                # declare a queue
                q = b.queue(@queue, :auto_delete=>true)


                # declare default direct exchange which is bound to all queues
                e = b.exchange("")



                # publish a message to the exchange which then gets routed to the queue

                #e.publish("Hello, everybody! 211", :key => @queue)
                #e.publish("Hello, everybody! 311", :key => @queue)

                # get message from the queue
                msg = q.pop[:payload]

                puts "This is the message: " + msg + "\n\n"

                # close the connection
                b.stop
                return msg
end


getItem
4

1 回答 1

0

我能够使用您的代码重现该错误。问题在于这一行:

msg = q.pop[:payload]

Queue#pop 方法在从队列中弹出消息后返回一个包含 3 个项目的数组。该方法应如下所示:

delivery_info, message_properties, msg = q.pop

现在,您应该在“msg”变量中看到您的消息有效负载。您可以检查其他两个结果以收集任何有用的信息(例如队列中剩余消息的数量),或者如果您不需要它们则完全忽略它们

于 2013-06-25T07:14:08.717 回答