花样青春 所有收视率:CometD Java 服务器服务集成

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 00:38:42

原文地址:

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

         

 

CometD Java 服务器服务集成

 

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

 

CometD Java 服务器服务集成

 

有几个方法,可以将您的 Bayeux 服务集成到web 应用程序。

 

Bayeux 对象是由一个 servlet创建的,但一般情况下,在一个Bayeux 对象被创建时,没有一个简单的方法去检测,这样的一个事实就使得所有的这些方法都很复杂,。

 

通过配置 Servlet 的集成

 

最简单方法是在初始化 web 应用程序时使用servlet配置您的 Bayeux 服务。

此servlet配置没有映射,因为它的作用仅是初始化 (或是用"线"连起来) 您的服务,确保您的 web 应用程序能正常工作。

 

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

 

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

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

        version="2.5">

 

   

       cometd

       org.cometd.server.continuation.ContinuationCometdServlet

       1

   

   

       cometd

       /cometd/*

   

 

   

       configuration

       com.acme.cometd.ConfigurationServlet

       2

   

 

 

请注意我们这样指定Cometd servlet 的为1 (这样Bayeux 对象被创建,并将其放入 ServletContext),为configuration  servlet指定值为2,而在 Cometd servlet 初始化后configuration  servlet才将会被初始化,从而使得 Bayeux 对象可用。

 

 

这是 Configuration Servlet 的代码:

 

public classConfigurationServlet 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 canconfigure the Bayeux object

        // by adding extensions or specifying aSecurityPolicy

    }

 

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

    {

        throw new ServletException();

    }

}

 

可以查看关于 EchoService的详细信息

 

通过配置监听器的集成

 

可以用配置侦听器来代替配置 servlet,但是需要编写一个类实现ServletContextAttributeListener。

 

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

 

 

 

 

 

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

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

        version="2.5">

 

   

       cometd

       org.cometd.server.continuation.ContinuationCometdServlet

       1

   

   

       cometd

       /cometd/*

   

 

   

       com.acme.cometd.BayeuxInitializer

   

 

 

BayeuxInitializer的代码是这样的:

 

public class BayeuxInitializer implementsServletContextAttributeListener

{

   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)

   {

   }

}