Java 脚本引擎在哪里使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1016128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the Java scripting engine used?
提问by Mohan Narayanaswamy
How do I effectively use the "Scripting Engine" inside Java?
如何有效地使用 Java 中的“脚本引擎”?
What are all the right use-cases to use the scripting engine?
使用脚本引擎的所有正确用例是什么?
Is there an open source project using "Scripting Engine"?
是否有使用“脚本引擎”的开源项目?
One thing comes to mind is "Closure, Functional programming" support is possible, but it is more of technical use than "Application Requirement".
我想到的一件事是“闭包,函数式编程”支持是可能的,但它比“应用程序需求”更多的是技术用途。
Configurable plugins are OK. But still so many patterns (visitor, decorator) on high level can do the same.
可配置的插件没问题。但是仍然有很多模式(访问者、装饰者)在高层可以做同样的事情。
I don't know the requirement well... how effectively it could be used in Java EE patterns... where it could complement with the existing patterns.
我不太了解需求……它在 Java EE 模式中的使用效果如何……它可以与现有模式互补。
Moreover I would like to see more answers with some business usecases. Maybe like finding a complex discount for a product during sale based on membership or location. Finding ranking for a complex algorithm. Especially why not Java in some scenario? (or C# in .NET world)
此外,我希望看到一些业务用例的更多答案。也许就像根据会员资格或位置在销售期间为产品找到复杂的折扣。查找复杂算法的排名。特别是在某些情况下为什么不是 Java?(或 .NET 世界中的 C#)
回答by Brian Agnew
In Java 6, scripting engine support is built in. For example,
在 Java 6 中,内置了脚本引擎支持。例如,
// Create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// Create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// Evaluate JavaScript code from String
engine.eval("print('Hello, World')");
Why would you use one? Some reasons:
你为什么要使用一个?一些原因:
- you have a library in a scripting language that you want to use in Java (e.g. a Python library that you could run via Jython)
- You want to provide a configurable programming mechanism for customers, such that they can provide short code snippets. For example, I've done this in the past allowing customers to write filters using JavaScript (e.g. is x < 2 and y > 5 and z > 10 ?).
- You can implement more complex logic in tools like Ant by scripting directly in the configuration file
- You can implement solutions in a language more suited to that domain (e.g. using lambdas via Clojure), but maintain your reliance on the JVM.
- 你有一个你想在 Java 中使用的脚本语言库(例如一个可以通过 Jython 运行的 Python 库)
- 您希望为客户提供可配置的编程机制,以便他们可以提供简短的代码片段。例如,我过去曾允许客户使用 JavaScript 编写过滤器(例如是 x < 2 和 y > 5 和 z > 10 ?)。
- 您可以通过直接在配置文件中编写脚本,在 Ant 等工具中实现更复杂的逻辑
- 您可以使用更适合该领域的语言来实现解决方案(例如,通过 Clojure 使用 lambdas),但要保持对 JVM 的依赖。
Implementations include Rhino (a Java implementation of Javascript), Jython (a Java Python) and many more.
实现包括 Rhino(Javascript 的 Java 实现)、Jython(Java Python)等等。
回答by Brian Agnew
Here are some cases where I've used it.
以下是我使用过的一些案例。
1) Java wants to call scripting language, example 1.I have a Java app that accepts user comments via the WMD JavaScript widget. (Same widget that StackOverflow uses, actually.) User enters comments in the Markdown format and a JavaScript library called Showdown converts it to HTML in two places: (1) on the client, to support real-time previews; and (2) on the server, since I want the client to send pure Markdown to the server and store that there so the user can edit the Markdown later (instead of having to somehow reverse the HTML into Markdown). When storing the comment on the server, I do run the conversion there as well, and I store the HTML alongside the Markdown so I don't have to dynamically convert the Markdown when displaying comment lists. To ensure that the HTML on the server matches the HTML on the client, I want to use the exact same Showdown library. So I run Showdown server-side inside the Rhino JavaScript engine.
1)Java要调用脚本语言,例子1。我有一个 Java 应用程序,它通过 WMD JavaScript 小部件接受用户评论。(实际上与 StackOverflow 使用的小部件相同。)用户以 Markdown 格式输入评论,一个名为 Showdown 的 JavaScript 库在两个地方将其转换为 HTML:(1)在客户端,支持实时预览;和 (2) 在服务器上,因为我希望客户端将纯 Markdown 发送到服务器并将其存储在那里,以便用户稍后可以编辑 Markdown(而不必以某种方式将 HTML 反转为 Markdown)。在服务器上存储评论时,我也会在那里运行转换,并将 HTML 与 Markdown 一起存储,因此在显示评论列表时我不必动态转换 Markdown。为了确保服务器上的 HTML 与客户端上的 HTML 匹配,我想使用完全相同的 Showdown 库。
2) Java wants to call scripting language, example 2.I'm working on a deployment automation application that involves stakeholders across different roles, such as developers, sysadmins and release engineers. The overall app (workflow and UI) is a Java app, but at various locations it calls various scripts (e.g. Ruby, bash), such as for pushing packages, verifying configuration, installing packages, smoke testing, etc. This is partly because script is better/more economical for expressing directory creation, copying, moving, wgetting, etc., and partly because the people who own that particular piece of the pie know how to work with scripting languages but not Java. So we invoke scripts here using Java's Scripting API. Admittedly in this case we could just execute the scripts outside of Java but see #3 below.
2) Java 想要调用脚本语言,示例 2。我正在开发一个部署自动化应用程序,该应用程序涉及不同角色的利益相关者,例如开发人员、系统管理员和发布工程师。整个应用程序(工作流和 UI)是一个 Java 应用程序,但在不同的位置调用各种脚本(例如 Ruby、bash),例如用于推送包、验证配置、安装包、冒烟测试等。这部分是因为脚本表达目录创建、复制、移动、wgetting 等更好/更经济,部分原因是拥有该特定部分的人知道如何使用脚本语言而不是 Java。所以我们在这里使用 Java 的 Scripting API 调用脚本。诚然,在这种情况下,我们可以只在 Java 之外执行脚本,但请参阅下面的 #3。
3) Scripting language wants to call Java.In the aforementioned deployment application, we have web-based deployment logs, and we put a lot of effort into making the deployment logs as easy to read and understand as possible, because a large developer/SQA/release engineer population consumes the logs, and not everybody understands all the details of what exactly goes on with a deployment. Pretty-printing and color-coding are part of the approach. We implemented a pretty-printing deployment log API in Java but we want to be able to have the scripts call that. So for example when the Ruby push script runs, we want it to log its progress to the pretty-printer. Running Ruby inside JRuby allows the Ruby script to see the Java pretty-printer API.
3)脚本语言要调用Java。在前面提到的部署应用程序中,我们有基于 Web 的部署日志,我们付出了很多努力使部署日志尽可能易于阅读和理解,因为大量的开发人员/SQA/发布工程师使用这些日志,并且并非每个人都了解部署的所有细节。漂亮的印刷和颜色编码是该方法的一部分。我们用 Java 实现了一个漂亮的打印部署日志 API,但我们希望能够让脚本调用它。因此,例如,当 Ruby 推送脚本运行时,我们希望它将其进度记录到漂亮的打印机。在 JRuby 中运行 Ruby 允许 Ruby 脚本查看 Java 漂亮打印机 API。
回答by Matthew Flaschen
"What are all the right use-case to use scripting engine?" This is a pretty vague question. There are many use cases. Here are just a few I can think of right away:
“使用脚本引擎的所有正确用例是什么?” 这是一个相当模糊的问题。有很多用例。以下是我能马上想到的几个:
- Plugin/extension system
- IDE
- Programming tutorial with live demos
- 插件/扩展系统
- 集成开发环境
- 带有现场演示的编程教程
I am assuming you are referring to JSR 223in particular. If so, you should look at scripting.dev.java.net
我假设您特别指的是JSR 223。如果是这样,您应该查看scripting.dev.java.net
回答by basszero
I haven't used JavaScript specifically, but I've integrated Groovy into my application framework to provide a domain specific language (DSL). I've created functions and classes that hook into my application.
我没有专门使用 JavaScript,但我已将 Groovy 集成到我的应用程序框架中以提供一种领域特定语言 (DSL)。我已经创建了挂钩到我的应用程序的函数和类。
The user is allowed to script common operations within the application (macros) as well as implement lightweight processing to avoid the much heavier code-compiler-jar-deploy solution. If the user has an idea for a plugin to my processing framework they can prototype via Groovy in realtime and move back to Java (maybe even native) when there is time (or when speed is needed). Keep in mind that scripting is typically orders of magnitude slower than Java/C#/C/C++
用户可以在应用程序(宏)中编写常用操作脚本,并实现轻量级处理以避免繁重的 code-compiler-jar-deploy 解决方案。如果用户对我的处理框架的插件有想法,他们可以通过 Groovy 实时进行原型设计,并在有时间(或需要速度时)返回到 Java(甚至可能是原生的)。请记住,脚本编写通常比 Java/C#/C/C++ 慢几个数量级

