clojure / jmx with-connection松动连接

时间:2016-08-02 21:50:23

标签: clojure jmx

我尝试使用clojure中的相同连接调用多个远程JMX操作。我正在使用

[org.clojure/java.jmx "0.3.3"]

但是我发现连接被本地JMX连接急切地取代了。举例说明:

下式给出:

 (ns test.core
  (:require
    [clojure.java.jmx :as jmx]))

如果我有类似的话:

(defn caller [col host port]
  (println jmx/*connection*)
  (jmx/with-connection {:host host, :port port :environment
                      {"jmx.remote.credentials" (into-array String ["tomcat" "tomcat"])}}
    (do
    (println jmx/*connection*)
    (map called col))))

(defn called [line]
  (println jmx/*connection*)
)

调用

     (caller [1] "host" 8000)

我明白了:

#object[com.sun.jmx.mbeanserver.JmxMBeanServer 0x2c914364 com.sun.jmx.mbeanserver.JmxMBeanServer@2c914364]
#object[javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection 0x2b4bbf01 javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@2b4bbf01]
#object[com.sun.jmx.mbeanserver.JmxMBeanServer 0x2c914364 com.sun.jmx.mbeanserver.JmxMBeanServer@2c914364]   

因此,远程连接仅适用于在返回到本地服务器之前访问连接的第一个语句。如何保持远程连接?

1 个答案:

答案 0 :(得分:3)

尝试用(map called col)替换(run! called col),我打赌它会起作用。这与clojure的集合懒惰有关:当你调用map时,它会立即返回,而不会实现元素,并且当它涉及到元素时。实现时,流已经超出with-connection块,这意味着在连接已经关闭的上下文中调用了函数called