Skip to content

Add coordinated SMTP connection failover #755

Closed

Description

@bbottema

Follow-up: preserve the provider's explicit port in failed-route history

A supplied Session may contain a typed direct port above an inherited String default:

Properties defaults = new Properties();
defaults.setProperty("mail.smtp.port", "2525");
Properties settings = new Properties(defaults);
settings.setProperty("mail.smtp.host", "localhost");
settings.put("mail.smtp.port", 2526);

Angus attempts port 2526, but failed and cached-skipped connection history can report 2525. Interpret explicit endpoint settings consistently with the selected provider. Unknown or unspecified endpoints remain absent rather than acquiring guessed defaults; successful transport facts, caller Session ownership and bounded private diagnostics keep their meanings.

This completes the unreleased failover history contract under ADR 0026 and ADR 0029.


Mailer mailer = mail.mailerBuilder()
    .withSMTPServerHost("primary.example.org")
    .withFailoverMailer(fallback, FailoverPolicy.builder().build())
    .withMailSendRetryPolicy(MailSendRetryPolicy.builder()
        .withMaximumAttempts(3).withRetryDelay(Duration.ofSeconds(5)).build())
    .buildMailer();
mailer.sync().sendMail(email);
// Guidance broadly said no automatic replay after provider invocation,
// contradicting the separately implemented safe submission-retry policy.
FailoverPolicy changed = FailoverPolicy.builder()
    .withPrimaryConnectionAttempts(5)
    .withPrimaryConnectionRetryDelay(original.getPrimaryConnectionRetryDelay())
    .withPrimaryRecheckInterval(original.getPrimaryRecheckInterval())
    .withPrimaryRecheckBackoff(original.getPrimaryRecheckBackoffMultiplier(),
        original.getPrimaryMaximumRecheckInterval())
    .withFailoverOnAuthenticationFailure(original.isFailoverOnAuthenticationFailure())
    .withFailoverOnCertificateVerificationFailure(original.isFailoverOnCertificateVerificationFailure())
    .build();

ADR/public guidance retained the original connection-only wording after bounded safe submission retries were implemented, implying those retries did not exist or were prohibited.

Changing one setting on immutable FailoverPolicy required reconstructing all unrelated policy values manually, risking lost retry/recheck budgets and opt-ins.

Expected contract: Clarify failover's pre-submission route-selection boundary and distinguish separately configured same-destination safe retries; align README, public Javadocs, architecture and website without changing runtime behavior.

Add independent toBuilder() retaining every stored failover setting, including resolved backoff caps and authentication/certificate opt-ins, with explicit resets preserving their meaning.

ADR 0003, ADR 0029, ADR 0030, ADR 0031.

Tracked retrospectively from the October 18–21 ADR alignment. Historical examples refer to the relevant pre-alignment revision of the unreleased 10.0.0 work.


Original request and earlier implementation context:

Implemented for 10.0.0

Coordinated connection-only failover is implemented and accepted on codex/10.0.0. Configure another Mailer and its policy together; ordinary synchronous and asynchronous sends use the route with or without connection pooling.

Mailer primary = mail.mailerBuilder()
    .withSMTPServer("primary.example.org", 587)
    .withFailoverMailer(failoverMailer, FailoverPolicy.builder()
        .withPrimaryRecheckInterval(Duration.ofSeconds(30))
        .build())
    .buildMailer();

primary.async().sendMail(email);

A reliably identified connection failure can move work immediately to an eligible configured failover Mailer. Successful rechecks restore preference for the primary. Ordinary pool-capacity waits, queue pressure and sending-limit waits keep their existing behavior; they are not outages.

Each selected Mailer applies its own ordinary Email defaults, overrides, validation, message protection and SMTP settings. Applicable mandatory message locks still travel with the operation. The related clustered-template correction is tracked in #757.

Authentication and certificate/identity failures stop by default and have independent opt-ins. No option weakens the selected destination's TLS checks. Once provider or CustomMailer invocation begins, there is no automatic replay, including partial acceptance, uncertain acceptance or acceptance followed by cleanup failure.

The initiating Mailer retains execution, cancellation, deadline, completion and observer ownership. Lazy batches may return to the recovered primary between untouched emails; open-connection callbacks stay pinned after acquisition. Named properties, configuration locks, Spring and CLI support, and bounded connection history are included.

Completion

  • Paired Mailer/policy API, nested routes and independent destination configuration.
  • Selected-only templates, applicable locks and exclusive live Session ownership.
  • Failure classification, recovery/recheck coordination and no post-invocation replay.
  • Ordinary sends, retained batches/callbacks and resource/lifecycle ownership.
  • Named configuration, provenance, Spring and generated CLI integration.
  • Connection history, public contracts, user guidance and migration notes.
  • Implementation and follow-up corrections reviewed and accepted.
  • Supporting pool integrations published and adopted.

Supporting libraries: Generic Object Pool 2.6.0, Clustered Object Pool 4.3.0, and SMTP Connection Pool 4.4.0.

Design and follow-up

Completion here means the feature is ready on the 10.0.0 branch, not that 10.0.0 has been released.

