从另一个 servlet 获取 Primefaces EventBus
Get Primefaces EventBus from another servlet
我正在使用 Primefaces Websocket 功能;它是使用 Atmosphere 框架实现的。
我在我的 webapp 中定义了两个 servlet:第一个用于前端(我正在使用 Primefaces 和 websocket 功能),第二个用于 API 功能(我在那里使用 Jersey 和 JAXRS 功能)。
我需要做的是:当一个请求被 posted 到资源(使用 API servlet)时,我需要使用 websockets 向所有连接的客户端发送消息。
在第二个 servlet 上,在拦截 post 请求的方法中,我尝试了这个解决方案:
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("subscriber/*", "This message is for all connected clients, through websockets");
它不起作用,似乎 EventBus
实例提供程序由 EventBusFactory
提供,与所有用户都连接的实例提供程序不同。消息未广播。
怎么可能?
如果我尝试从第一个 servlet 广播消息,在客户端使用 websockets 的情况下,一切正常。
我已经解决了这个问题。
只需使用
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("subscriber/*", "This message is for all connected clients, through websockets");
在第二个 servlet 上,EventBus
将得到解决。
我犯了语法错误,所以第二个 servlet 没有正确解析 EventBus
。
因此,请注意编写正确的订阅者主题,在我的例子中 "subscriber/*"
,以拦截在第三方 servlet 中创建的所有端点。
我正在使用 Primefaces Websocket 功能;它是使用 Atmosphere 框架实现的。
我在我的 webapp 中定义了两个 servlet:第一个用于前端(我正在使用 Primefaces 和 websocket 功能),第二个用于 API 功能(我在那里使用 Jersey 和 JAXRS 功能)。
我需要做的是:当一个请求被 posted 到资源(使用 API servlet)时,我需要使用 websockets 向所有连接的客户端发送消息。
在第二个 servlet 上,在拦截 post 请求的方法中,我尝试了这个解决方案:
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("subscriber/*", "This message is for all connected clients, through websockets");
它不起作用,似乎 EventBus
实例提供程序由 EventBusFactory
提供,与所有用户都连接的实例提供程序不同。消息未广播。
怎么可能?
如果我尝试从第一个 servlet 广播消息,在客户端使用 websockets 的情况下,一切正常。
我已经解决了这个问题。 只需使用
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("subscriber/*", "This message is for all connected clients, through websockets");
在第二个 servlet 上,EventBus
将得到解决。
我犯了语法错误,所以第二个 servlet 没有正确解析 EventBus
。
因此,请注意编写正确的订阅者主题,在我的例子中 "subscriber/*"
,以拦截在第三方 servlet 中创建的所有端点。