【转】java即时消息推送
整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp
Comet4J目前仅支持Tomcat6、7版本,根据您所使用的Tomcat版本下载【comet4jtomcat6.jar】或【comet4j-tomcat7.jar】文件放置到WEB项目的WEB-INF\lib目录下。
本文下载comet4j-tomcat7.jar, 下载地址:http://pan.baidu.com/s/1eSBXmfS
下载客户端js文件
下载【comet4j.js】到您的项目中,比如:WebContent\JS\cometd目录下。
下载地址:http://pan.baidu.com/s/1qXYxkYg
修改服务器配置文件
因为Comet4J工作在NIO方式下,所以我们需要调整服务器连接器配置,更换为NOI连接器。 打开server.xml文件将找到原先的连接器配置:
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
替换为
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
客户端
我们利用Comet4J开发一个每隔一秒向所有客户端推送一个定时增长的数字。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Cometd首页</title> <script type="text/javascript" src="./JS/cometd/comet4j.js"></script> <script type="text/javascript"> var projectPath = '<%= request.getContextPath()%>'; function cometdCallback(text){ var kbDom = document.getElementById('num1'); kbDom.innerHTML = text; } function init() { JS.Engine.on({ number : cometdCallback }); JS.Engine.start(projectPath+'/conn'); } </script> </head> <body onload="init()"> 数字3: <span id="num1">...</span> </body> </html>
其中,<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径。
package com.cvicse.ump.cometd; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.comet4j.core.CometContext; public class CometdTestClass implements ServletContextListener { private static final String CHANNEL_NUM = "number"; private static int number = 0; @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent arg0) { CometContext.getInstance().registChannel(CHANNEL_NUM); Thread timeThread = new Thread(new TimeThread(),"numTimeThread"); timeThread.setDaemon(true);//设置为守护线程 timeThread.start(); } class TimeThread implements Runnable{ @Override public void run() { while(true){ CometContext.getInstance().getEngine().sendToAll(CHANNEL_NUM, number++); System.out.println(CHANNEL_NUM+": "+number); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
配置
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>cometd</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <listener> <description>Comet4J容器侦听</description> <listener-class>org.comet4j.core.CometAppListener</listener-class> </listener> <listener> <listener-class>com.cvicse.ump.cometd.CometdTestClass</listener-class> </listener> <servlet> <description>Comet连接[默认:org.comet4j.core.CometServlet]</description> <servlet-name>CometServlet</servlet-name> <servlet-class>org.comet4j.core.CometServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CometServlet</servlet-name> <url-pattern>/conn</url-pattern> </servlet-mapping> </web-app>
特此感谢:http://blog.csdn.net/u012726702/article/details/73928421