RabbitMQ:客户端启动之前,客户端不接收发送的消息

时间:2019-07-23 21:45:57

标签: clojure jvm rabbitmq

我正在将RabbitMQ与Langohr(clojure客户端)一起使用。如果使用者已经在运行,则可以很好地接收消息,但是如果在启动客户端之前发送消息,则客户端将永远不会收到这些消息。

是否有一个配置选项(或服务器和客户端的配置选项组合)来确保客户端可以接收运行前生成的消息?

我如何开始尝试对此进行测试或调试?

我正在使用默认交易所。这是我用来设置客户端的代码:

(ns sisyphus.rabbit
  (:require
   [cheshire.core :as json]
   [langohr.core :as lcore]
   [langohr.channel :as lchannel]
   [langohr.exchange :as lexchange]
   [langohr.queue :as lqueue]
   [langohr.consumers :as lconsumers]
   [langohr.basic :as lbasic]
   [sisyphus.log :as log]))

(defn connect!
  [config]
  (let [connection (lcore/connect {})
        channel (lchannel/open connection)
        _ (lbasic/qos channel 1)
        queue-name (get config :queue "sisyphus")
        exchange (get config :exchange "")
        queue (lqueue/declare channel "sisyphus" {:exclusive false :durable true})
        routing-key (get config :routing-key "sisyphus")]
    (if-not (= exchange "")
      (lqueue/bind channel queue-name exchange {:routing-key routing-key}))
    {:queue queue
     :queue-name queue-name
     :exchange exchange
     :routing-key routing-key
     :connection connection
     :channel channel
     :config config})

然后发布:

(defn publish!
  [rabbit message]
  (lbasic/publish
   (:channel rabbit)
   (:exchange rabbit)
   (:routing-key rabbit)
   (json/generate-string message)
   {:content-type "text/plain"
    :peristent true}))

谢谢!

2 个答案:

答案 0 :(得分:0)

您似乎可以设置一个时间来保留消息和队列。因此,当没有人连接到该队列时,该队列会一直存在,因此消息会保留一段时间而不被声明为“死”。

http://clojurerabbitmq.info/articles/extensions.html#per-queue-message-time-to-live

  

示例

     

下面的示例将新服务器命名队列的消息TTL设置为500毫秒。然后,它发布一条路由到队列的消息,并在等待600毫秒后对队列中的消息进行计数:

(ns clojurewerkz.langohr.examples.per-queue-message-ttl
  (:gen-class)
  (:require [langohr.core    :as rmq]
            [langohr.channel :as lch]
            [langohr.queue   :as lq]
            [langohr.basic   :as lb]))

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

(defn -main
  [& args]
  (let [conn  (rmq/connect)
        ch    (lch/open conn)
        qname "clojurewerkz.langohr.examples.per-queue-message-ttl"]
    (lq/declare ch qname {:arguments {"x-message-ttl" 500} :durable false})
    (lb/publish ch default-exchange-name qname "a message")
    (Thread/sleep 50)
    (println (format "Queue %s has %d messages" qname (lq/message-count ch qname)))
    (println "Waiting for 600 ms")
    (Thread/sleep 600)
    (println (format "Queue %s has %d messages" qname (lq/message-count ch qname)))
    (println "[main] Disconnecting...")
    (rmq/close ch)
    (rmq/close conn)))

答案 1 :(得分:0)

事实证明,您需要设置:auto-delete false才能使持久队列正常运行。你知道的更多!