【问题标题】:Should ServerEndpointConfig.Configurator instance be thread safe?ServerEndpointConfig.Configurator 实例应该是线程安全的吗?
【发布时间】:2014-05-14 13:27:39
【问题描述】:

我想在 websocket 连接之间共享一些信息。共享数据有点复杂,所以我不能使用EndpointConfig.getUserProperties()。我想扩展ServerEndpointConfig.Configurator 并添加我自己的数据结构。(我不想覆盖其他方法,例如getEndpointInstance)。问题是我应该使用线程安全的数据结构吗?或者我可以使用任何数据结构,并且 Web 套接字容器将保证我的 ServerEndpointConfig.Configurator 实例将以安全的方式被访问。

【问题讨论】:

    标签: java multithreading jakarta-ee websocket tyrus


    【解决方案1】:

    感觉太老套了。
    配置器应该是不可变的(检查Tyrus configurator for example),而不是作为共享点。
    在 Java EE 环境中有更好的方法。
    Websocket 规范为 CDI/EJB 提供了一些支持。您可以注入 @ApplicationScoped@Singleton bean 并使用它来共享数据(bean 必须同步)。
    Tyrus samples on Github有一个共享的反例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2012-01-08
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多