File: per_queue_message_ttl.rb

package info (click to toggle)
ruby-amqp 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,508 kB
  • sloc: ruby: 8,272; sh: 11; makefile: 10
file content (56 lines) | stat: -rw-r--r-- 1,304 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env ruby
# encoding: utf-8

require "bundler"
Bundler.setup

$:.unshift(File.expand_path("../../../../lib", __FILE__))
require 'amqp'
require "amqp/extensions/rabbitmq"

AMQP.start do |connection|
  puts "Connected!"
  channel = AMQP::Channel.new(connection)
  channel.on_error do |ch, channel_close|
    puts "Oops! a channel-level exception: #{channel_close.reply_text}"
  end

  x = channel.fanout("amq.fanout")
  channel.queue("", :auto_delete => true, :arguments => { "x-message-ttl" => 1000 }) do |q|
    puts "Declared a new server-named queue: #{q.name}"
    q.bind(x)


    EventMachine.add_timer(0.3) do
      10.times do |i|
        puts "Publishing message ##{i}"
        x.publish("Message ##{i}")
      end
    end

    EventMachine.add_timer(0.7) do
      q.pop do |headers, payload|
        puts "Got a message: #{payload}"
      end
    end

    EventMachine.add_timer(1.5) do
      q.pop do |headers, payload|
        if payload.nil?
          puts "No messages in the queue"
        else
          raise "x-message-ttl didn't seem to work (timeout isn't up)"
        end
      end
    end
  end

  show_stopper = Proc.new {
    AMQP.stop { EventMachine.stop }
  }


  EM.add_timer(3, show_stopper)
  Signal.trap('INT',  show_stopper)
  Signal.trap('TERM', show_stopper)
end