commented by alexmiller
对于任何Clojure var,您将使用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) {
}
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);
}
```
然后
```
try (Object conn = getConnection(59258)) {
Object client = getClient(conn, 1000);
Object result = send(client, Map.of(opKW, "eval", codeKW, "(time (reduce + (range 1e6)))"));
这里的result是map类型,你可以做任何处理
}
```