【发布时间】:2023-03-28 06:59:01
【问题描述】:
我希望能够从 Web 应用程序启动/暂停/退出后台进程。我希望进程无限期地运行。
用户会访问一个网页。按一个按钮启动线程,它会一直运行,直到用户告诉它停止。
我正在尝试确定执行此操作的最佳工具。我看过 Quartz 之类的东西,但我没有看到任何关于 Quartz 之类的东西是否适合无限期线程的讨论。
我的第一个想法是做这样的事情。
public class Background implements Runnable{
private running = true;
run(){
while(running){
//processing
}
}
stop(){
running = false;
}
}
//Then to start
Background background = new Background();
new Thread(background).start();
getServletContext().setAttribute("background", background);
//Then to stop
background = getServletContext().getAttribute("background");
background.stop();
我将对此进行测试。但我很好奇是否有更好的方法来实现这一点。
【问题讨论】:
-
回答满意吗?
标签: java jakarta-ee web-applications glassfish