gpt4 book ai didi

servlets - 有关在Grails中进行异步处理的简单方法的建议

转载 作者:行者123 更新时间:2023-12-04 03:16:58 25 4
gpt4 key购买 nike

假设我有一个像这样的简单 Controller :

class FooController {

def index = {
someVeryLongCompution() //e.g crawl a set of web pages
render "Long computation was launched."
}
}

当调用index操作时,我希望该方法在异步运行长计算时立即返回给用户。

我知道最可靠的方法是在体系结构中使用消息代理,但是我想知道是否存在更简单的方法。

我尝试了Executor插件,但这阻止了http请求返回,直到完成了长时间的计算。

我尝试了Quartz插件,但这似乎对定期任务很有用(除非有一种方法可以只运行一次作业?)

你们如何在Grails中处理此类请求?

最佳答案

您想在同一Grails服务器或另一台服务器上的何处处理veryLongComputation()?

如果是同一台服务器,则不需要JMS,另一种选择是只创建一个新线程并异步处理计算。

def index = {
def asyncProcess = new Thread({
someVeryLongComputation()
} as Runnable )
asyncProcess.start()

render "Long computation was launched."
}

关于servlets - 有关在Grails中进行异步处理的简单方法的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6163514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com