原文地址: http:///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: <?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 对象被创建,并将其放入 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(); } } 可以查看关于 EchoService的详细信息 通过配置监听器的集成 可以用配置侦听器来代替配置 servlet,但是需要编写一个类实现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)
{
} } |
|
来自: phoneone > 《CometD 2.x》