コード例 #1
0
ファイル: mailcomet.java プロジェクト: storm12358/kyou
  public void event(CometEvent e) throws IOException, ServletException {
    HttpServletRequest request = e.getHttpServletRequest();
    HttpServletResponse response = e.getHttpServletResponse();
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html,charset=utf-8");
    int id = StringUtil.strToInt(request.getSession().getAttribute("userid").toString());

    if (e.getEventType() == EventType.BEGIN) {
      log("Begin for session: " + request.getSession(true).getId());
      request.setAttribute("org.apache.tomcat.comet.timeout", TIMEOUT);
      PrintWriter out = response.getWriter();
      int t = mailcometDAO.qryUnreadMail(id);
      out.write(String.valueOf(t));
      out.flush();
    } else if (e.getEventType() == CometEvent.EventType.ERROR) {
      log("Error for session: " + request.getSession(true).getId());
      e.close();
    } else if (e.getEventType() == CometEvent.EventType.END) {
      log("End for session: " + request.getSession(true).getId());
      e.close();
    } else if (e.getEventType() == CometEvent.EventType.READ) {
      throw new UnsupportedOperationException("This servlet does not accept data");
    }
  }
コード例 #2
0
ファイル: TomcatCometServlet.java プロジェクト: sbryzak/errai
  /**
   * When an event is received, it is processed accordingly. A post event will tell the servlet to
   * wait for the messages, if there are messages waiting, they will be transmitted. Any errors will
   * be handled.
   *
   * @param event - the Http event that occured
   * @throws IOException - thrown if there is a read/write error
   * @throws ServletException - thrown if a servlet error occurs
   */
  public void event(final CometEvent event) throws IOException, ServletException {
    final HttpServletRequest request = event.getHttpServletRequest();
    final QueueSession session =
        sessionProvider.getSession(
            request.getSession(), request.getHeader(ClientMessageBus.REMOTE_QUEUE_ID_HEADER));

    MessageQueue queue;
    switch (event.getEventType()) {
      case BEGIN:
        boolean post = "POST".equals(request.getMethod());
        if ((queue = getQueue(session, !post)) != null) {
          synchronized (activeEvents) {
            Set<CometEvent> events = activeEvents.get(session);

            if (post) {
              // do not pause incoming messages.
              break;
            } else if (queue.messagesWaiting()) {
              transmitMessages(event.getHttpServletResponse(), queue);
              event.close();
              break;
            }

            if (!queueToSession.containsKey(queue)) {
              queueToSession.put(queue, session);
            }

            if (events == null) {
              activeEvents.put(session, events = new HashSet<CometEvent>());
            }
            if (events.contains(event)) {
              break;
            } else {
              event.setTimeout(30000);
              events.add(event);
            }
          }
        } else {
          switch (getConnectionPhase(request)) {
            case CONNECTING:
            case DISCONNECTING:
              return;
          }

          sendDisconnectWithReason(
              event.getHttpServletResponse().getOutputStream(),
              "There is no queue associated with this session.");
        }
        break;

      case END:
        event.close();
        synchronized (activeEvents) {
          Set<CometEvent> evt = activeEvents.get(session);
          if (evt != null && !evt.remove(event)) {
            return;
          }
        }

        if ((queue = getQueue(session, false)) != null) {
          queue.heartBeat();
        } else {
          return;
        }

        break;

      case ERROR:
        log.error("An Error Occured: " + (event != null ? event.getEventSubType() : "<null>"));

        queue = getQueue(session, false);
        if (queue == null) {
          return;
        }

        synchronized (activeEvents) {
          Set<CometEvent> evt = activeEvents.get(session);
          if (evt != null && !evt.remove(event)) {
            return;
          }
        }

        if (event.getEventSubType() == CometEvent.EventSubType.TIMEOUT) {
          if (queue != null) queue.heartBeat();
        } else {
          if (queue != null) {
            queueToSession.remove(queue);
            service.getBus().closeQueue(session.getSessionId());
            activeEvents.remove(session);
          }
        }

        event.close();
        break;

      case READ:
        readInRequest(session, request, event.getHttpServletResponse());
        event.close();
    }
  }