對于頁面一直監控,以前都是使用ajax請求即可,但是小并發這做法沒多大問題,但是到了大并發就不太合適,如果不想自己寫線程來操控就可以偷懶找一些插件,例如comet4j
下面我來演示下如何使用這個插件
先準備需要的工具:
comet4j-tomcat6.jar(tomcat6的就導入這個)
comet4j-tomcat7.jar(tomcat7的就導入這個)
comet4j.js(頁面引入這個js)
具體操作看下面
然后就寫個class
- package com.shadow.extras.comet4j;
-
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- import org.comet4j.core.CometContext;
- import org.comet4j.core.CometEngine;
-
- public class TestComet implements ServletContextListener {
- private static final String CHANNEL = "test";
- private static final String CHANNEL2 = "test2";
-
- public void contextInitialized(ServletContextEvent arg0) {
- CometContext cc = CometContext.getInstance();
- cc.registChannel(CHANNEL);// 注冊應用的channel
- cc.registChannel(CHANNEL2);
-
- Thread helloAppModule = new Thread(new HelloAppModule(),
- "Sender App Module");
- // 是否啟動
- helloAppModule.setDaemon(true);
- // 啟動線程
- helloAppModule.start();
-
- Thread helloAppModule2 = new Thread(new HelloAppModule2(),
- "Sender App Module");
- // 是否啟動
- helloAppModule2.setDaemon(true);
- // 啟動線程
- helloAppModule2.start();
- }
-
- class HelloAppModule2 implements Runnable {
- public void run() {
- while (true) {
- try {
- // 睡眠時間
- Thread.sleep(5000);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- CometEngine engine = CometContext.getInstance().getEngine();
- // 獲取消息內容
- long l = getFreeMemory();
- // 開始發送
- engine.sendToAll(CHANNEL2, l);
- }
- }
- }
-
- class HelloAppModule implements Runnable {
- public void run() {
- while (true) {
- try {
- // 睡眠時間
- Thread.sleep(2000);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- CometEngine engine = CometContext.getInstance().getEngine();
- // 獲取消息內容
- long l = getFreeMemory();
- // 開始發送
- engine.sendToAll(CHANNEL, l);
- }
- }
- }
-
- public void contextDestroyed(ServletContextEvent arg0) {
-
- }
-
- public long getFreeMemory() {
- return Runtime.getRuntime().freeMemory() / 1024;
- }
- }
然后再寫個頁面
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Comet4J Hello World</title>
- <script type="text/javascript" src="plugin/comet4j/comet4j.js"></script>
- <script type="text/javascript">
- function init(){
- var kbDom = document.getElementById('kb');
- var kbDom2 = document.getElementById('kb2');
- JS.Engine.on({
- test : function(aa){//偵聽一個channel
- kbDom.innerHTML = aa;
- },
- test2 : function(bb){
- kbDom2.innerHTML = bb;
- }
- });
- JS.Engine.start('comet');
- }
- </script>
- </head>
- <body onload="init()">
- 剩余內存:<span id="kb">...</span>KB <br/>
- 剩余內存:<span id="kb2">...</span>KB
- </body>
- </html>
接著配置下web.xml就ok了
- <!-- comet4j -->
- <listener>
- <description>Comet4J容器偵聽</description>
- <listener-class>org.comet4j.core.CometAppListener</listener-class>
- </listener>
- <servlet>
- <description>Comet連接[默認:org.comet4j.core.CometServlet]</description>
- <display-name>CometServlet</display-name>
- <servlet-name>CometServlet</servlet-name>
- <servlet-class>org.comet4j.core.CometServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>CometServlet</servlet-name>
- <url-pattern>/comet</url-pattern>
- </servlet-mapping>
-
- <listener>
- <description>TestComet</description>
- <listener-class>com.shadow.extras.comet4j.TestComet</listener-class>
- </listener>
最后修改下tomcat的server.xml文件
把protocol參數值改成下面的,因為這是基于nio開發的插件
- <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
測試,很簡單就是訪問我們剛剛創建的test.html,然后就可以看到內存數值一直自動刷新波動
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請
點擊舉報。