有人可以建议解决这个问题的最佳方法是什么:
我喜欢做的是对数据库表进行连续轮询(或查询)。每当发生新的更新时,我都想将其推送到 Java 应用程序中的客户端。这个想法是用户可以建议他们想要订阅哪些数据,并定义一个事件处理程序来处理任何传入的数据。
因此,我需要在后台进行轮询。但我似乎无法正确执行此操作。我正在考虑为本地 JVM 发布/订阅使用一个简单的事件总线,但我似乎无法进行异步连续轮询。
Could someone suggest what’s the best approach to go about this:
What I like to do is draw a continuous poll (or query) of a database table. And anytime a new update has occurred, I want to push this to the client in a Java application. The idea is users can suggest what data they want to subscribe to and define an event-handler to handle any incoming data.
I therefore need to have the polling being done in the background. But I seem to having trouble with doing this correctly. I’m looking at using a simple event bus for local JVM pub/sub, but I can’t seem to get the asynchronous continuous polling going.
发布评论
评论(2)
您的解决方案有很多缺点。
更好的解决方案是创建负责在数据库中保存数据和通知的业务逻辑。例如,您可以创建订阅 JMS 主题的应用程序。另一个组件接收处理和保存数据的调用。它存储数据,然后发送此操作的通知。订阅该主题的所有组件都会收到此通知并做出相应反应。
Your solution has a lot of disadvantages.
A better solution is to create business logic that is responsible for saving data in the database and for notifications. You can for example create an application that is subscribed to JMS topic. Another component receives calls to process and save data. It stores data and then sends notification of this action. All components subscribed to the topic receive this notification and react accordingly.
看一下 java.util.concurrent 。您可以创建一个调度程序,利用线程池以给定的时间间隔运行,例如 ScheduledThreadPoolExecutor。
Have a look at java.util.concurrent. You can create a scheduler to run at a given interval utilizing a thread pool like with a ScheduledThreadPoolExecutor.