jvm RabbitMQ:客户端未接收到客户端启动前发送的消息

ss2ws0br  于 2022-11-07  发布在  RabbitMQ
关注(0)|答案(2)|浏览(190)

我使用的是带有Langohr(clojure客户端)的RabbitMQ,如果消费者已经在运行,我可以很好地接收消息,但是如果我在客户端启动之前发送消息,客户端永远不会接收到这些消息。
是否有一个配置选项(或用于服务器和客户端的配置选项组合)可以确保客户端可以接收在其运行之前生成的消息?
我如何开始尝试测试或调试它?
我使用的是默认的exchange。下面是我用来设置客户端的代码:

(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}))

谢谢你!

mum43rcc

mum43rcc1#

看起来您可以为消息和队列设置一个生存时间。因此,当没有人连接到队列时,队列会保留一段时间,因此消息会保留一段时间,而不会被声明为“死”。
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)))
798qvoo8

798qvoo82#

事实证明,您需要设置:auto-delete false,以使持久队列表现良好。

相关问题