Java 的 JavaScript 引擎
目前我在我的应用程序中使用Rhino。我需要评估一些 JavaScript ant 从中获取值(我不需要通过 JavaScript 使用 Java 类)。但速度太慢了。也许有什么方法可以将 V8 与 Java 应用程序一起使用?
更新:
我有大量不同类型的对象。我需要一个灵活的机制来验证和转换这些对象到所需的形式(用户应该能够更改验证和转换的规则(在运行时),即在Java中硬编码这些规则不合适)。 现在,Rhino 上一切正常,但性能很差。我想过使用 NodeJS,但似乎与它通信、通过进程进行对象序列化等等 - 这些都会花费很大。
Currently I'm using Rhino in my application. I need to eval some JavaScript ant get values from it (I don't need to use Java classes through JavaScript). But it is too slow. Maybe there are any ways to use V8 with Java application?
Update:
I have a large collection of objects of different types. I need a flexible mechanism for validation and transformation these objects to the required form (the user should be able to change the rules of validation and transformation (in runtime), ie hardcoding these rules in Java not suitable).
Now everything works on Rhino, but performance is bad. I thought of using NodeJS, but it seems that communication with it, object serialization through processes, etc. - these all will cost very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您绝对确定您确实需要
eval
吗?实际上需要eval
的地方非常非常少。您可以使用
ProcessBuilder
向底层系统可用的任何进程进行 shell 处理。我想说它比 Rhinoeval
更快的可能性很低。您可以让
NodeJS
进程与您的应用程序一起运行,您通过插座。 可能会在 Rhino 中赢得与eval
的速度竞赛。如果你给出一个你实际想要实现的目标的例子,人们可能会为你想出更好的方法。
Are you absolutely sure you really need
eval
? There are very, very few places whereeval
is actually necessary.You can use
ProcessBuilder
to shell out to any process available to the underlying system. I'd say the odds of it being faster than a Rhinoeval
are low.You might keep a
NodeJS
process running alongside your app which you communicate with via a socket. That might win a speed race witheval
in Rhino.If you give an example of what you're actually trying to achieve, it may be that people can come up with a better approach for you.
看一下
javax.script.ScriptEngine
。它是一个标准的 Java 包,允许评估和数据绑定:Have a look at
javax.script.ScriptEngine
. It's a standard Java package, allows evals and data binding: