|
| 1 | +using AMQPClient |
| 2 | +const VIRTUALHOST = "/" |
| 3 | +const HOST = "127.0.0.1" |
| 4 | + |
| 5 | + |
| 6 | +function receive() |
| 7 | + # 1. Create a connection to the localhost or 127.0.0.1 of virtualhost '/' |
| 8 | + connection(; virtualhost=VIRTUALHOST, host=HOST) do conn |
| 9 | + # 2. Create a channel to send messages |
| 10 | + channel(conn, AMQPClient.UNUSED_CHANNEL, true) do chan |
| 11 | + # 3. Declare a exchange |
| 12 | + exchange = "direct_logs" |
| 13 | + exchange_declare(chan, exchange, EXCHANGE_TYPE_DIRECT) |
| 14 | + |
| 15 | + result, queue_name, _, _ = queue_declare(chan, "", exclusive=true) |
| 16 | + |
| 17 | + # 4. Receive queues to bind |
| 18 | + if length(Base.ARGS) <= 0 |
| 19 | + println(Base.stdout, "Usage: [info] [warning] [error]\n") |
| 20 | + Base.exit(1) |
| 21 | + end |
| 22 | + |
| 23 | + # 4.1 Bind queues |
| 24 | + for severity in Base.ARGS[1:end] |
| 25 | + queue_bind(chan, queue_name, exchange, |
| 26 | + severity) |
| 27 | + end |
| 28 | + |
| 29 | + println(" [*] Waiting for messages. To exit press CTRL+C") |
| 30 | + on_receive = (msg) -> begin |
| 31 | + data = String(msg.data) |
| 32 | + println("Received the message: $data") |
| 33 | + basic_ack(chan, msg.delivery_tag) |
| 34 | + end |
| 35 | + |
| 36 | + success, consumer_tag = basic_consume(chan, queue_name, on_receive) |
| 37 | + |
| 38 | + while true |
| 39 | + sleep(1) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +# Don't exit on Ctrl-C |
| 46 | +Base.exit_on_sigint(false) |
| 47 | +try |
| 48 | + receive() |
| 49 | +catch ex |
| 50 | + if ex isa InterruptException |
| 51 | + println("Interrupted") |
| 52 | + else |
| 53 | + println("Exception: $ex") |
| 54 | + end |
| 55 | +end |
0 commit comments