Пример #1
0
  /**
   * Sends message based on properties and fields configured for the service.
   *
   * @param mailParms
   * @return a PollStatus
   */
  private PollStatus sendTestMessage(final MailTransportParameters mailParms) {
    PollStatus status = PollStatus.unavailable("Test not completed.");

    final long interval = mailParms.getSendTestAttemptInterval();

    final TimeoutTracker tracker =
        new TimeoutTracker(
            mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout());
    for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) {
      tracker.startAttempt();
      LOG.debug(
          "sendTestMessage: sending mail attempt: {}, elapsed time: {}ms",
          (tracker.getAttempt() + 1),
          String.format("%.2f", tracker.elapsedTimeInMillis()));
      try {
        final JavaMailer sendMailer = createMailer(mailParms);
        overRideDefaultProperties(mailParms, sendMailer);
        sendMailer.mailSend();
        status = PollStatus.available();
        break;
      } catch (final JavaMailerException e) {
        status = PollStatus.unavailable(e.getLocalizedMessage());
      }

      if (tracker.shouldRetry()) {
        delayTest(status, interval);
      }
    }
    return status;
  }
Пример #2
0
  private PollStatus readTestMessage(final MailTransportParameters mailParms) {
    LOG.debug("readTestMessage: Beginning read mail test.");
    PollStatus status = PollStatus.unavailable("Test not completed.");

    final long interval = mailParms.getReadTestAttemptInterval();

    if (mailParms.isEnd2EndTestInProgress()) {
      LOG.debug(
          "Initially delaying read test: {} because end to end test is in progress.",
          mailParms.getReadTestAttemptInterval());

      if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) {
        return status;
      }
    }

    Store mailStore = null;
    Folder mailFolder = null;
    try {
      final JavaMailer readMailer = new JavaMailer(mailParms.getJavamailProperties());
      setReadMailProperties(mailParms, readMailer);

      final TimeoutTracker tracker =
          new TimeoutTracker(
              mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout());
      for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) {
        tracker.startAttempt();

        if (tracker.getAttempt() > 0) {
          if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) {
            LOG.warn("readTestMessage: Status set to: {} during delay, exiting test.", status);
            break;
          }
        }
        LOG.debug(
            "readTestMessage: reading mail attempt: {}, elapsed time: {}ms.",
            (tracker.getAttempt() + 1),
            String.format("%.2f", tracker.elapsedTimeInMillis()));
        try {
          mailStore = readMailer.getSession().getStore();
          mailFolder = retrieveMailFolder(mailParms, mailStore);
          mailFolder.open(Folder.READ_WRITE);
        } catch (final MessagingException e) {
          if (tracker.shouldRetry()) {
            LOG.warn("readTestMessage: error reading INBOX", e);
            closeStore(mailStore, mailFolder);
            continue; // try again to get mail Folder from Store
          } else {
            LOG.warn("readTestMessage: error reading INBOX", e);
            return PollStatus.down(e.getLocalizedMessage());
          }
        }
        if (mailFolder.isOpen()
            && (mailParms.getReadTest().getSubjectMatch() != null
                || mailParms.isEnd2EndTestInProgress())) {
          status = processMailSubject(mailParms, mailFolder);
          if (status.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
            break;
          }
        }
      }

    } catch (final JavaMailerException e) {
      status = PollStatus.down(e.getLocalizedMessage());
    } finally {
      closeStore(mailStore, mailFolder);
    }
    return status;
  }