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
