Rails 应用程序内的 AMQP 订阅者

发布于 2024-09-01 17:34:02 字数 180 浏览 7 评论 0原文

是否可以使用我的 Rails 应用程序启动 AMQP 订阅者?可能通过初始化程序或其他东西。

我想让它同时运行,也可以与 Rails 模型交互。下面是我的意思的伪代码示例。

queue.subscribe do |msg,body|
  Foo.create(....)
end

Is it possible to start an AMQP subscriber with my Rails app? Possibly through an initializer or something.

I'd like to have it running at the same time that can also interact with Rails models. Below is a pseudo-code example of what I mean.

queue.subscribe do |msg,body|
  Foo.create(....)
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-09-08 17:34:02

我通常通过加载 Rails 环境的单独消息传递守护进程来执行此操作。

因此,一个非常简单的示例如下所示:rails_root/script/myapp_daemon.rb :



    #!/usr/bin/env ruby
    require 'rubygems'
    require 'amqp'
    require 'daemons'

    ENV["RAILS_ENV"] ||= "development"
    require File.dirname(__FILE__) + "/../config/environment"

    options = { :backtrace => true, :dir => '.', :log_output => true}

    Daemons.run_proc('myapp_daemon', options) do
      EventMachine.run do
        connection = AMQP.connect(:host => "127.0.0.1")

        channel = AMQP::Channel.new(connection)
        queue    = channel.queue("/myapp_daemon", :durable => true)
        exchange = channel.direct("")

        queue.subscribe do |payload|
          obj = JSON.parse(payload)
          #... handle messages here, utilize your rails models
          Foo.create(...)
        end
      end
    end

您还需要在 Gemfile 中添加正确的 gem 要求:amqp、daemons、eventmachine

然后要么与您的应用程序一起手动运行它:

RAILS_ENV=development script/myapp_daemon.rb run

要么从以下之一启动它你的应用程序初始值设定项:

system('script/myapp_daemon.rb start')

要深入了解 amqp,请查看以下内容,这将提供一个很好的高级概述:
http://www.rubyinside.com/ Why-rubyists-should-care-about-messaging-a-high-level-intro-5017.html

这给出了非常详细的解释和工作示例:
http://rubydoc.info/github/ruby-amqp/amqp/ master/file/docs/Exchanges.textile#Publishing_messages_as_immediate_

最后看看 Bunny 是否完成了客户端所需的一切,就更简单了:
https://github.com/celldee/bunny/wiki/Using-Bunny

希望有帮助

I usually do this via a separate messaging daemon which loads the rails environment.

So a very simplistic example would look like this in rails_root/script/myapp_daemon.rb :



    #!/usr/bin/env ruby
    require 'rubygems'
    require 'amqp'
    require 'daemons'

    ENV["RAILS_ENV"] ||= "development"
    require File.dirname(__FILE__) + "/../config/environment"

    options = { :backtrace => true, :dir => '.', :log_output => true}

    Daemons.run_proc('myapp_daemon', options) do
      EventMachine.run do
        connection = AMQP.connect(:host => "127.0.0.1")

        channel = AMQP::Channel.new(connection)
        queue    = channel.queue("/myapp_daemon", :durable => true)
        exchange = channel.direct("")

        queue.subscribe do |payload|
          obj = JSON.parse(payload)
          #... handle messages here, utilize your rails models
          Foo.create(...)
        end
      end
    end

You would also need the right gem requires in your Gemfile: amqp, daemons, eventmachine

Then either run it manually alongside your app:

RAILS_ENV=development script/myapp_daemon.rb run

Or start it from one of your app initializers:

system('script/myapp_daemon.rb start')

To dig into amqp check out the following, this will give a nice high level overview:
http://www.rubyinside.com/why-rubyists-should-care-about-messaging-a-high-level-intro-5017.html

This gives a very detailed explanation with working examples:
http://rubydoc.info/github/ruby-amqp/amqp/master/file/docs/Exchanges.textile#Publishing_messages_as_immediate_

Finally see if Bunny accomplishes everything you need for the client, it is simpler:
https://github.com/celldee/bunny/wiki/Using-Bunny

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文