使用Java EE监听由不同层进行的数据库更改

时间:2016-01-19 14:34:58

标签: database java-ee websocket real-time

我有2个独立的模块(服务和Web)。他们唯一的共同点是数据库。

Web模块通过websocket连接连接到客户端浏览器。然后它可以随时发送消息。但我希望该客户端获取有关服务层所做的每个数据库更改的实时日志。

         ___  Service Layer
        /
DATABASE
        \___  Web module <--> Client browser

我看到了this solution,但首先,使用

beanManager仍为null
@Inject

CDI.current().getBeanManager().fireEvent(new MenuChangeEvent(menu))

第二,在下面的代码中,我应该在哪里调用@Observes更改的函数?

@ServerEndpoint("/dashboard")   // ws://localhost/app/dashboard
public class DashboardController extends EmptyInterceptor {

    private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());

    @OnOpen
    public void onOpen(Session session) {
        sessions.add(session);
    }

    @OnClose
    public void onClose(Session session) {
        sessions.remove(session);
    }

    public void sendMessage(String message) {
        for (Session session : sessions) {
            session.getAsyncRemote().sendText(message);
        }
    }

}

或者你们有其他解决方案吗? (我想避免使用数据库触发器)

0 个答案:

没有答案