示例#1
0
  @Override
  public void run() {
    logger.debug("Created NamedThread {}", getName());
    try {
      numAlive.incrementAndGet();
      super.run();
    } catch (Throwable t) {
      // Log the problem but do so within a try/catch in case it is
      // an OutOfMemoryError and need to exit even if get another
      // OutOfMemoryError when logging.
      try {
        // Output info to stderr since this is an exception situation.
        // This will log it to the nohup file used for running core app.
        System.err.println("Throwable \"" + t.getMessage() + "\" occurred at " + new Date());
        t.printStackTrace();

        // Log since this is a serious problem
        if (t instanceof OutOfMemoryError) {
          // Would like to send out an e-mail as part of logging but
          // found that when running out of memory that sending out an
          // e-mail can hang the system for a while. This is a bad
          // thing since OutOfMemoryError is really serious and want
          // to terminate the program right away so that it can be
          // automatically restarted. This is unfortunate since
          // really want to notify folks that there is an out of
          // memory problem but notifying via email is not as
          // important as quickly getting the system restarted.
          logger.error(
              "OutOfMemoryError occurred in thread {} so "
                  + "terminating application for agencyId={}",
              getName(),
              AgencyConfig.getAgencyId(),
              t);
        } else {
          // Log and send out e-mail since this is an unexpected problem
          logger.error(
              Markers.email(),
              "Unexpected Throwable occurred which will cause "
                  + "thread {} to terminate for agencyId={}",
              getName(),
              AgencyConfig.getAgencyId(),
              t);
        }
      } catch (Throwable t2) {
      }

      // OutOfMemoryErrors are really serious. Don't want application to
      // continue in some kind of crippled mode that monitoring has a
      // difficult time detecting. Therefore exit the application so that
      // can be automatically restarted.
      if (t instanceof OutOfMemoryError) {
        System.exit(-1);
      }
    } finally {
      numAlive.decrementAndGet();
      logger.debug("Exiting NamedThread {}", getName());
    }
  }
  /**
   * Initiates a real-time amigocloud feed. If there is a JSONException while starting connection
   * then will try again every 10 seconds until successful.
   */
  public void startRealtimeWebsockets() {
    logger.info("Starting amigocloud AVL feed");

    int numberOfExceptions = 0;
    boolean exceptionOccurred = false;
    do {
      try {
        // Actually make the connection
        AmigoWebsockets socket =
            new AmigoWebsockets(
                userId.getValue(),
                projectId.getValue(),
                datasetId.getValue(),
                feedUrl.toString(),
                new MyAmigoWebsocketListener(this));
        socket.connect();
        exceptionOccurred = false;
      } catch (JSONException e) {
        ++numberOfExceptions;
        exceptionOccurred = true;

        // If exception has occurred several times then send e-mail to
        // indicate there is an ongoing problem
        if (numberOfExceptions == 3) {
          logger.error(
              Markers.email(),
              "For agencyId={} exception when starting up "
                  + "AmigoCloudAvlModule. {}. numberOfExceptions={}",
              AgencyConfig.getAgencyId(),
              e.getMessage(),
              numberOfExceptions,
              e);
        }

        // Sleep 10 seconds before trying again
        Time.sleep(10 * Time.MS_PER_SEC);
      }
    } while (exceptionOccurred);
  }