StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 9 · Scheduling and Async Work

Bulk Email Dispatch Queue

medium—No data yet

Problem statement

Implement the background dispatch machinery of a bulk email service: a bounded worker pool, retry with growing backoff, and a clean give-up after the final attempt.

Requirements

AsyncEmailConfig

Make AsyncEmailConfig supply the dispatch worker pool under the name emailExecutor, the pool the dispatch service is wired to use. The pool must behave as follows:

  • Deliveries run on exactly two worker threads, never more
  • Up to ten pending sends wait in the pool's queue while both workers are busy
  • Every worker thread has a name starting with "email-"
  • When the application shuts down, every send already handed to the pool is still carried out; sends waiting in the queue are drained, not dropped

EmailDispatchService

The provided controller passes each POST /emails request (a to address and a subject) to dispatch. The provided EmailClient performs the actual sends, records every delivery attempt it makes (recipient, attempt number, timestamp, thread name) in an in-memory log, and throws its delivery error when the recipient is unreachable.

  • dispatch hands the send to the worker pool and returns without waiting, so POST /emails replies 202 Accepted with status "QUEUED" and the recipient in to before the delivery completes
  • Every delivery attempt runs on a dispatch worker thread, never on the thread that called dispatch
  • Every accepted send gets its own delivery to its own recipient
  • A send that succeeds makes exactly one delivery attempt
  • A failed attempt is retried, once per configured delay in the property email.retry-delays-ms: the service waits at least the first delay before the second attempt and at least the longer second delay before the third attempt, so the backoff grows between attempts
  • In production the delays are 1000 and 2000 milliseconds, set in application.properties; the delays must always be read from the property, whatever values it holds
  • When the final attempt also fails, three attempts in total, the send is abandoned: the service logs a warning with the message "Giving up on " followed by the recipient address, exactly once, and makes no further attempts for that send
  • A send that recovers, on the second attempt or on the final one, is never reported as abandoned
  • An abandoned recipient is not blacklisted: a later dispatch to the same recipient starts a fresh delivery with its own attempts
  • Two sends can be in delivery at the same time, one per worker; a slow delivery that is busy retrying does not delay other recipients
  • A burst of sends that fits within the queue capacity is delivered completely, every send included

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /emails

Request:

{
   "to": "ada.lovelace@example.com",
   "subject": "Invoice ready"
}

Response (202 Accepted):

{
   "status": "QUEUED",
   "to": "ada.lovelace@example.com"
}

POST /emails

Request:

{
   "to": "grace.hopper@example.com",
   "subject": "Weekly digest"
}

Response (202 Accepted):

{
   "status": "QUEUED",
   "to": "grace.hopper@example.com"
}

Application log after a recipient stayed unreachable through all three attempts:

WARN c.s.api.service.EmailDispatchService : Giving up on gone.forever@example.com

More in Scheduling and Async Work

See the module
Greenhouse Sensor PollerThis problem covers running work on a repeating timer in the background, with nothing needed to trigger it.0%0 of 1 solvedmediumOverdue Fine Calculator JobThis problem covers running a job on a nightly schedule that recalculates fines from each loan's due date.—No data yetmediumNightly Sales Digest BuilderThis problem covers asking three sources at the same time instead of one after another, and building one result from whatever answers.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.