Esempio n. 1
0
 @Override
 public void run() {
   int timeToSleep = NO_WAIT;
   boolean connected = false;
   while (!closed) {
     try {
       if (!closed && null == stream) {
         // try establishing connection
         logger.info("Establishing connection.");
         setStatus("[Establishing connection]");
         stream = (StatusStreamBase) getStream();
         connected = true;
         logger.info("Connection established.");
         for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
           try {
             listener.onConnect();
           } catch (Exception e) {
             logger.warn(e.getMessage());
           }
         }
         // connection established successfully
         timeToSleep = NO_WAIT;
         logger.info("Receiving status stream.");
         setStatus("[Receiving stream]");
         while (!closed) {
           try {
             stream.next(this.streamListeners, this.rawStreamListeners);
           } catch (IllegalStateException ise) {
             logger.warn(ise.getMessage());
             break;
           } catch (TwitterException e) {
             logger.info(e.getMessage());
             stream.onException(e, this.streamListeners, this.rawStreamListeners);
             throw e;
           } catch (Exception e) {
             if (!(e instanceof NullPointerException)
                 && !e.getMessage().equals("Inflater has been closed")) {
               logger.info(e.getMessage());
               stream.onException(e, this.streamListeners, this.rawStreamListeners);
               closed = true;
               break;
             }
           }
         }
       }
     } catch (TwitterException te) {
       logger.info(te.getMessage());
       if (!closed) {
         if (NO_WAIT == timeToSleep) {
           if (te.getStatusCode() == FORBIDDEN) {
             logger.warn("This account is not in required role. ", te.getMessage());
             closed = true;
             for (StreamListener statusListener : this.streamListeners) {
               statusListener.onException(te);
             }
             break;
           }
           if (te.getStatusCode() == NOT_ACCEPTABLE) {
             logger.warn("Parameter not accepted with the role. ", te.getMessage());
             closed = true;
             for (StreamListener statusListener : streamListeners) {
               statusListener.onException(te);
             }
             break;
           }
           connected = false;
           for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
             try {
               listener.onDisconnect();
             } catch (Exception e) {
               logger.warn(e.getMessage());
             }
           }
           if (te.getStatusCode() > 200) {
             timeToSleep = HTTP_ERROR_INITIAL_WAIT;
           } else if (0 == timeToSleep) {
             timeToSleep = TCP_ERROR_INITIAL_WAIT;
           }
         }
         if (te.getStatusCode() > 200 && timeToSleep < HTTP_ERROR_INITIAL_WAIT) {
           timeToSleep = HTTP_ERROR_INITIAL_WAIT;
         }
         if (connected) {
           for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
             try {
               listener.onDisconnect();
             } catch (Exception e) {
               logger.warn(e.getMessage());
             }
           }
         }
         for (StreamListener statusListener : streamListeners) {
           statusListener.onException(te);
         }
         // there was a problem establishing the connection, or the connection closed by peer
         if (!closed) {
           // wait for a moment not to overload Twitter API
           logger.info("Waiting for " + (timeToSleep) + " milliseconds");
           setStatus("[Waiting for " + (timeToSleep) + " milliseconds]");
           try {
             Thread.sleep(timeToSleep);
           } catch (InterruptedException ignore) {
           }
           timeToSleep =
               Math.min(
                   timeToSleep * 2,
                   (te.getStatusCode() > 200) ? HTTP_ERROR_WAIT_CAP : TCP_ERROR_WAIT_CAP);
         }
         stream = null;
         logger.debug(te.getMessage());
         connected = false;
       }
     }
   }
   if (this.stream != null && connected) {
     try {
       this.stream.close();
     } catch (IOException ignore) {
     } catch (Exception e) {
       e.printStackTrace();
       logger.warn(e.getMessage());
     } finally {
       for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
         try {
           listener.onDisconnect();
         } catch (Exception e) {
           logger.warn(e.getMessage());
         }
       }
     }
   }
   for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
     try {
       listener.onCleanUp();
     } catch (Exception e) {
       logger.warn(e.getMessage());
     }
   }
 }
