, Servlet 3.0
30 ( ).
@WebServlet(async ="true")
public class AsyncServlet extends HttpServlet {
Timer timer = new Timer("ClientNotifier");
public void doGet(HttpServletRequest req, HttpServletResponse res) {
AsyncContext aCtx = request.startAsync(req, res);
timer.schedule(new TimerTask(aCtx) {
public void run() {
try{
int unreadAlertCount = alertManager.getUnreadAlerts(username);
response.write(unreadAlertCount);
}
catch(Exception e){
aCtx.complete();
}
}
}, 30000);
}
}
. alertManager, AlertNotificationHandler, .
@WebServlet(async="true")
public class AsyncServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
final AsyncContext asyncCtx = request.startAsync(req, res);
alertManager.register(new AlertNotificationHandler() {
public void onNewAlert() {
try {
int unreadAlertCount =
alertManager.getUnreadAlerts();
ServletResponse response = asyncCtx.getResponse();
writeResponse(response, unreadAlertCount);
} catch (Exception ex) {
asyncCtx.complete();
}
}
});
}
}