作为 Apache Storm 的一部分,我们有一些代码可以从 Java 使用以下代码加载 Clojure 函数。
`
public static IFn loadClojureFn(String namespace, String name) {
try {
clojure.lang.Compiler.eval(RT.readString("(require '" + namespace + ")"));
} catch (Exception e) {
//if playing from the repl and defining functions, file won't exist
}
return (IFn) RT.var(namespace, name).deref();
}
`
如果这个函数同时从多个不同的线程中被调用,尝试导入相同的命名空间,我有时会得到一些非常奇怪的错误。注意:我必须修改 catch 以打印出错误消息(我们也不应该 eating 异常)。
{verbatim}
2016-01-07 16:26:09.305 b.s.u.Utils (链接:警告) 寄存器加载失败
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: 在此上下文中无法解析符号:sentence-spout,编译:(storm/starter/clj/word_count.clj:21:1)
at clojure.lang.Compiler.analyze(Compiler.java:6543) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6485) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3791) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6725) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6524) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6485) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.eval(Compiler.java:6786) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.load(Compiler.java:7227) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.loadResourceScript(RT.java:371) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.loadResourceScript(RT.java:362) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.load(RT.java:446) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.load(RT.java:412) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load$fn__5448.invoke(core.clj:5866) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load.doInvoke(core.clj:5865) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.invoke(RestFn.java:408) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_one.invoke(core.clj:5671) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_lib$fn__5397.invoke(core.clj:5711) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_lib.doInvoke(core.clj:5710) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.applyTo(RestFn.java:142) ~(link: clojure-1.7.0.jar:?)
at clojure.core$apply.invoke(core.clj:632) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_libs.doInvoke(core.clj:5749) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.applyTo(RestFn.java:137) ~(link: clojure-1.7.0.jar:?)
at clojure.core$apply.invoke(core.clj:632) ~(link: clojure-1.7.0.jar:?)
at clojure.core$require.doInvoke(core.clj:5832) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.invoke(RestFn.java:408) ~(link: clojure-1.7.0.jar:?)
at clojure.core$eval114.invoke(NO_SOURCE_FILE:0) ~(link: ?:?)
at clojure.lang.Compiler.eval(Compiler.java:6782) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.eval(Compiler.java:6745) ~(link: clojure-1.7.0.jar:?)
at backtype.storm.utils.Utils.loadClojureFn(Utils.java:602) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at backtype.storm.clojure.ClojureBolt.prepare(ClojureBolt.java:57) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at backtype.storm.daemon.executor$fn__8297$fn__8310.invoke(executor.clj:785) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at backtype.storm.util$async_loop$fn__556.invoke(util.clj:482) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at clojure.lang.AFn.run(AFn.java:22) (link: clojure-1.7.0.jar:?)
at java.lang.Thread.run(Thread.java:745) (link: ?:1.8.0_60)
原因:java.lang.RuntimeException:在此上下文中无法解析符号:sentence-spout
at clojure.lang.Util.runtimeException(Util.java:221) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.resolveIn(Compiler.java:7019) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.resolve(Compiler.java:6963) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6924) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6506) ~(link: clojure-1.7.0.jar:?)
... 33 more
{verbatim}
如果我将静态 Java 函数同步,问题就会消失。它总是出现在解析几个特定的宏时,其中某个符号无法解析。
正在尝试加载的命名空间。
https://github.com/apache/storm/blob/a99d9c11be005ade7c308bebdda71c7fb0111acc/examples/storm-starter/src/clj/storm/starter/clj/word_count.clj
遇到异常的宏。
https://github.com/apache/storm/blob/a99d9c11be005ade7c308bebdda71c7fb0111acc/storm-core/src/clj/backtype/storm/clojure.clj#L77-L138
正如我所说的,这看起来像某种线程问题。当我添加了 synchronized 关键字后,一切如常。