[英]Java EE7 websocket initialization - implement logic before first @OnOpen
I think this is more a design specific question, than direct coding issue. 我认为这比直接编码问题更具有设计特定性问题。
I want to implement a websocket service which serves an updated dataset from a foreign http:// resource to the clients. 我想实现一个websocket服务,它提供从外部http://资源到客户端的更新数据集。 but i want to have the data available before the first client connects, so @OnLoad
notation won't do. 但我想在第一个客户端连接之前获得数据,所以@OnLoad
表示法不会这样做。
In HttpServlet
world I would 在HttpServlet
世界我会
@Override public void init() throws...
I could not figure out a suitable way for doing so just using JSR-356 . 仅使用JSR-356,我无法找到合适的方法。 I tried with custom ServerEndpointConfig.Configurator
but that does not seem to give me access to method similar to init()
from HttpServlet
. 我尝试使用自定义ServerEndpointConfig.Configurator
但似乎没有让我访问类似于HttpServlet
init()
方法。
So my question is: 所以我的问题是:
Letting the websocket Endpoint class extend HttpServlet
gives me access to the init()
method and I can place my initial logic there. 让websocket Endpoint类扩展HttpServlet
让我可以访问init()
方法,我可以在那里放置我的初始逻辑。 Is that a suitable way for solving my problem, or have i missed something in JSR-356 which does the job elegantly and without importing mostly unused servlet packages? 这是解决我的问题的合适方式,还是我错过了JSR-356中的一些优点并且无需导入大部分未使用的servlet包的工作?
Thank you very much! 非常感谢你!
A class annotated with @ServerEndpoint("/myEndPoint")
is instantiated each time a new connection is created via @OnOpen
. 每次通过@OnOpen
创建新连接时,都会实例化一个使用@ServerEndpoint("/myEndPoint")
注释的类。 It is not a static
class nor a singleton (eg not behaves as Spring @Service
). 它不是static
类,也不是单例(例如,不像Spring @Service
)。
I have a similar problem to yours, I need to make a web socket the observer of a Spring web service (don't ask, I'm with you that is a bad architecture the problem). 我有一个类似的问题,我需要使一个Web套接字成为Spring Web服务的观察者(不要问,我和你在一起这是一个糟糕的架构问题)。 In order to make it an observer, I have to add it to the observable class, but because of the lack of an initialization for the web socket I don't have a clear spot where to add the observer, adding it in the @OnOpen
method would repeatedly add it on each new connection. 为了使它成为一个观察者,我必须将它添加到observable类,但由于缺少web socket的初始化,我没有清楚的位置添加观察者,在@OnOpen
添加它方法会在每个新连接上重复添加它。
The only solution I found is a workaround. 我找到的唯一解决方案是解决方法。 Usually a web socket class has a static Set
of the peers connected to it, you need something similar for your initialization. 通常,Web套接字类具有连接到它的对等体的static Set
,您需要类似的初始化。 Either use a static block
or a static
flag in the constructor. 在构造函数中使用static block
或static
标志。 In my case I solved with: 在我的情况下,我解决了:
private static boolean observerFlag = false;
private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());
public MyWebSocket() {
if (!observerFlag) {
observable.addObserver(this);
observerFlag = true;
}
}
And to remove the observer: 并删除观察者:
@OnClose
public void onClose(Session peer) {
peers.remove(peer);
if (peers.isEmpty()) {
observable.deleteObserver(this);
observerFlag = false;
}
}
I repeat that this is a workaround, I think that there is a more elegant solution. 我再说一遍,我认为有更优雅的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.