Tomcat提供了一种高级IO,可以实现Comet的功能。
Servlet需要实现 CometProcessor接口,并且设置Tomcat连接器为org.apache.coyote.http11.Http11NioProtocol
下面代码实现的功能还是获取服务器JVM信息。
前台页面:
Servlet需要实现 CometProcessor接口,并且设置Tomcat连接器为org.apache.coyote.http11.Http11NioProtocol
下面代码实现的功能还是获取服务器JVM信息。
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Date;
- import java.util.Queue;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.catalina.comet.CometEvent;
- import org.apache.catalina.comet.CometProcessor;
-
- @WebServlet(urlPatterns = { "/comet.do" })
- public class InitServlet extends HttpServlet implements CometProcessor, Runnable {
- Queue<HttpServletResponse> queue = new ConcurrentLinkedQueue<HttpServletResponse>();
-
- ScheduledExecutorService singleThread = Executors.newSingleThreadScheduledExecutor();
- {
- singleThread.scheduleWithFixedDelay(this, 3, 3, TimeUnit.SECONDS);
- }
-
- private String htmlEscape(String message) {
- return "" + message.replaceAll("\n", "").replaceAll("\r", "") + "\");\n";
- }
-
- private String getJVMInfo() {
- long useMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
- useMemory = useMemory / 1024;
- return "已用内存:" + String.valueOf(useMemory) + "K";
- }
-
- @Override
- public void run() {
- System.out.println("发送广播通知");
- String data = "广播通知\n" + new Date() + "\n" + getJVMInfo();
- for (HttpServletResponse response : queue) {
- try {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter writer = response.getWriter();
- writer.println(htmlEscape(data));
- writer.flush();
- } catch (IOException e) {
- queue.remove(response);
- e.printStackTrace();
- }
-
- }
- }
-
- @Override
- public void event(CometEvent event) throws IOException, ServletException {
- HttpServletRequest request = event.getHttpServletRequest();
- HttpServletResponse response = event.getHttpServletResponse();
-
- String name = request.getParameter("name");
- if (event.getEventType() == CometEvent.EventType.BEGIN) {
- event.setTimeout(Integer.MAX_VALUE);
- queue.add(response);
- } else if (event.getEventType() == CometEvent.EventType.ERROR) {
- queue.remove(event);
- event.close();
- } else if (event.getEventType() == CometEvent.EventType.END) {
- queue.remove(event);
- event.close();
- } else if (event.getEventType() == CometEvent.EventType.READ) {
- }
- }
- }
<!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">
<script type="text/javascript"
src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
var connection = {
iframe : false,
server : 'comet.do',
init : function() {
connection.iframe = document.createElement('iframe');
connection.iframe.setAttribute("id", "comet_iframe");
with (connection.iframe.style) {
left = top = "-100px";
height = width = "1px";
visibility = "hidden";
display = 'none';
}
$('body').append(connection.iframe)
},
request : function() {
var name = $('#name').val();
connection.iframe.setAttribute("src", connection.server + '?name='
+ name);
},
callback : function(data) {
$('#content').html(data);
}
}
$(document).ready(function() {
connection.init();
});
</script>
</head>
<body>
<input type="text" id="name" />
<input type="button" value="start" onclick="connection.request();" />
<div id="content"></div>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript"
src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
var connection = {
iframe : false,
server : 'comet.do',
init : function() {
connection.iframe = document.createElement('iframe');
connection.iframe.setAttribute("id", "comet_iframe");
with (connection.iframe.style) {
left = top = "-100px";
height = width = "1px";
visibility = "hidden";
display = 'none';
}
$('body').append(connection.iframe)
},
request : function() {
var name = $('#name').val();
connection.iframe.setAttribute("src", connection.server + '?name='
+ name);
},
callback : function(data) {
$('#content').html(data);
}
}
$(document).ready(function() {
connection.init();
});
</script>
</head>
<body>
<input type="text" id="name" />
<input type="button" value="start" onclick="connection.request();" />
<div id="content"></div>
</body>
</html>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29254281/viewspace-1065449/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29254281/viewspace-1065449/