我希望能够从我的Java应用程序执行groovy脚本。如果需要,我想在运行时重新加载groovy脚本。根据他们的tutorials,我可以这样做:
long now = System.currentTimeMillis();
for(int i = 0; i < 100000; i++) {
try {
GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine("");
System.out.println(groovyScriptEngine.run("myScript.groovy", new Binding()););
} catch (Exception e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("time " + (end - now));//24 secsmyScript.groovy
"Hello-World"这可以很好地工作,并且每次我在myScript.groovy中更改一行时都会重新加载脚本。
问题是这不是时间效率,它所做的是每次都从文件中解析脚本。
还有没有别的选择?例如,一些更智能的东西,它检查脚本是否已经被解析,如果它在上次解析后没有改变,就不再解析它。
发布于 2019-05-08 05:35:57
由于注释>>而编辑的<<
正如其中一条注释中提到的,如果您需要性能,则必须将解析(这很慢)与执行(这很快)分开。
例如,对于响应式重新加载脚本源,我们可以使用java nio watch service:
import groovy.lang.*
import java.nio.file.*
def source = new File('script.groovy')
def shell = new GroovyShell()
def script = shell.parse(source.text)
def watchService = FileSystems.default.newWatchService()
source.canonicalFile.parentFile.toPath().register(watchService, StandardWatchEventKinds.ENTRY_MODIFY)
boolean keepWatching = true
Thread.start {
while (keepWatching) {
def key = watchService.take()
if (key.pollEvents()?.any { it.context() == source.toPath() }) {
script = shell.parse(source.text)
println "source reloaded..."
}
key.reset()
}
}
def binding = new Binding()
def start = System.currentTimeMillis()
for (i=0; i<100; i++) {
script.setBinding(binding)
def result = script.run()
println "script ran: $result"
Thread.sleep(500)
}
def delta = System.currentTimeMillis() - start
println "took ${delta}ms"
keepWatching = false上面启动了一个单独的监视器线程,该线程使用java监视服务来监视父目录中的文件修改,并在检测到修改时重新加载脚本源。这假设java版本7或更高版本。睡眠只是为了更容易地处理代码,如果要测量性能,应该自然地将其删除。
将字符串System.currentTimeMillis()存储在script.groovy中并运行上面的代码将使它每秒循环两次。在循环期间对script.groovy进行修改会导致:
~> groovy solution.groovy
script ran: 1557302307784
script ran: 1557302308316
script ran: 1557302308816
script ran: 1557302309317
script ran: 1557302309817
source reloaded...
script ran: 1557302310318
script ran: 1557302310819
script ran: 1557302310819
source reloaded...其中,无论何时对源文件进行更改,都会打印source reloaded...行。
我对windows不太确定,但我相信至少在linux上java会在幕后使用fsnotify系统,这应该会让文件监控部分表现出色。
需要注意的是,如果我们真的不走运,脚本变量将被两行之间的监视器线程重置:
script.setBinding(binding)
def result = script.run()这将破坏代码,因为重新加载的脚本实例将不具有绑定集。例如,我们可以使用锁来解决这个问题:
import groovy.lang.*
import java.nio.file.*
import java.util.concurrent.locks.ReentrantLock
def source = new File('script.groovy')
def shell = new GroovyShell()
def script = shell.parse(source.text)
def watchService = FileSystems.default.newWatchService()
source.canonicalFile.parentFile.toPath().register(watchService, StandardWatchEventKinds.ENTRY_MODIFY)
lock = new ReentrantLock()
boolean keepWatching = true
Thread.start {
while (keepWatching) {
def key = watchService.take()
if (key.pollEvents()?.any { it.context() == source.toPath() }) {
withLock {
script = shell.parse(source.text)
}
println "source reloaded..."
}
key.reset()
}
}
def binding = new Binding()
def start = System.currentTimeMillis()
for (i=0; i<100; i++) {
withLock {
script.setBinding(binding)
def result = script.run()
println "script ran: $result"
}
Thread.sleep(500)
}
def delta = System.currentTimeMillis() - start
println "took ${delta}ms"
keepWatching = false
def withLock(Closure c) {
def result
lock.lock()
try {
result = c()
} finally {
lock.unlock()
}
result
}这在一定程度上使代码变得复杂,但使我们免受并发问题的影响,这些问题往往很难追踪到。
https://stackoverflow.com/questions/56006532
复制相似问题