对于任何Clojure变量,您将使用Clojure.var()来获取它的引用,然后在其上调用invoke()。对于类似消息这样的字面量,您可以使用Clojure.read()从字符串中读取Clojure数据,或者使用构造函数,如hash-map。with-open是一个宏,但在这种情况下,它基本上是复制了您在Java中可以用try-with-resources做到的事情,因此您其实不需要它。->将需要展开,或者您可以将更多内容放入一个Clojure函数中,然后调用它。
我还没有编译这个,但您最终会得到类似以下代码的结果
```
private static final IFn require = Clojure.var("clojure.core", "require");
private static final IFn doall = Clojure.var("clojure.core", "doall");
private static final Object portKW = Clojure.read(":port");
private static final Object opKW = Clojure.read(":op");
private static final Object codeKW = Clojure.read(":code");
private static final IFn connect;
private static final IFn client;
private static final IFn message;
static {
require.invoke(Clojure.read("nrepl.core"));
connect = Clojure.var("nrepl.core", "connect");
client = Clojure.var("nrepl.core", "client");
message = Clojure.var("nrepl.core", "message");
}
public Object getConnection(int port) {
return connect.invoke(portKW, port);
}
public Object getClient(Object conn, int timeout) {
return client.invoke(conn, timeout);
}
public Object send(Object client, Object message) {
Object result = message.invoke(client, message);
return doall.invoke(result);
}
```
then
```
try (Object conn = getConnection(59258)) {
Object client = getClient(conn, 1000);
Object result = send(client, Map.of(opKW, "eval", codeKW, "(time (reduce + (range 1e6)))"));
// result here is a map, do what you want with it
}
```