在Play 2.4.2服务器中运行连续后台任务 [英] Running a continuous background task in Play 2.4.2 server
问题描述
我正在创建一个Web应用程序,该应用程序将传入事件作为字符串显示在Web浏览器上.这些事件应该来自我在程序的另一部分中定义的侦听器.这些事件将通过套接字发送到客户端.
I'm creating a web application that displays incoming events as strings onto the web browser. The events should be coming in from a listener that I have defined in another part of the program. These events will be emitted through a socket to the client side.
问题:我需要保持侦听器持续运行以接收消息.侦听器实质上是一个类似于listener.run()
的代码块,并且不会终止.现在,我正在创建一个单独的线程来运行此代码块,并且我已经配置了全局设置以在应用程序启动时运行该线程.
The problem: I need to keep the listener continually running in order to receive messages. The listener is essentially one block of code that's something like listener.run()
, and it doesn't terminate. Right now, I am making a separate thread that runs this block of code and I have configured Global settings to run the thread on application startup.
我不希望每次刷新页面时都停止任务并重新开始.服务器启动时如何使监听器正确运行?
I don't want my task to stop and start every time I refresh the page. How can I make the listener run right when the server starts?
推荐答案
在文档末尾查看安排异步任务: 查看全文