Tomcat 6支持的Comet

阅读更多

今天在测试中无意中看到Tomcat 6的新特性Comet,使用Comet技术可以轻松实现一个Pushlet的功能,让我看了着实兴奋不已,而其官方网站还给出了一个Messenger的例子。

   
   
   
   
  1. public class ChatServlet
  2. extends HttpServlet implements CometProcessor {
  3.  
  4. protected ArrayList connections =
  5. new ArrayList ( );
  6. protected MessageSender messageSender = null;
  7. public void init ( ) throws ServletException {
  8. messageSender = new MessageSender ( );
  9. Thread messageSenderThread =
  10. new Thread (messageSender, "MessageSender[" + getServletContext ( ). getContextPath ( ) + "]" );
  11. messageSenderThread. setDaemon ( true );
  12. messageSenderThread. start ( );
  13. }
  14.  
  15. public void destroy ( ) {
  16. connections. clear ( );
  17. messageSender. stop ( );
  18. messageSender = null;
  19. }
  20.  
  21. /**
  22. * Process the given Comet event.
  23. *
  24. * @param event The Comet event that will be processed
  25. * @throws IOException
  26. * @throws ServletException
  27. */
  28. public void event (CometEvent event )
  29. throws IOException, ServletException {
  30. HttpServletRequest request = event. getHttpServletRequest ( );
  31. HttpServletResponse response = event. getHttpServletResponse ( );
  32. if (event. getEventType ( ) == CometEvent. EventType. BEGIN ) {
  33. log ( "Begin for session: " + request. getSession ( true ). getId ( ) );
  34. PrintWriter writer = response. getWriter ( );
  35. writer. println ( "\"-//w3c//dtd html 4.0 transitional//en\">" );
  36. writer. println ( "JSP Chat\"#FFFFFF\">" );
  37. writer. flush ( );
  38. synchronized (connections ) {
  39. connections. add (response );
  40. }
  41. } else if (event. getEventType ( ) == CometEvent. EventType. ERROR ) {
  42. log ( "Error for session: " + request. getSession ( true ). getId ( ) );
  43. synchronized (connections ) {
  44. connections. remove (response );
  45. }
  46. event. close ( );
  47. } else if (event. getEventType ( ) == CometEvent. EventType. END ) {
  48. log ( "End for session: " + request. getSession ( true ). getId ( ) );
  49. synchronized (connections ) {
  50. connections. remove (response );
  51. }
  52. PrintWriter writer = response. getWriter ( );
  53. writer. println ( "" );
  54. event. close ( );
  55. } else if (event. getEventType ( ) == CometEvent. EventType. READ ) {
  56. InputStream is = request. getInputStream ( );
  57. byte [ ] buf = new byte [ 512 ];
  58. do {
  59. int n = is. read (buf ); //can throw an IOException
  60. if (n > 0 ) {
  61. log ( "Read " + n + " bytes: " + new String (buf, 0, n )
  62. + " for session: " + request. getSession ( true ). getId ( ) );
  63. } else if (n < 0 ) {
  64. error (event, request, response );
  65. return;
  66. }
  67. } while (is. available ( ) > 0 );
  68. }
  69. }
  70.  
  71. public class MessageSender implements Runnable {
  72.  
  73. protected boolean running = true;
  74. protected ArrayList messages = new ArrayList ( );
  75. public MessageSender ( ) {
  76. }
  77. public void stop ( ) {
  78. running = false;
  79. }
  80.  
  81. /**
  82. * Add message for sending.
  83. */
  84. public void send ( String user, String message ) {
  85. synchronized (messages ) {
  86. messages. add ( "[" + user + "]: " + message );
  87. messages. notify ( );
  88. }
  89. }
  90.  
  91. public void run ( ) {
  92.  
  93. while (running ) {
  94.  
  95. if (messages. size ( ) == 0 ) {
  96. try {
  97. synchronized (messages ) {
  98. messages. wait ( );
  99. }
  100. } catch ( InterruptedException e ) {
  101. // Ignore
  102. }
  103. }
  104.  
  105. synchronized (connections ) {
  106. String [ ] pendingMessages = null;
  107. synchronized (messages ) {
  108. pendingMessages = messages. toArray ( new String [ 0 ] );
  109. messages. clear ( );
  110. }
  111. // Send any pending message on all the open connections
  112. for ( int i = 0; i < connections. size ( ); i++ ) {
  113. try {
  114. PrintWriter writer = connections. get (i ). getWriter ( );
  115. for ( int j = 0; j < pendingMessages. length; j++ ) {
  116. writer. println (pendingMessages [j ] + "
    "
    );
  117. }
  118. writer. flush ( );
  119. } catch ( IOException e ) {
  120. log ( "IOExeption sending message", e );
  121. }
  122. }
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. }
  130.  
  131. }

你可能感兴趣的:(Comet,Tomcat,J#,JSP,thread)