我尝试在端点中获得http会话。我遵循了这个建议https://stackoverflow.com/a/17994303。这就是我为什么这么做的原因:
public class MyConfigurator extends ServerEndpointConfig.Configurator
{
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request,
HandshakeResponse response)
{
HttpSession httpSession = (HttpSession)request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
和
@ServerEndpoint(value = "/foo", configurator = MyConfigurator.class)
public class MyEndpoint {
private Session wsSession;
private HttpSession httpSession;
@OnOpen
public void open(final Session session,EndpointConfig config) {
this.wsSession=session;
this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
}
}
这就是我得到的
java.lang.RuntimeException: Cannot load platform configurator
at javax.websocket.server.ServerEndpointConfig$Configurator.fetchContainerDefaultConfigurator(ServerEndpointConfig.java:123)
at javax.websocket.server.ServerEndpointConfig$Configurator.getContainerDefaultConfigurator(ServerEndpointConfig.java:128)
at javax.websocket.server.ServerEndpointConfig$Configurator.checkOrigin(ServerEndpointConfig.java:192)
at org.eclipse.jetty.websocket.jsr356.server.JsrCreator.createWebSocket(JsrCreator.java:88)
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:187)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:207)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)
at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:70)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180)
at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:276)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
at org.eclipse.jetty.server.Server.handle(Server.java:524)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
at java.lang.Thread.run(Thread.java:745)
我使用osgi, jetty 9.3.11和pax-web 6.0.0
问题是javax。websocket-api jar没有设置为使用osgi中的ServiceLoader机制,所以它可以找到自定义的Configurator实例。
为了使其在osgi中工作,在javax。Websocket-api jar需要有这些行:
Require-Capability: osgi.serviceloader;filter:="(osgi.serviceloader=javax.websocket.server.ServerEndpointConfig.Configurator)";resolution:=optional;cardin
ality:=multiple,osgi.extender;filter:="(osgi.extender=osgi.serviceloa
der.processor)"
因此,由于它没有这些行,您将需要添加另一个带有清单的捆绑包,其中包含这些行,并将自己声明为javax的片段。websocket-api包。
如果你正在使用maven,那么你需要的代码行是这样的:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${bundle-symbolic-name};singleton:=true</Bundle-SymbolicName>
<Bundle-Name>OSGi Websocket API Fragment</Bundle-Name>
<Fragment-Host>javax.websocket-api</Fragment-Host>
<Require-Capability>osgi.serviceloader; filter:="(osgi.serviceloader=)javax.websocket.server.ServerEndpointConfig.Configurator";resolution:=optional;cardinality:=multiple, osgi.extender; filter:="(osgi.extender=osgi.serviceloader.processor)"</Require-Capability>
</instructions>
</configuration>
</plugin>
您可以安装mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.javax-websocket-api/1.1_1
包,它是原始javax的重新打包。websocket-api,但有适当的OSGi描述符。更多信息请访问:http://mavi.logdown.com/posts/7813065-deploying-vaadin-app-on-karaf
这是创建我的片段修复工作:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId};singleton:=true</Bundle-SymbolicName>
<Bundle-Name>Websocket API OSGi Fix</Bundle-Name>
<Fragment-Host>javax.websocket-api</Fragment-Host>
<Require-Capability>
osgi.serviceloader;osgi.serviceloader="javax.websocket.server.ServerEndpointConfig$Configurator"
</Require-Capability>
</instructions>
</configuration>
</plugin>
这不是答案,但我们认为它可能会帮助其他遇到同样问题的用户:我们有与OP描述的相同的问题,但"可接受的答案"并没有解决它。也就是说,我们将描述的条目精确地添加到指定jar中的Manifest中,并且我们继续得到相同的异常(与OP列出的异常相同)。
我们设法解决了问题如下。在Dropwizard initialize()方法中,我们有如下代码:
@Override
public void initialize(Bootstrap<AppConfig> bootstrap) {
...
bootstrap.addBundle(new WebsocketBundle(MyWebsocketHandler.class));
...
}
在跟踪Dropwizard代码之后,我们发现我们正在调用的WebsocketBundle构造函数将null赋值给默认配置器,尽管MyWebsocketHandler类上的@ServerEndpoint注释指定了一个配置器类,如下所示:
@ServerEndpoint(value="/myWebsocketPath",configurator=CustomEndpointConfigurator.class)
将initialize()中的构造函数调用改为
bootstrap.addBundle(new WebsocketBundle(new CustomEndpointConfigurator(), TeamWebsocketHandler.class));
和poof,异常消失。(它确实导致了一连串新的"noclassdefffound"异常,但这些异常很容易找到。在安装了包含所有缺失类的缺失库之后,应用程序websockets开始按预期运行。
这是完全可能,我们已经完全误解了一些关于Dropwizard如何工作(或应该工作);当涉及到这个领域时,我们项目中的所有人都是严肃的新手,我们欢迎任何解释为什么我们所做的是"错误的"或"没有意义的"。但我们认为,当"公认的答案"不起作用时,它可能会帮助其他人发现一些对我们有用的东西。
在我的情况下,我混淆了javax.websocket
与proguard。下面的代码为我修复了这个问题:
-keep class javax.websocket.** { *; }