OGNL 线程安全
我将在 Struts2 范围之外重用 OGNL 库。我有相当大的公式集,这就是为什么我想预编译所有公式:
Ognl.parseExpression(expressionString);
但我不确定预编译表达式是否可以在多线程环境中使用。有人知道是否可以使用吗?
I'm going to reuse OGNL library out of Struts2 scope. I have rather large set of formulas, that is why I would like to precompile all of them:
Ognl.parseExpression(expressionString);
But I'm not sure if precompiled expression can be used in multi-thread environment. Does anybody knows if it can be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OGNL 的 PropertyUtils 代码被编写为线程安全的,所以我猜测编译的表达式应该是线程安全的。
进一步的证据是,大多数访问器 API 提供可变状态作为上下文参数(例如,请参阅 PropertyAccessor),因此类本身几乎没有可变状态。不可变类本质上是线程安全的。开发者指南敦促扩展是线程安全的,最后
查看代码,如果存在可变状态,则它在同步块中受到保护,例如,请参见 评估池。
总而言之,OGNL 似乎被设计为线程安全的。是否真的是另一个问题!您可以编写一个快速测试来确定,例如使用 Concutest。或者,如果线程数量合理,则将所有表达式存储在 ThreadLocal 中可以完全回避该问题,但会花费一点额外的内存(也可能不会,因为 OGNL 会进行表达式缓存。)
This PropertyUtils code from OGNL is written to be thread-safe, and so I would guess that compiled expressions are intended to be thread safe.
Further evidence is that most of the accessor API provide the mutable state as a context parameter (e.g. see PropertyAccessor), so the classes themselves have little mutable state. Immutable classes are intrinsicly thread-safe. The developer guide urges extensions to be thread-safe, and finally
looking through the code, where there is mutable state, it is guarded in a synchronized block, for example see EvaluationPool.
In summary, it seems OGNL has been designed to be thread-safe. Whether it actually is or not is another question! You could write a quick test to see for sure, using for example Concutest. Alternatively, if the number of threads is reasonable, storing all the expressions in a ThreadLocal sidesteps the issue altogether, at the cost of a little extra memory (or possibly not, as OGNL does expression caching.)
我认为您最好的选择是直接或通过邮件列表联系原始开发人员:
http://www.opensymphony.com/ognl/members.action
https://ognl.dev.java.net/servlets/ProjectMailingListList
该项目似乎被遗弃一段时间,所以几乎没有人知道:/
I think your best option is to contact original developers, directly or through mailing list:
http://www.opensymphony.com/ognl/members.action
https://ognl.dev.java.net/servlets/ProjectMailingListList
The project seems to be abandoned for some time, so there is hardly anybody else who knows :/