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

Nightly Sales Digest Builder

medium—No data yet

Problem statement

Implement the fan-out that builds the nightly sales digest from three independent sales feeds in parallel, under a shared deadline, degrading gracefully when a feed is slow or down.

Requirements

DigestAsyncConfig

Set up background execution for source fetches on a dedicated executor. Its threads must have names starting with digest- and there must be enough of them for all three sources to be fetched at the same time.

SalesFetchService

fetchAmount must run the given source's fetch on the digest executor and return a future that completes with the fetched amount, or completes exceptionally when the source throws. The provided sources record the thread name of every invocation, and those records must show only digest- threads, never the request thread.

DigestService

buildDigest assembles one digest per call:

  • Start the fetch of all three sources at the same time through SalesFetchService, then combine the outcomes into a single digest; with every source delayed by about 250 ms the whole request finishes well under 600 ms rather than the 750 ms one-by-one fetching would need, and never before the slowest source has answered
  • List the sources in the fixed order STORE, ONLINE, WHOLESALE
  • Consult each source exactly once per digest and rebuild the digest on every call; nothing is cached between calls
  • A source that answers in time appears as an entry carrying exactly its source name, status OK, and its amountCents
  • Every source gets the same deadline: the number of milliseconds configured in the digest.source-timeout-ms property, counted from the moment the fetches are started. A source that has not answered by that deadline, or that fails, appears as an entry carrying exactly its source name and status UNAVAILABLE, with no amountCents field
  • A request held up by a slow source stays bounded near the deadline instead of waiting out that source's full delay, and a failing source is reported without waiting for the deadline at all
  • The digest never fails as a whole: when every source is slow or failing the call still succeeds with three UNAVAILABLE entries
  • totalCents is the sum of amountCents over the OK entries only; with no OK entry it is 0
  • A source that recovers (its delay or failure goes away) is OK again on the next digest

DigestController

  • GET /digest returns 200 OK with the digest built by DigestService: a JSON object carrying exactly sources and totalCents

Provided sales sources

The three source beans (StoreSalesSource, OnlineSalesSource, WholesaleSalesSource) are provided and complete: each returns its sales figure in cents, records every invocation with the thread name and start time, and can be given a simulated delay or a simulated failure. The tests script the sources directly; for manual runs the digest.store.simulated-delay-ms and digest.store.simulated-failure properties and their online and wholesale counterparts do the same. The per-source deadline ships in application.properties as digest.source-timeout-ms (2000 by default); the tests supply smaller values for the same property.

Sample Requests & Responses

Expand to view details on sample requests and responses.

GET /digest

Response (200 OK):

{
   "sources": [
      {
         "source": "STORE",
         "status": "OK",
         "amountCents": 125000
      },
      {
         "source": "ONLINE",
         "status": "OK",
         "amountCents": 98500
      },
      {
         "source": "WHOLESALE",
         "status": "OK",
         "amountCents": 240000
      }
   ],
   "totalCents": 463500
}

GET /digest with the app started with --digest.wholesale.simulated-delay-ms=5000 (the wholesale feed answers after the 2000 ms deadline):

Response (200 OK):

{
   "sources": [
      {
         "source": "STORE",
         "status": "OK",
         "amountCents": 125000
      },
      {
         "source": "ONLINE",
         "status": "OK",
         "amountCents": 98500
      },
      {
         "source": "WHOLESALE",
         "status": "UNAVAILABLE"
      }
   ],
   "totalCents": 223500
}

GET /digest with the app started with --digest.online.simulated-failure=true (the online feed throws on every fetch):

Response (200 OK):

{
   "sources": [
      {
         "source": "STORE",
         "status": "OK",
         "amountCents": 125000
      },
      {
         "source": "ONLINE",
         "status": "UNAVAILABLE"
      },
      {
         "source": "WHOLESALE",
         "status": "OK",
         "amountCents": 240000
      }
   ],
   "totalCents": 365000
}

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 yetmediumBulk Email Dispatch QueueThis problem covers handing work to a background worker pool, retrying failures on a growing delay and giving up after a few tries.—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.