CometProcessor 接口要求實現 event 方法
在清單
清單
private class MessageSender implements Runnable {
protected boolean running = true;
protected final ArrayList messages = new ArrayList();
private ServletResponse connection;
private synchronized void setConnection(ServletResponse connection){
nnection = connection;
notify();
}
public void send(String message) {
synchronized (messages) {
messages
log(
messages
}
}
public void run() {
while (running) {
if (messages
try {
synchronized (messages) {
messages
}
} catch (InterruptedException e) {
// Ignore
}
}
String[] pendingMessages = null;
synchronized (messages) {
pendingMessages = messages
messages
}
try {
if (connection == null){
try{
synchronized(this){
wait();
}
} catch (InterruptedException e){
// Ignore
}
}
PrintWriter writer = connection
for (int j =
final String forecast = pendingMessages[j] +
writer
log(
}
writer
writer
connection = null;
log(
} catch (IOException e) {
log(
}
}
}
}
這個類基本上是樣板代碼
回頭看清單
清單
private class Weatherman implements Runnable{
private final List zipCodes;
private final String YAHOO_WEATHER =
public Weatherman(Integer
zipCodes = new ArrayList(zips
for (Integer zip : zips) {
try {
zipCodes
} catch (Exception e) {
// dont add it if it sucks
}
}
}
public void run() {
int i =
while (i >=
int j = i % zipCodes
SyndFeedInput input = new SyndFeedInput();
try {
SyndFeed feed = input
SyndEntry entry = (SyndEntry) feed
messageSender
Thread
} catch (Exception e) {
// just eat it
}
i++;
}
}
private String entryToHtml(SyndEntry entry){
StringBuilder html = new StringBuilder(
html
html
html
return html
}
}
這個類使用 Project Rome 庫解析來自 Yahoo Weather 的 RSS feed
清單
<%@page contentType=
<!DOCTYPE HTML PUBLIC
<html>
<head>
<meta http
<title>Comet Weather</title>
<SCRIPT TYPE=
function go(){
var url = //localhost:
var request = new XMLHttpRequest();
request
request
request
if (request
if (request
if (request
document
request
}
}
go();
}
};
request
}
</SCRIPT>
</head>
<body>
<h
<input type=
<div id=
</body>
</html>
該代碼只是在用戶單擊 Go 按鈕時開始長輪詢
現在
Jetty 和 Comet
Jetty 服務器使用稍微不同的技術來支持 Comet 的可伸縮的實現
可以使用 Jetty 的 org
清單
public class JettyWeatherServlet extends HttpServlet {
private MessageSender messageSender = null;
private static final Integer TIMEOUT =
public void begin(HttpServletRequest request
throws IOException
request
request
messageSender
Weatherman weatherman = new Weatherman(
new Thread(weatherman)
}
public void end(HttpServletRequest request
throws IOException
synchronized (request) {
request
Continuation continuation = ContinuationSupport
(request
if (continuation
continuation
}
}
}
public void error(HttpServletRequest request
throws IOException
end(request
}
public boolean read(HttpServletRequest request
throws IOException
throw new UnsupportedOperationException();
}
@Override
protected void service(HttpServletRequest request
throws IOException
synchronized (request) {
Continuation continuation = ContinuationSupport
(request
if (!continuation
begin(request
}
Integer timeout = (Integer) request
(
boolean resumed = continuation
timeout
if (!resumed) {
error(request
}
}
}
public void setTimeout(HttpServletRequest request
int timeout) throws IOException
UnsupportedOperationException {
request
}
}
這裡最需要注意的是
希望這足以鼓舞人心
Servlet
在此
清單
@WebServlet(asyncSupported=true
public class WeatherServlet extends HttpServlet {
private MessageSender messageSender;
// init and destroy are the same as other
@Override
protected void doGet(HttpServletRequest request
throws ServletException
AsyncContext async = request
messageSender
Weatherman weatherman = new Weatherman(
async
}
}
值得高興的是
回頭看看清單
而且
結束語
Comet 風格的 Web 應用程序可以為 Web 帶來全新的交互性
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26942.html