当Rabbitmq服务器重新启动时,RabbitMQ Clojure Langohr不会抛出错误

时间:2015-02-03 23:38:05

标签: clojure rabbitmq langohr

我有以下代码监视数据库表,每隔几秒就创建一个MQ消息。我发现当我重新启动RabbitMQ服务器时,应用程序仍会运行而不会抛出异常,并且仍然会创建打印消息。那么为什么它不会抛出异常呢?另一个问题是当我杀死应用程序时如何关闭连接?由于它是一项服务,我无法编写代码来关闭RabbitMQ连接。

(require '[clojure.java.jdbc :as j])
(require '[langohr.core      :as rmq])
(require '[langohr.channel   :as lch])
(require '[langohr.queue     :as lq])
(require '[langohr.exchange  :as le])
(require '[langohr.consumers :as lc])
(require '[langohr.basic     :as lb])
(require '[clojure.data.json :as json])
(require '[clojure.java.io   :as io])

(defn load-props
  [file-name]
  (with-open [^java.io.Reader reader (io/reader file-name)]
    (let [props (java.util.Properties.)]
      (.load props reader)
      (into {} (for [[k v] props] [(keyword k) (read-string v)])))))

(def ^{:const true}
  default-exchange-name "")

(defn create-message-from-database
  ([channel qname db-spec]
   (let [select-sql "select a.file_id from file_store_metadata 
                     where processed=false "
         results (j/query db-spec select-sql)
         ]
     (doseq [row results]
       (let [file-id (:file_id row)]
         (lb/publish channel default-exchange-name qname (json/write-str row) {:content-type "text/plain" :type "greetings.hi" :persistent true})
         (j/update! db-spec :file_store_metadata {:processed true} ["file_id = ?" (:file_id row)])
         (println "message created for a new file id" (:file_id row))))
     ))
  )

(defn create-message-from-database-loop
  ([x channel qname db] (
                         while true
                          (Thread/sleep (* x 1000))
                          (create-message-from-database channel qname db)
                          ))
  )

(defn -main
  [& args]
  (let [
        properties (load-props "resource.properties")
        postgres-db (:database properties)
        rabbitmq-url (:rabbitmq properties)
        wake-up-interval (:interval properties)
        rmq-conn  (rmq/connect {:uri rabbitmq-url})
        ch    (lch/open rmq-conn)
        qname "etl scheduler"]
    (println "monitoring file store meta data on " postgres-db " every " wake-up-interval " seconds")
    (println "message will be created on rabbitmq server" rabbitmq-url)
    (lq/declare ch qname {:exclusive false :auto-delete false :persistent true})
    (create-message-from-database-loop wake-up-interval ch qname postgres-db)
    )
  )

0 个答案:

没有答案