Skip to content

Commit e566a90

Browse files
author
Michael Klishin
committed
Tutorial 5 in Clojure
1 parent 51e4c8d commit e566a90

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(ns rabbitmq.tutorials.emit-log-topic
2+
(:require [langohr.core :as lc]
3+
[langohr.channel :as lch]
4+
[langohr.exchange :as le]
5+
[langohr.basic :as lb]
6+
[clojure.string :as s]))
7+
8+
(def ^{:const true} x "topic_logs")
9+
10+
(defn -main
11+
[severity & args]
12+
(with-open [conn (lc/connect)]
13+
(let [ch (lch/open conn)
14+
severity (or severity "anonymous.info")
15+
payload (if (empty? args)
16+
"Hello, world!"
17+
(s/join " " args))]
18+
(le/topic ch x :durable false :auto-delete false)
19+
(lb/publish ch x severity payload)
20+
(println (format " [x] Sent %s" payload)))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(ns rabbitmq.tutorials.receive-logs-topic
2+
(:require [langohr.core :as lc]
3+
[langohr.channel :as lch]
4+
[langohr.exchange :as le]
5+
[langohr.queue :as lq]
6+
[langohr.basic :as lb]
7+
[langohr.consumers :as lcons]))
8+
9+
(def ^{:const true} x "topic_logs")
10+
11+
(defn handle-delivery
12+
"Handles message delivery"
13+
[ch {:keys [routing-key]} payload]
14+
(println (format " [x] %s:%s" routing-key (String. payload "UTF-8"))))
15+
16+
17+
(defn -main
18+
[& args]
19+
(with-open [conn (lc/connect)]
20+
(let [ch (lch/open conn)
21+
{:keys [queue]} (lq/declare ch "" :durable false :auto-delete false)]
22+
(le/topic ch x :durable false :auto-delete false)
23+
(doseq [severity args]
24+
(lq/bind ch queue x :routing-key severity))
25+
(println " [*] Waiting for logs. To exit press CTRL+C")
26+
(lcons/blocking-subscribe ch queue handle-delivery))))

0 commit comments

Comments
 (0)