Skip to content

Instantly share code, notes, and snippets.

@msszczep
Created April 24, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msszczep/11266360 to your computer and use it in GitHub Desktop.
Save msszczep/11266360 to your computer and use it in GitHub Desktop.
Reconnection in Langohr
;;adapted from http://clojurerabbitmq.info/articles/error_handling.html
(ns killwabbit.core
(:gen-class)
(:require [langohr.core :as rmq]
[langohr.channel :as lch]
[langohr.queue :as lq]
[langohr.exchange :as lx]
[langohr.consumers :as lc]
[langohr.basic :as lb])
(:import java.io.IOException
com.rabbitmq.client.AlreadyClosedException))
(defn message-handler
[ch {:keys [content-type delivery-tag type] :as meta} ^bytes payload]
(println (format "[consumer] Received a message: %s"
(String. payload "UTF-8"))))
(defn start-consumer
[ch ^String q]
(lq/declare ch q :durable true :auto-delete false); :exclusive false :auto-delete false)
(lc/subscribe ch q message-handler :auto-ack true))
(defn -main
[& args]
(let [conn (rmq/connect {:host "localhost"
:port 5672
:username "guest"
:password "guest"
:vhost "/"
:automatically-recover true
:automatically-recover-topology false})
ch (lch/open conn)
q "alestorm"
x ""]
(start-consumer ch q)
(rmq/on-recovery ch (fn [ch]
(start-consumer ch q)))
;; publish messages that are routed to langohr.examples.recovery.example1.q
;; and demonstrate consumer recovery
(while true
(Thread/sleep 1000)
(try
; (lb/publish ch x q "hello")
(catch AlreadyClosedException ace
(println "Happens when you publish while the connection is down"))
(catch IOException ioe
(println "Happens when an IOException is caught"))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment