Last active
January 2, 2016 13:39
-
-
Save kawasima/8311601 to your computer and use it in GitHub Desktop.
Publish and consume clojure values via RabbitMQ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[langohr.queue :as lq] | |
'[langohr.core :as rmq] | |
'[langohr.channel :as lch] | |
'[langohr.basic :as lb] | |
'[langohr.consumers :as lc] | |
'[clojure.data.fressian :as fress]) | |
;; Publish clojure values | |
(let [conn (rmq/connect {:uri "amqp://localhost"}) | |
ch (lch/open conn)] | |
(lq/declare ch "test-queue") | |
(try | |
(lb/publish | |
ch "" "test-queue" | |
(->> {:a {:b [1 2 3] :c "CC" :d #{4 5 6}}} | |
(fress/write) | |
(.array))) | |
(catch Exception e (.getMessage e)) | |
(finally (rmq/close ch))) | |
;; Consume clojure values (Push API) | |
(let [conn (rmq/connect {:uri "amqp://localhost"}) | |
ch (lch/open conn)] | |
(lq/declare ch "test-queue") | |
(lb/consume ch "test-queue" | |
(lc/create-default ch | |
:handle-delivery-fn (fn [ch metadata payload] | |
(println (fress/read payload)))))) | |
;; Consume clojure values (Pull API) | |
(let [conn (rmq/connect {:uri "amqp://localhost"}) | |
ch (lch/open conn)] | |
(lq/declare ch "test-queue") | |
(try | |
(if-let [[metadata msg] (lb/get ch "test-queue")] | |
(println (fress/read msg))) | |
(catch Exception e (.getMessage e)) | |
(finally (rmq/close ch)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment