0

这是我的代码:

class ParsepdfClient 
#!/usr/bin/env ruby
# encoding: utf-8

require 'amqp'
require "rubygems"
require 'mq'

  def self.test

    EventMachine.run do
        connection = AMQP.connect(:host => '127.0.0.1')
        puts "Connected to AMQP broker. Running #{AMQP::VERSION} version of the gem..."

        channel = AMQP::Channel.new(connection)
        queue = channel.queue("amqpgem.examples.helloworld", :auto_delete => true)
        exchange = channel.direct("")

        queue.subscribe do |payload|
            sleep(1.minutes)
            puts "Received a message: #{payload}. Disconnecting..."
            connection.close { EventMachine.stop }
        end

        exchange.publish "Hello, world!", :routing_key => queue.name
    end   
  end
end

我使用Rabbitmq作为带有 rails amqp gem的经纪人。现在我打电话:

ParsepdfClient.test从控制器。

据我了解,我的电话不应该睡一分钟,而是等一分钟然后输出

"Received a message: Hello, world!. Disconnecting..."

然后它执行我的控制器代码的其余部分。调用不应该是异步的吗?如果不是,我怎样才能使它异步?

我的意思是它不应该在输出后执行我的控制器的其余代码

Connected to AMQP broker. Running #{AMQP::VERSION} version of the gem...
4

1 回答 1

0

在我的前辈的帮助下,我成功地在 Rails 中实现了 Rabbitmq。如果有人有任何问题,我在这里写了一篇博文。

于 2013-09-28T22:14:36.977 回答