简体   繁体   English

调用amqp消息队列不是异步的

[英]Calling amqp message queque is not asynchronous

Here is my code: 这是我的代码:

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

I'm using Rabbitmq as a broker with rails amqp gem . 我使用Rabbitmq作为Rails amqp gem的经纪人。 Now I'm calling : 现在我打电话:

ParsepdfClient.test from a controller. 从控制器ParsepdfClient.test

From my understanding my call shouldn't sleep for a minute but it waits for a minute and it outputs 据我了解,我的电话不应该睡一分钟,而是要等待一分钟,然后输出

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

And then it executes rest of my code of controller. 然后执行我的控制器代码的其余部分。 Shouldn't the call be asynchronous? 调用不应该是异步的吗? If not how can I make it asynchronous? 如果没有,如何使它异步?

What I mean is that shouldn't it execute the rest of the code of my controller after outputting 我的意思是说,它不应该在输出后执行控制器的其余代码

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

I managed to implement Rabbitmq in rails successfully with the help of my senior. 在我的上级的帮助下,我成功地在Rails中成功实现Rabbitmq。 If any one is having any issue I have written a blog post here . 如果有任何问题,我在这里写了一篇博客文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM