簡體   English   中英

AMQP多任務

[英]AMQP Multitasking

我有一個需要解決的有趣情況。 我需要有一個EventMachine循環,該循環坐下並等待AMQP隊列中的消息,然后中斷該循環,以便定期將消息發送到單獨的AMQP隊列。 我是EventMachine的新手,到目前為止,這是我所擁有的,但EventMachine循環不會發送必要的消息。

現在,我進行了兩個處理:

    listen_loop = Proc.new {
        AMQP.start(connection_config) do |connection|
            AMQP::Channel.new(connection) do |channel|
                channel.queue("queue1", :exclusive => false, :durable => true) do |requests_queue|
                    requests_queue.once_declared do
                        consumer = AMQP::Consumer.new(channel, requests_queue).consume
                        consumer.on_delivery do |metadata, payload|
                            puts "[requests] Got a request #{metadata.message_id}. Sending a reply to #{metadata.reply_to}..."
                            response = "responding"
                            channel.default_exchange.publish(response,
                                :routing_key    => metadata.reply_to,
                                :correlation_id => metadata.message_id,
                                :mandatory      => true)
                            metadata.ack
                        end
                    end
                end
            end
        end
        Signal.trap("INT")  { AMQP.stop { EM.stop } }
        Signal.trap("TERM") { AMQP.stop { EM.stop } }
    }

    send_message = Proc.new {
        AMQP.start(connection_config) do |connection|
            channel = AMQP::Channel.new(connection)
            queue   = channel.queue('queue2')

            channel.default_exchange.publish("hello world", :routing_key => queue.name)
            EM.add_timer(0.5) do
                connection.close do
                    EM.stop{ exit }
                end
            end
        end
    }

然后我有了EventMachine循環:

    EM.run do 
        EM.add_periodic_timer(5) { send_message.call }
        listen_loop.call
    end

我能夠在偵聽循環中接收消息,但是無法按固定間隔發送任何消息。

弄清楚我在做什么錯。 消息循環無法打開與RabbitMQ服務器的新連接,因為它已經連接。 將所有內容合並到一個EventMachine循環中,並重新使用該連接即可工作。

對於那些好奇的人,它看起來像這樣:

EM.run do

    AMQP.start(connection_config) do |connection|
        channel = AMQP::Channel.new(connection)

        EM.add_periodic_timer(5) { channel.default_exchange.publish("foo", :routing_key => 'queue2') }

        queue = channel.queue("queue1", :exclusive => false, :durable => true)
        channel.prefetch(1)
        queue.subscribe(:ack => true) do |metadata, payload|
            puts "[requests] Got a request #{metadata.message_id}. Sending a reply to #{metadata.reply_to}..."
            response = "bar"
            channel.default_exchange.publish(response,
                :routing_key    => metadata.reply_to,
                :correlation_id => metadata.message_id,
                :mandatory      => true)
            metadata.ack
        end
    end
    Signal.trap("INT")  { AMQP.stop { EM.stop } }
    Signal.trap("TERM") { AMQP.stop { EM.stop } }

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM