Last active
December 29, 2015 12:29
-
-
Save simshanith/7670657 to your computer and use it in GitHub Desktop.
Clojure RabbitMQ Langohr "Hello World" example as an executable command-line script
This file contains hidden or 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
#!/usr/bin/env lein-exec | |
(require 'leiningen.exec) | |
(leiningen.exec/deps '[[com.novemberain/langohr "1.4.1"]]) | |
(ns clojurewerkz.langohr.examples.hello-world | |
(:gen-class) | |
(:require [langohr.core :as rmq] | |
[langohr.channel :as lch] | |
[langohr.queue :as lq] | |
[langohr.consumers :as lc] | |
[langohr.basic :as lb])) | |
(def ^{:const true} | |
default-exchange-name "") | |
(defn message-handler | |
[ch {:keys [content-type delivery-tag type] :as meta} ^bytes payload] | |
(println (format "[consumer] Received a message: %s, delivery tag: %d, content type: %s, type: %s" | |
(String. payload "UTF-8") delivery-tag content-type type))) | |
(defn -main | |
[& args] | |
(let [conn (rmq/connect) | |
ch (lch/open conn) | |
qname "langohr.examples.hello-world"] | |
(println (format "[main] Connected. Channel id: %d" (.getChannelNumber ch))) | |
(lq/declare ch qname :exclusive false :auto-delete true) | |
(lc/subscribe ch qname message-handler :auto-ack true) | |
(lb/publish ch default-exchange-name qname "Hello!" :content-type "text/plain" :type "greetings.hi") | |
(Thread/sleep 2000) | |
(println "[main] Disconnecting...") | |
(rmq/close ch) | |
(rmq/close conn))) | |
(-main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment