示例#1
0
 private void ensureStatusStreamListenerIsSet() {
   for (StreamListener listener : streamListeners) {
     if (!(listener instanceof StatusListener)) {
       throw new IllegalStateException(
           "Only StatusListener is supported. found: " + listener.getClass());
     }
   }
 }
示例#2
0
 /** {@inheritDoc} */
 public void user(final String[] track) {
   ensureAuthorizationEnabled();
   ensureListenerIsSet();
   for (StreamListener listener : streamListeners) {
     if (!(listener instanceof UserStreamListener)) {
       throw new IllegalStateException(
           "Only UserStreamListener is supported. found: " + listener.getClass());
     }
   }
   startHandler(
       new TwitterStreamConsumer() {
         public UserStream getStream() throws TwitterException {
           return getUserStream(track);
         }
       });
 }
示例#3
0
 /**
  * Waits until the process is terminated or the timeout is elapsed.
  *
  * @return The exit code of the process
  */
 public int waitFor() {
   int exitCode = -1;
   if (process != null) {
     try {
       exitCode = process.waitFor();
       logger.debug("Closing errorStream...");
       errorStream.close();
       logger.debug("Closing outputStream...");
       outputStream.close();
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
     ProcessTimer.removeTimer(timer);
     logger.debug("TProcess ended: exitCode = " + exitCode);
   } else {
     ErrorsAndWarnings.addError("Cannot call waitFor() on a null TProcess");
   }
   return exitCode;
 }
示例#4
0
  /**
   * Makes the TProcess print out and error streams into one specified file. Several files can be
   * registered.
   *
   * @param logFile The file to print.
   * @throws FileNotFoundException
   */
  public void registerOutput(File logFile, String prompt) throws FileNotFoundException {

    // Links to standard and error outputs
    if (errorStream == null || outputStream == null) {
      errorStream = new StreamListener(process.getErrorStream());
      outputStream = new StreamListener(process.getInputStream());

      errorStream.start();
      outputStream.start();
    }

    // Links to output file
    if (logFile != null) {
      FileOutputStream fileOut = null;

      fileOut = new FileOutputStream(logFile);
      errorStream.registerOutputStream(prompt, fileOut);
      outputStream.registerOutputStream(prompt, fileOut);
    }
  }
示例#5
0
 /** Destroys the process */
 public void stop() {
   ProcessTimer.removeTimer(timer);
   try {
     Thread.sleep(StreamListener.PRINTING_PERIOD_IN_MILLIS);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   if (errorStream != null) {
     logger.debug("Closing errorStream...");
     errorStream.close();
   }
   if (outputStream != null) {
     logger.debug("Closing outputStream...");
     outputStream.close();
   }
   if (process != null) {
     logger.debug("destroying TProcess...");
     process.destroy();
     logger.debug("TProcess destroyed");
   }
 }
示例#6
0
 /** {@inheritDoc} */
 public void site(final boolean withFollowings, final long[] follow) {
   ensureOAuthEnabled();
   ensureListenerIsSet();
   for (StreamListener listener : streamListeners) {
     if (!(listener instanceof SiteStreamsListener)) {
       throw new IllegalStateException(
           "Only SiteStreamListener is supported. found: " + listener.getClass());
     }
   }
   startHandler(
       new TwitterStreamConsumer() {
         public StreamImplementation getStream() throws TwitterException {
           try {
             return new SiteStreamsImpl(
                 getDispatcher(), getSiteStream(withFollowings, follow), conf);
           } catch (IOException e) {
             throw new TwitterException(e);
           }
         }
       });
 }
 private void notifyStreamClosed(IStream stream) {
   for (Listener listener : listeners) {
     if (listener instanceof StreamListener) {
       try {
         ((StreamListener) listener).onStreamClosed(stream);
       } catch (Exception x) {
         LOG.info("Exception while notifying listener " + listener, x);
       } catch (Error x) {
         LOG.info("Exception while notifying listener " + listener, x);
         throw x;
       }
     }
   }
 }
 public String checkHostName(final boolean longHost, final String hostName0) {
   nodeName = "foo" + System.currentTimeMillis();
   final ProcessBuilder builder =
       new ProcessBuilder(
           Lists.newArrayList(
               otpHome + "/bin/erl",
               longHost ? "-name" : "-sname",
               nodeName,
               "-setcookie",
               "erlide"));
   String result = null;
   try {
     final Process process = builder.start();
     try {
       final StreamListener listener = new StreamListener(process.getInputStream());
       while (listener.isAlive()) {
         try {
           listener.join();
           final String hostName = hostName0 != null ? hostName0 : listener.getResult();
           ErlLogger.debug("Test %s hostname: %s", longHost ? "long" : "short", hostName);
           if (canConnect(hostName)) {
             result = hostName;
             ErlLogger.debug("OK");
           } else {
             ErlLogger.warn("Can't use %s as %s name", hostName, longHost ? "long" : "short");
           }
         } catch (final InterruptedException e) {
         }
       }
     } finally {
       process.destroy();
     }
   } catch (final IOException e) {
     ErlLogger.error(e);
   }
   return result;
 }
示例#9
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());
     }
   }
 }
示例#10
0
 public void setLogToConsole(boolean logToConsole) {
   errorStream.setLogToConsole(logToConsole);
   outputStream.setLogToConsole(logToConsole);
 }
示例#11
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());
        }
      }
    }