在Tomcat上运行彗星的websocket传输的“未知Bayeux传输”

时间:2019-05-23 13:53:43

标签: websocket java-8 cometd tomcat8.5

我正在尝试在我的Web应用程序中将CometD与websocket传输一起使用,但是与这种传输的握手失败,并显示错误“未知的Bayeux传输”,并且CometD总是回退到长轮询。

我正在使用

  • Java 8
  • CometD 3.1.8和Jetty 9.4.14.v20181114库
  • Tomcat 8.5作为容器

这是 web.xml

<servlet>
  <servlet-name>cometd</servlet-name>
  <servlet-class>org.cometd.server.CometDServlet</servlet-class>
  <init-param>
    <param-name>jsonContext</param-name>
    <param-value>com.myapp.MyAppJettyJSONContextServer</param-value>
  </init-param>
  <init-param>
    <param-name>allowedTransports</param-name>
    <param-value>websocket,long-polling</param-value>
  </init-param>
  <init-param>
    <param-name>transports</param-name>
    <param-value>org.cometd.websocket.server.JettyWebSocketTransport,org.cometd.server.transport.AsyncJSONTransport</param-value>
  </init-param>
  <init-param>
    <param-name>ws.cometdURLMapping</param-name>
    <param-value>/cometd/*</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
  <servlet-name>cometd</servlet-name>
  <url-pattern>/cometd/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>configuration</servlet-name>
    <servlet-class>com.myapp.MyAppConfigurationServlet</servlet-class>
    <init-param>
      <param-name>SERVER_URL</param-name>
      <param-value>http://localhost:8380/myapp/cometd</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

<filter>
  <filter-name>cross-origin</filter-name>
  <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
  <async-supported>true</async-supported>
</filter>
<filter-mapping>
  <filter-name>cross-origin</filter-name>
  <url-pattern>/cometd/*</url-pattern>
</filter-mapping>

这是 MyAppConfigurationServlet.java

的代码
public class MyAppConfigurationServlet extends GenericServlet
{

    private static final long serialVersionUID = 6918716199131599352L;

    private static final Logger log = LoggerFactory.getLogger(MyAppConfigurationServlet.class);

    public static final String PUSH_SERVICE = "pushService";

    public static final String CONFIG_SERVER_URL = "SERVER_URL";

    @Override
    public void init() throws ServletException
    {
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        BayeuxServer bayeux = (BayeuxServer) getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
        String serverUrl = getInitParameter(CONFIG_SERVER_URL);
        log.debug("Creating PushService at url {}", serverUrl);
        PushService pushService = new PushService(bayeux, serverUrl);
        sharePushService(ctx, pushService);
    }

    private void sharePushService(WebApplicationContext ctx, PushService pushService)
    {
        try
        {
            PushServiceHolder container = (PushServiceHolder) ctx.getBean(PUSH_SERVICE);
            container.setPushService(pushService);
        }
        catch (BeansException e)
        {
            log.warn("Cannot find the bean {});
            log.trace("Error!", e);
        }
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
        throw new ServletException();
    }

}

其中PushServiceHolder是一个简单的spring bean,它使PushService可用于其他bean。

这是客户端实施

Map<String, Object> options = new HashMap<>();
JettyJSONContextClient jsonContext = new MyAppJettyJSONContextClient();
options.put(ClientTransport.JSON_CONTEXT_OPTION, jsonContext);

HttpClient httpClient = new HttpClient();
httpClient.setMaxConnectionsPerDestination(2);
httpClient.setConnectTimeout(60000);
httpClient.start();

WebSocketClient webSocketClient = new WebSocketClient(httpClient);
webSocketClient.start();
ClientTransport wsTransport = new JettyWebSocketTransport(options, null, webSocketClient);
BayeuxClient client = new BayeuxClient(url, wsTransport, longPollingTransport);
client.handshake();
client.waitFor(5000, BayeuxClient.State.CONNECTED);

对于Websocket传输,握手永远不会成功,带有 404 http错误代码,因此CometD在长轮询时会回退。

调试到org.cometd.server.CometDServlet中,我发现了这一部分

@Override
protected void service(HttpServletRequest request, HttpServletResponse     response) throws ServletException, IOException {
    if ("OPTIONS".equals(request.getMethod())) {
        serviceOptions(request, response);
        return;
    }

    AbstractHttpTransport transport = _bayeux.findHttpTransport(request);
    if (transport == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown Bayeux Transport");
    } else {
        transport.handle(request, response);
    }
}

org.cometd.websocket.server.JettyWebSocketTransport不是AbstractHttpTransport,因此即使将其正确配置为允许的传输,此代码也不会考虑它。
因此,Websocket传输的握手请求始终以错误“未知的Bayeux传输” 结束。

我已经使用CometD文档herehere来创建web.xml
我想看看CometD maven primer,这是CometD团队多次建议的,但是命令

$ mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -DarchetypeCatalog=http://cometd.org 

返回此错误

[WARNING] Error reading archetype catalog http://cometd.org
org.apache.maven.wagon.TransferFailedException: URI does not specify a valid host name: http:/cometd.org
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:1069)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:963)
at org.apache.maven.wagon.StreamWagon.getInputStream (StreamWagon.java:126)
at org.apache.maven.wagon.StreamWagon.getIfNewer (StreamWagon.java:88)
at org.apache.maven.wagon.StreamWagon.get (StreamWagon.java:61)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.downloadCatalog (RemoteCatalogArchetypeDataSource.java:119)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.getArchetypeCatalog (RemoteCatalogArchetypeDataSource.java:87)
at org.apache.maven.archetype.DefaultArchetypeManager.getRemoteCatalog (DefaultArchetypeManager.java:216)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.getArchetypesByCatalog (DefaultArchetypeSelector.java:218)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.selectArchetype (DefaultArchetypeSelector.java:71)
at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute (CreateProjectFromArchetypeMojo.java:181)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.wagon.providers.http.httpclient.client.ClientProtocolException: URI does not specify a valid host name: http:/cometd.org
at org.apache.maven.wagon.providers.http.httpclient.impl.client.CloseableHttpClient.determineTarget (CloseableHttpClient.java:95)
at org.apache.maven.wagon.providers.http.httpclient.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:83)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.execute (AbstractHttpClientWagon.java:825)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:986)
at org.apache.maven.wagon.providers.http.wagon.shared.AbstractHttpClientWagon.fillInputData (AbstractHttpClientWagon.java:963)
at org.apache.maven.wagon.StreamWagon.getInputStream (StreamWagon.java:126)
at org.apache.maven.wagon.StreamWagon.getIfNewer (StreamWagon.java:88)
at org.apache.maven.wagon.StreamWagon.get (StreamWagon.java:61)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.downloadCatalog (RemoteCatalogArchetypeDataSource.java:119)
at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.getArchetypeCatalog (RemoteCatalogArchetypeDataSource.java:87)
at org.apache.maven.archetype.DefaultArchetypeManager.getRemoteCatalog (DefaultArchetypeManager.java:216)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.getArchetypesByCatalog (DefaultArchetypeSelector.java:218)
at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.selectArchetype (DefaultArchetypeSelector.java:71)
at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute (CreateProjectFromArchetypeMojo.java:181)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)

我的配置中缺少什么? CometD原型目录的正确地址是什么?

非常感谢

1 个答案:

答案 0 :(得分:0)

通常,CometD建议在Jetty中进行部署并使用最新版本(目前为4.0.4)。

您的web.xml的{​​{1}} transports值不正确。

init-param是CometD传输,在其实现中使用特定于Jetty的API。因此,它仅在您的应用程序部署在Jetty中时有效。

我建议您删除JettyWebSocketTransport transports并让CometD选择适当的默认值,这将在Jetty和Tomcat中都适用。

作为记录,默认的WebSocket传输是init-param,它基于由Jetty和Tomcat实施的标准JSR 356 API。

您的客户端是正确的,尽管您可能还想使用标准的WebSocket传输org.cometd.websocket.server.WebSocketTransport

我尝试使用该命令从原型生成项目,并且对我来说效果很好。 也许您对此错误有一个提示:org.cometd.websocket.client.WebSocketTransport-看看怎么只有1个斜杠,而不是必需的2个斜杠。

总结:

  • 移动到CometD 4.0.x
  • 如果可以的话,移到码头
  • 删除URI does not specify a valid host name: http:/cometd.org配置并使用默认设置。
  • 使用正确的URL:transports重新运行原型。