Esempio n. 2
0
    @Override
    public void run() {
      int timeToSleep = NO_WAIT;
      boolean connected = false;
      while (!closed) {
        try {
          if (!closed && null == stream) {
            // try establishing connection
            logger.info("Establishing connection.");
            setStatus("[Establishing connection]");
            stream = (StatusStreamBase) getStream();
            connected = true;
            logger.info("Connection established.");
            for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
              try {
                listener.onConnect();
              } catch (Exception e) {
                logger.warn(e.getMessage());
              }
            }
            // connection established successfully
            timeToSleep = NO_WAIT;
            logger.info("Receiving status stream.");
            setStatus("[Receiving stream]");
            while (!closed) {
              try {
                /*
                 * new built
                 */
                stream.next(this.streamListeners, this.rawStreamListeners);

              } catch (IllegalStateException ise) {
                logger.warn(ise.getMessage());
                break;
              } catch (TwitterException e) {
                logger.info(e.getMessage());
                stream.onException(e, this.streamListeners, this.rawStreamListeners);
                throw e;
              } catch (Exception e) {
                logger.info(e.getMessage());
                stream.onException(e, this.streamListeners, this.rawStreamListeners);
                closed = true;
                break;
              }
            }
          }
        } catch (TwitterException te) {
          logger.info(te.getMessage());
          if (!closed) {
            if (NO_WAIT == timeToSleep) {
              if (te.getStatusCode() == FORBIDDEN) {
                logger.warn("This account is not in required role. ", te.getMessage());
                closed = true;
                for (StreamListener statusListener : streamListeners) {
                  statusListener.onException(te);
                }
                break;
              }
              if (te.getStatusCode() == NOT_ACCEPTABLE) {
                logger.warn("Parameter not accepted with the role. ", te.getMessage());
                closed = true;
                for (StreamListener statusListener : streamListeners) {
                  statusListener.onException(te);
                }
                break;
              }
              connected = false;
              for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
                try {
                  listener.onDisconnect();
                } catch (Exception e) {
                  logger.warn(e.getMessage());
                }
              }
              if (te.getStatusCode() > 200) {
                timeToSleep = HTTP_ERROR_INITIAL_WAIT;
              } else if (0 == timeToSleep) {
                timeToSleep = TCP_ERROR_INITIAL_WAIT;
              }
            }
            if (te.getStatusCode() > 200 && timeToSleep < HTTP_ERROR_INITIAL_WAIT) {
              timeToSleep = HTTP_ERROR_INITIAL_WAIT;
            }
            if (connected) {
              for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
                try {
                  listener.onDisconnect();
                } catch (Exception e) {
                  logger.warn(e.getMessage());
                }
              }
            }
            for (StreamListener statusListener : streamListeners) {
              statusListener.onException(te);
            }
            // there was a problem establishing the connection, or the connection closed by peer
            if (!closed) {
              // wait for a moment not to overload Twitter API
              logger.info("Waiting for " + (timeToSleep) + " milliseconds");
              setStatus("[Waiting for " + (timeToSleep) + " milliseconds]");
              try {
                Thread.sleep(timeToSleep);
              } catch (InterruptedException ignore) {
              }
              timeToSleep =
                  Math.min(
                      timeToSleep * 2,
                      (te.getStatusCode() > 200) ? HTTP_ERROR_WAIT_CAP : TCP_ERROR_WAIT_CAP);
            }
            stream = null;
            logger.debug(te.getMessage());
            connected = false;
          }
        }
      } // end  for while
      if (this.stream != null && connected) {
        try {
          /*
           * new built
           */
          String line =
              "{\"created_at\":\"Fri\",\"id\":123,\"id_str\":\"123\",\"text\":\"YOU are WORNG!.\",\"source\":\"123\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"user\":{\"id\":319351645,\"id_str\":\"319351645\",\"name\":\"Briana who? \",\"screen_name\":\"Yo_daddyknows\",\"location\":\"\",\"url\":null,\"description\":null,\"protected\":false,\"followers_count\":811,\"friends_count\":735,\"listed_count\":0,\"created_at\":\"Fri Jun 17 23:58:49 +0000 2011\",\"favourites_count\":10,\"utc_offset\":-18000,\"time_zone\":\"Central Time (US & Canada)\",\"geo_enabled\":false,\"verified\":false,\"statuses_count\":34697,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"642D8B\",\"profile_background_image_url\":\"g.gif\",\"profile_background_image_url_https\":\"g.gif\",\"profile_background_tile\":true,\"profile_image_url\":\"http:l.jpeg\",\"profile_image_url_https\":\"l.jpeg\",\"profile_banner_url\":\"923\",\"profile_link_color\":\"FF0000\",\"profile_sidebar_border_color\":\"65B0DA\",\"profile_sidebar_fill_color\":\"7AC3EE\",\"profile_text_color\":\"3D1957\",\"profile_use_background_image\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweet_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"favorited\":false,\"retweeted\":false,\"filter_level\":\"medium\",\"lang\":\"en\"}";
          JSONObject json = new JSONObject(line);
          // System.out.println(line);
          JSONObjectType.Type event = JSONObjectType.determine(json);
          this.stream.onStatus(json, this.streamListeners);

          this.stream.close();
        } catch (IOException ignore) {
        } catch (Exception e) {
          e.printStackTrace();
          logger.warn(e.getMessage());
        } finally {
          for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
            try {
              listener.onDisconnect();
            } catch (Exception e) {
              logger.warn(e.getMessage());
            }
          }
        }
      }

      for (ConnectionLifeCycleListener listener : lifeCycleListeners) {
        try {
          listener.onCleanUp();
        } catch (Exception e) {
          logger.warn(e.getMessage());
        }
      }
    }