创建了Bayeux服务器的重复实例

创建了Bayeux服务器的重复实例

本文介绍了创建了Bayeux服务器的重复实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CometD应用程序有问题。看起来好像在创建Bayeux服务器的多个实例。我的配置文件如下所示,我正在使用Web套接字作为Transport / GigaSpaces来部署应用程序(该应用程序使用其自己的嵌入式Jetty服务器)。只是想知道我是否在以下设置中配置错误?

I'm having a problem with my CometD Application. It looks like its creating multiple instances of the Bayeux Server. My configuration Files look like the following and i'm using Web Sockets as Transport/GigaSpaces to deploy the Application (which uses its own embedded jetty Server). Just wondering if I've misconfigured something in the following setup?

WEB.XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>CometDApplication</display-name>
    <servlet>
        <servlet-name>cometd</servlet-name>
        <servlet-class>org.cometd.server.CometdServlet</servlet-class>
        <init-param>
            <param-name>jsonContext</param-name>
            <param-value>org.cometd.server.JacksonJSONContextServer</param-value>
        </init-param>
        <init-param>
            <param-name>transports</param-name>
            <param-value>org.cometd.websocket.server.WebSocketTransport</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cometd</servlet-name>
        <url-pattern>/cometd/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.openspaces.pu.container.jee.context.ProcessingUnitContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/META-INF/spring/pu.xml</param-value>
    </context-param>
</web-app>

POM.XML:

<dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>bayeux-api</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>cometd-java-server</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>cometd-java-annotations</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>cometd-websocket-jetty</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-websocket</artifactId>
            <version>7.6.8.v20121106</version>
        </dependency>

我的应用上下文文件(pu.XML):

My Applcation Context File (pu.XML):

<bean id="Bayeux" class="org.cometd.server.BayeuxServerImpl" init-method="start" destroy-method="stop">
    <property name="options">
        <map>
            <entry key="logLevel" value="0" />
            <entry key="timeout" value="15000" />
        </map>
    </property>
    <property name="transports">
        <list>
            <bean id="websocketTransport" class="org.cometd.websocket.server.WebSocketTransport">
                <constructor-arg ref="Bayeux" />
            </bean>
            <bean id="jsonTransport" class="org.cometd.server.transport.JSONTransport">
                <constructor-arg ref="Bayeux" />
            </bean>
            <bean id="jsonpTransport" class="org.cometd.server.transport.JSONPTransport">
                <constructor-arg ref="Bayeux" />
            </bean>
        </list>
    </property>
</bean>


<bean id="ContextExporter" class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="org.cometd.bayeux">
                <ref local="Bayeux" />
            </entry>
        </map>
    </property>
</bean>


推荐答案

您实际上是在创建两个<$ c $的实例c> BayeuxServer ,由您在 web.xml 中定义的 CometdServlet 创建的一个

You are indeed creating two instances of BayeuxServer, one created by the CometdServlet you define in web.xml and one created by Spring.

类似于,如果您使用Spring初始化CometD,则整个 BayeuxServer 配置必须在Spring中。
不要在 web.xml 中复制它。

Like stated in the CometD and Spring integration documentation, if you use Spring to initialize CometD, then your whole BayeuxServer configuration must be in Spring.Don't duplicate it in web.xml.

此外,因为您定义了< load-on-startup> 元素, CometdServlet 之前初始化,而Spring的 ContextLoaderListener ,在Spring有机会创建自己的实例并将其导出之前创建一个 BayeuxServer 实例。

Furthermore, since you define a <load-on-startup> element, the CometdServlet is initialized before Spring's ContextLoaderListener, creating a BayeuxServer instance before Spring gets the chance to create its own and export it.

web.xml 中删除​​所有< init-param> $ c>< load-on-startup> ,您应该会很好:该servlet将被延迟初始化,并将找到由Spring创建的 BayeuxServer 可以正确导出,而无需创建其他实例。

Remove all <init-param> from web.xml, remove <load-on-startup> and you should be good: the servlet will be lazily initialized and will find the Spring-created BayeuxServer exported correctly, without creating an additional instance.

这篇关于创建了Bayeux服务器的重复实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:50