我想你最好考虑让事件驱动,而不是定期考虑性能和架构,如果可能的话。首先要通过使用该DBMS的触发器(AFAIK,某些DBMS通过触发器提供执行的Web服务提供执行的Web服务,但我不确定是否需要它)或原始应用程序的侦听器来通知您的应用程序服务器@PostPersist(如果它在同一个Java进程中,你可以使用一些静态或DI驱动的监听器,否则你可以执行HTTP请求)。
现在您正在监听数据库的更改,您需要一种方法来通知感兴趣的客户端该更改。您可以选择任何称为WebSocket或Comet的技术来实现这一点。然而,最简单的选择是在我看来采用这种用法的框架。正如我是Cettia的作者,我给你一个用Cettia编写的例子,但是你可以像atmosphere-play这样的其他项目做类似的工作。
// Assumes you are notified of database changes through this listener
// However, it doesn't matter
public class SomeEntityListener {
// Assumes you are using Dependency Injection framework like Spring or CDI
// so you can inject io.cettia.Server to anywhere you want to handle clients
@Inject
Server server;
// This method is called after persisting of some entity
@PostPersist
public void postPersist(SomeEntity e) {
// Broadcasts just a persisted entity to interested clients
// No matter how you are notified of database event, only this line is necessary
server.byTag("someentity-subscribers-postpersist", e);
}
}
关于如何安装瑟提亚,见this quick start guide和a working example for Play。当然,你可以用香草WebSocket做到这一点,但它不是银弹。 (这就是为什么这么多的框架出现在各种语言和平台)