电竞比分网-中国电竞赛事及体育赛事平台

分享

CometD Java 服務(wù)器服務(wù)集成

 phoneone 2011-11-17

原文地址:

               http:///documentation/cometd-java/server/services/integration

         

 

CometD Java 服務(wù)器服務(wù)集成

 

sbordet 提交于星期二,2009/11/17-13:51。

 

CometD Java 服務(wù)器服務(wù)集成

 

有幾個方法,可以將您的 Bayeux 服務(wù)集成到web 應(yīng)用程序。

 

Bayeux 對象是由一個 servlet創(chuàng)建的,但一般情況下,在一個Bayeux 對象被創(chuàng)建時,沒有一個簡單的方法去檢測,這樣的一個事實就使得所有的這些方法都很復(fù)雜,。

 

通過配置 Servlet 的集成

 

最簡單方法是在初始化 web 應(yīng)用程序時使用servlet配置您的 Bayeux 服務(wù)。

servlet配置沒有映射,因為它的作用僅是初始化 (或是用""連起來) 您的服務(wù),確保您的 web 應(yīng)用程序能正常工作。

 

之后你可以找到示例 web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java./xml/ns/javaee"

         xmlns:xsi="http://www./2001/XMLSchema-instance"

         xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd"

         version="2.5">

 

    <servlet>

        <servlet-name>cometd</servlet-name>

        <servlet-class>org.cometd.server.continuation.ContinuationCometdServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>cometd</servlet-name>

        <url-pattern>/cometd/*</url-pattern>

    </servlet-mapping>

 

    <servlet>

        <servlet-name>configuration</servlet-name>

        <servlet-class>com.acme.cometd.ConfigurationServlet</servlet-class>

        <load-on-startup>2</load-on-startup>

    </servlet>

 

</web-app>

 

請注意我們這樣指定Cometd servlet <load-on-startup>1 (這樣Bayeux 對象被創(chuàng)建,并將其放入 ServletContext),為configuration  servlet指定值為2,而在 Cometd servlet 初始化后configuration  servlet才將會被初始化,從而使得 Bayeux 對象可用。

 

 

這是 Configuration Servlet 的代碼:

 

public class ConfigurationServlet extends GenericServlet

{

    public void init() throws ServletException

    {

        // Grab the Bayeux object

        Bayeux  bayeux =

(Bayeux)getServletContext().getAttribute(Bayeux.ATTRIBUTE);

        new EchoService(bayeux);

        // Create other services here

 

        // This is also the place where you can configure the Bayeux object

        // by adding extensions or specifying a SecurityPolicy

    }

 

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException

    {

        throw new ServletException();

    }

}

 

可以查看關(guān)于 EchoService的詳細信息

 

通過配置監(jiān)聽器的集成

 

可以用配置偵聽器來代替配置 servlet,但是需要編寫一個類實現(xiàn)ServletContextAttributeListener

 

之后你可以找到 web.xml 文件:

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java./xml/ns/javaee"

         xmlns:xsi="http://www./2001/XMLSchema-instance"

         xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd"

         version="2.5">

 

    <servlet>

        <servlet-name>cometd</servlet-name>

        <servlet-class>org.cometd.server.continuation.ContinuationCometdServlet</servlet-class>

        <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>com.acme.cometd.BayeuxInitializer</listener-class>

    </listener>

 

</web-app>

 

BayeuxInitializer的代碼是這樣的:

 

public class BayeuxInitializer implements ServletContextAttributeListener

{

    public void attributeAdded(ServletContextAttributeEvent event)

    {

        if (Bayeux.ATTRIBUTE.equals(event.getName()))

        {

            // Grab the Bayeux object

            Bayeux bayeux = (Bayeux)event.getValue();

            new EchoService(bayeux);

            // Create other services here

 

            // This is also the place where you can configure the Bayeux object

            // by adding extensions or specifying a SecurityPolicy

        }

    }

 

    public void attributeRemoved(ServletContextAttributeEvent event)

    {

    }

 

    public void attributeReplaced(ServletContextAttributeEvent event)

    {

    }

}

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多