Activity

  1. added this to the 10.0.0 milestone on Oct 7, 2026
  2. bbottema commented on Oct 7, 2026

    @bbottema
    OwnerAuthor

    Scope decision and retry follow-up

    The maintainer selected connection failover first for 10.0.0: try a configured eligible backup when the primary cannot provide a usable connection. Once provider submission invocation begins, retain the existing failure and receipt behavior without automatic replay. This applies to ordinary sync/async sends, with or without pooling.

    The implementation's website walkthrough must include a concrete application-owned retry recipe using the existing mail-send observer and receipt guidance, with sample output. The observer hands eligible work to a separate job queue/scheduler; it must not sleep or recursively resend. Explain job/Email correlation, bounded attempts/backoff, safe unaccepted-recipient selection, and why failed completion or a missing receipt is not proof that nothing was accepted. Exclude blind retry after uncertain acceptance or acceptance followed by cleanup failure.

    Built-in retry policy and possible backoff are now separate research in #756, not an expansion of this connection-only scope. No milestone has been assigned to that follow-up.

    Pool saturation versus outage handling, authentication/TLS failure handling, route/API representation, attempt history and cooldown behavior still need maintainer decisions. ADR 0029 remains proposed; no implementation is approved by this comment. The project's actual status is In Progress for ADR research and decisions, not runtime implementation.

  3. changed the title [-]Research SMTP submission failover and settle its ADR[/-] [+]Add coordinated SMTP connection failover[/+] on Oct 8, 2026
  4. bbottema commented on Oct 8, 2026

    @bbottema
    OwnerAuthor

    The accepted ADR and implementation plan are now implemented locally and ready for production-code review, including named configuration, locks, nested routes, recovery, connection history and the website walkthrough.

    Mailer primary = mail.mailerBuilder()
        .withSMTPServer("primary.example.org", 587)
        .withFailoverMailer(failoverMailer, FailoverPolicy.builder().build())
        .buildMailer();
    
    primary.async().sendMail(email);

    A connection failure can move the operation to an eligible configured destination. Ordinary pool/rate waiting does not trigger failover, and an email is never automatically replayed once provider submission begins. Each selected Mailer applies its own ordinary templates; mandatory message locks remain binding. The clustered-template correction is tracked separately in #757.

    The required upstream changes are implemented and tested locally but not released: Generic Object Pool #26, Clustered Object Pool #31, and SMTP Connection Pool #37. Publication requires separate approval.

    All implementation changes remain uncommitted. This issue stays In Progress for review; no release, push or closure has occurred. Built-in submission retry remains separate research in #756.

  5. bbottema commented on Oct 12, 2026

    @bbottema
    OwnerAuthor

    The supporting-library cascade is now released: Generic Object Pool 2.6.0 → Clustered Object Pool 4.3.0 → SMTP Connection Pool 4.4.0. Their linked issues and release milestones are complete. SJM can now use the published candidate-selection, claim-identity and connection-observation hooks. The SJM implementation remains in review; this does not complete or close #755.

  6. 52 remaining items

  7. bbottema commented on Nov 1, 2026

    @bbottema
    OwnerAuthor

    Development addenda for unreleased 10.0.0

    These follow-ups complete this feature's 10.0.0 implementation. Dates identify the original development records; before examples describe earlier development revisions.

    2026-11-01: Allow failover when SMTP authentication is interrupted by an outage

    An SMTP server can disconnect, time out or return 421 while authentication is in progress. Angus wraps these setup outages in AuthenticationFailedException, and Simple Java Mail then treats them as rejected credentials. The default failover policy stops even though the server never rejected the account.

    Mailer primary = mail.mailerBuilder()
        .withSMTPServer("primary.example", 587, "account", "secret")
        .withFailoverMailer(secondary, FailoverPolicy.builder().build())
        .buildMailer();
    primary.sync().sendMail(email);

    If the primary becomes unavailable during AUTH, use the retained, reliable setup-outage evidence to permit the next eligible destination. Keep genuine credential rejection behind its existing authentication-failure opt-in, and preserve certificate/identity permissions, cleanup requirements, cancellation, deadlines and the boundary before message submission. Do not classify arbitrary nested socket failures as permission to bypass a known security rejection.

    The correction uses observed setup-outage evidence from Simple Java Mail's managed Angus connections, for both direct and pooled sending. A caller-supplied Session keeps its configured provider and factories. If that Session uses ordinary Angus without this managed evidence, its authentication wrappers retain the existing conservative interpretation and authentication-failure opt-in.

    This correction targets the 10.0.0 development branch and follows ADR 0029 and ADR 0017.

    Completed behavior — 2026-11-01

    Mailer primary = mail.mailerBuilder()
        .withSMTPServer("primary.example", 587, "account", "secret")
        .withFailoverMailer(secondary, FailoverPolicy.builder().build())
        .buildMailer();
    primary.sync().sendMail(email);

    Managed Angus connections now retain the observed setup-outage reason when an authentication exchange is interrupted by a 421, EOF/reset or read timeout. Those outages can use the normal failover policy before message submission; genuine credential rejection, certificate permissions, operation stops and incomplete cleanup keep their existing rules. The original provider exception chain remains available.

    A caller-supplied Session keeps its provider and factories. Ordinary supplied Angus Sessions without managed outage evidence retain the conservative authentication-failure interpretation and its existing opt-in.

    Implemented on the 10.0.0 development branch, not a published release, in 467e4cd0a, following ADR 0029 and ADR 0017.

    Related guidance is in 8febbd095 and the website documentation change.

  8. bbottema commented on Nov 9, 2026

    @bbottema
    OwnerAuthor

    A follow-up from the feature review is now tracked in Angus issue #235. When a custom HostnameVerifier returns false, Angus exposes a generic SSLException, so SJM cannot reliably identify the hostname rejection and stops even when withFailoverOnCertificateVerificationFailure(true) is enabled.

    Angus PR #236 proposes reporting this result as SSLPeerUnverifiedException, which lets SJM apply the existing failover permission while the backup continues to enforce its own TLS policy. SJM adoption remains pending upstream acceptance and a compatible Angus release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions