|
9 | 9 | AMQP |
10 | 10 | ConnectionFactory |
11 | 11 | Consumer |
12 | | - QueueingConsumer))) |
| 12 | + QueueingConsumer) |
| 13 | + com.rabbitmq.client.QueueingConsumer$Delivery)) |
13 | 14 |
|
14 | 15 |
|
15 | 16 | ;; abbreviatons: |
|
56 | 57 | (.close ch) |
57 | 58 | (.close conn)) |
58 | 59 |
|
| 60 | +(defn #^String delivery-contents |
| 61 | + "Return the contents of the Delivery as a String." |
| 62 | + [#^"QueueingConsumer$Delivery" d] |
| 63 | + (when d |
| 64 | + (String. (.getBody d)))) |
| 65 | + |
| 66 | +(defn ack-delivery [#^Channel ch |
| 67 | + #^"QueueingConsumer$Delivery" d] |
| 68 | + "Acknowledge a Delivery from the given Channel." |
| 69 | + (.basicAck ch (.. d getEnvelope getDeliveryTag) false)) |
| 70 | + |
| 71 | +(defn delivery-seq |
| 72 | + "Returns a lazy sequence of deliveries from the queue. |
| 73 | + The caller is responsible for acking the message and |
| 74 | + extracting its contents." |
| 75 | + [#^Channel ch |
| 76 | + #^QueueingConsumer q] |
| 77 | + (lazy-seq |
| 78 | + (let [d (.nextDelivery q)] |
| 79 | + (cons d (delivery-seq ch q))))) |
| 80 | + |
59 | 81 | ;;;; AMQP Queue as a sequence |
60 | | -(defn delivery-seq [#^Channel ch |
61 | | - #^QueueingConsumer q] |
| 82 | +(defn message-seq |
| 83 | + "Return a lazy sequence of queue items. Automatically |
| 84 | + acks each message and returns the content as a string." |
| 85 | + [#^Channel ch |
| 86 | + #^QueueingConsumer q] |
62 | 87 | (lazy-seq |
63 | 88 | (let [d (.nextDelivery q) |
64 | | - m (String. (.getBody d))] |
65 | | - (.basicAck ch (.. d getEnvelope getDeliveryTag) false) |
66 | | - (cons m (delivery-seq ch q))))) |
| 89 | + m (delivery-contents d)] |
| 90 | + (ack-delivery ch d) |
| 91 | + (cons m (message-seq ch q))))) |
67 | 92 |
|
68 | 93 | (defn #^QueueingConsumer |
69 | 94 | declare-queue-and-consumer |
70 | 95 | "Return a QueueingConsumer with the appropriate settings." |
71 | 96 | [#^Channel ch queue prefetch] |
72 | 97 | (.queueDeclare ch queue) |
73 | 98 | (when prefetch |
74 | | - (.basicQos ch prefetch) |
75 | | - (QueueingConsumer. ch))) |
76 | | - |
77 | | -(defn queue-seq |
78 | | - "Return a sequence of the messages in queue with name queue-name" |
79 | | - ([#^Channel ch |
80 | | - {:keys [queue prefetch]}] |
81 | | - (let [consumer (declare-queue-and-consumer ch queue prefetch)] |
82 | | - (.basicConsume ch queue consumer) |
83 | | - (delivery-seq ch consumer))) |
| 99 | + (.basicQos ch prefetch)) |
| 100 | + (QueueingConsumer. ch)) |
| 101 | + |
| 102 | +(defmacro with-declared-queue-and-consumer |
| 103 | + "Macro which establishes a declared queue and consumer, starting |
| 104 | + basicConsume, then executing the body. |
| 105 | + `consumer` is bound to the resulting consumer for the duration of the body." |
| 106 | + [[ch consumer options] & body] |
| 107 | + `(let [opts# ~options |
| 108 | + queue# (:queue opts#) |
| 109 | + prefetch# (:prefetch opts#) |
| 110 | + cch# ~ch] |
| 111 | + (let [~consumer (declare-queue-and-consumer cch# queue# prefetch#)] |
| 112 | + (.basicConsume cch# queue# ~consumer) |
| 113 | + ~@body))) |
| 114 | + |
| 115 | +(defn queue-delivery-seq |
| 116 | + "Return a sequence of the Delivery objects in the named queue." |
| 117 | + ([#^Channel ch opts] |
| 118 | + (with-declared-queue-and-consumer |
| 119 | + [ch consumer opts] |
| 120 | + (delivery-seq ch consumer)))) |
| 121 | + |
| 122 | +(defn queue-message-seq |
| 123 | + "Return a sequence of the messages in queue with name `queue-name`." |
| 124 | + ([#^Channel ch opts] |
| 125 | + (with-declared-queue-and-consumer |
| 126 | + [ch consumer opts] |
| 127 | + (message-seq ch consumer))) |
84 | 128 |
|
85 | 129 | ([conn |
86 | 130 | #^Channel ch |
87 | 131 | c] |
88 | | - (queue-seq ch c))) |
89 | | - |
| 132 | + (queue-message-seq ch c))) |
| 133 | + |
| 134 | +(defn queue-seq [& args] |
| 135 | + (apply queue-message-seq args)) |
90 | 136 |
|
91 | 137 | ;;; consumer routines |
92 | 138 | (defn consume-wait |
|
0 commit comments