如何使用clojure连接到远程rabbitmq

时间:2015-01-29 14:39:56

标签: clojure rabbitmq

我正在使用langohr连接rabbitmq,如果我没有指定任何连接字符串,它运行良好并连接到本地服务器,但我想连接到远程服务器。所以我有以下代码。我使用的是amqp:// bigdata:bigdata @ s1:5672的连接字符串,s2是远程服务器的主机名。

let [            a (println rabbitmq-url)
            rmq-conn  (rmq/connect {:uri rabbitmq-url})
            a (println rabbitmq-url)]

但它会抛出以下错误

$ lein run
amqp://bigdata:bigdata@s1:5672
Exception in thread "main" java.io.IOException, compiling:(/tmp/form-init589039011205967992.clj:1:71)
    at clojure.lang.Compiler.load(Compiler.java:7142)
    at clojure.lang.Compiler.loadFile(Compiler.java:7086)
    at clojure.main$load_script.invoke(main.clj:274)
    at clojure.main$init_opt.invoke(main.clj:279)
    at clojure.main$initialize.invoke(main.clj:307)
    at clojure.main$null_opt.invoke(main.clj:342)
    at clojure.main$main.doInvoke(main.clj:420)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:383)
    at clojure.lang.AFn.applyToHelper(AFn.java:156)
    at clojure.lang.Var.applyTo(Var.java:700)
    at clojure.main.main(main.java:37)
Caused by: java.io.IOException
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:376)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:36)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:83)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:609)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:651)
    at com.novemberain.langohr.Connection.init(Connection.java:92)
    at langohr.core$connect.invoke(core.clj:93)
    at clojurewerkz.testcom.core$create_message_from_database.invoke(core.clj:33)
    at clojurewerkz.testcom.core$create_message_from_database_loop.invoke(core.clj:53)
    at clojurewerkz.testcom.core$_main.doInvoke(core.clj:60)
    at clojure.lang.RestFn.invoke(RestFn.java:397)
    at clojure.lang.Var.invoke(Var.java:375)
    at user$eval5.invoke(form-init589039011205967992.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:6703)
    at clojure.lang.Compiler.eval(Compiler.java:6693)
    at clojure.lang.Compiler.load(Compiler.java:7130)
    ... 11 more
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
    ... 27 more
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
    at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
    at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95)
    at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:139)
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:534)
    at java.lang.Thread.run(Thread.java:745)

3 个答案:

答案 0 :(得分:0)

首先,确保RabbitMQ在远程服务器上运行。其次,检查连接到该实例的用户是否具有这样做的权限。

答案 1 :(得分:0)

同样的问题如下:我的帐户无法访问虚拟主机。 RabbitMQ new connection refused due to SocketException

答案 2 :(得分:0)

试试这段代码。

首先,将这些库放入project.clj

 ;;rabbitmq client
 [com.novemberain/langohr "2.11.0"]
 ;;logging
 [org.clojure/tools.logging "0.2.4"]

然后,在clj文件中进行必要的导入:

(:require
;logging
[clojure.tools.logging :as log]

;;rabbitmq
[langohr.core :as rmq]
[langohr.channel :as lch]
[langohr.queue :as lq]
[langohr.exchange :as le]
[langohr.basic :as lb] ))

这是连接功能:

(defn get-mq-connect
"This function connects to RabbitMQ.
params example: {:host \"localhost\" :port 5672 :username \"guest\" :password \"guest\" :vhost \"/\"}
Returns: connect object - connection successful, nil - error occured."
[params]
(try
  (log/trace params)
  (rmq/connect params)
  (catch Exception e
    (log/error (.getMessage e))
    (println "MQ connection error:" (.getMessage e)))))

你可以这样调用这个函数:

(let [ params {:host "myhost.com" :port 5672 :username "admin" :password "StrongPassw098" :vhost "/"}
       conn (get-mq-connect mq-params)]
(when-not (nil? conn)
  (let [ch (lch/open conn)]
    (while (not @stop-flag?)
      (do
         ;;put here your code
         ))
    (rmq/close ch)
    (rmq/close conn)))))