原文地址: http:///documentation/cometd-java/server/services/integration
CometD Java 服務器服務集成 由 sbordet 提交于星期二,2009/11/17-13:51。 CometD Java 服務器服務集成 有幾個方法,可以將您的 Bayeux 服務集成到web 應用程序。 Bayeux 對象是由一個 servlet創(chuàng)建的,但一般情況下,在一個Bayeux 對象被創(chuàng)建時,沒有一個簡單的方法去檢測,這樣的一個事實就使得所有的這些方法都很復雜,。 通過配置 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 對象被創(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(); } } 可以查看關于 EchoService的詳細信息 通過配置監(jiān)聽器的集成 可以用配置偵聽器來代替配置 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》