Problem statement
Build the notification fan-out of a warehouse stock service: every stock change reaches three background handlers, and one handler's failure never touches the others.
Requirements
AsyncConfig
Out of the box this application would run event handlers on the very thread that publishes the event. Switch on background execution here and provide the dedicated worker pool the three handlers run on. The pool's threads must be named with the stock- prefix.
PricingListener, AlertingListener, AuditListener
Implement onStockChanged in each of the three classes. The three handlers follow the same rules and differ only in their handler name: PRICING, ALERTING, and AUDIT.
- Every published stock change event must reach all three handlers, and each handler must process it in the background: on a thread of the stock- pool, never on the thread serving the HTTP request and never on the main thread
- Before doing anything else, a handler must ask the provided FaultInjector to check its own handler name together with the event's sku; the check throws when the tests have scripted a fault for exactly that pair
- When the check passes, the handler records exactly one execution in the provided HandlerExecutionLog, carrying its own handler name and the event's sku; the log itself captures the time and the thread of the recording, so the recording must happen at the moment the handler runs, on the handler's own thread
- A change is recorded once per handler per event: after N changes each handler has N executions and the log holds 3xN entries overall, counted per sku
- When the check throws, the handler must catch the error, record nothing for that event, and instead log an error through the shared listener logger in StockListenerLog with exactly the message "Handler ", its own handler name, " failed for ", and the event's sku, for example "Handler PRICING failed for SKU-BOLT-M4". Exactly one failure line is written per faulted event; the handler does not retry
- A handler failure is contained: the other two handlers still complete normally for that same event, and neither the HTTP response nor the stored quantity is affected by the failure
- A fault is scoped to its handler and sku pair: while a fault is armed for one sku, changes to any other sku keep reaching all three handlers, and once the fault is cleared the next change for the same sku reaches all three handlers again
- A successful handler run writes no failure line
- Handlers run only for changes that actually happen: a rejected request (unknown sku or missing delta) must not produce any handler execution, while the next successful change is delivered normally
Provided stock endpoint and harness
The HTTP side is provided and complete: PATCH /stock/{sku} takes a JSON body with an integer delta, adds it to the stored quantity, saves the row, replies with the sku and the updated quantity, and announces a stock change event carrying the sku, the old quantity, and the new quantity. An unknown sku is rejected with 404 Not Found; the error message is "Stock item not found with sku: " followed by the requested sku. A body without delta is rejected with 400 Bad Request and the error message "Delta is required". Both rejections carry a JSON body with error, timestamp, and path fields. Also provided: HandlerExecutionLog (a thread-safe log that records the handler name, sku, time, and thread of every execution), FaultInjector (lets the tests script a failure for any handler and sku pair), and StockListenerLog (the shared logger the handlers write their failure lines through). Three stock items are seeded at startup for manual play.
Sample Requests & Responses
Expand to view details on sample requests and responses.
PATCH /stock/SKU-WIDGET-A
Request:
{
"delta": -5
}
Response (200 OK):
{
"sku": "SKU-WIDGET-A",
"quantity": 35
}
PATCH /stock/SKU-UNKNOWN (unknown sku)
Request:
{
"delta": 1
}
Response (404 Not Found):
{
"error": "Stock item not found with sku: SKU-UNKNOWN",
"timestamp": "2026-08-05T23:39:56.066906",
"path": "/stock/SKU-UNKNOWN"
}
PATCH /stock/SKU-BOLT-M4 (missing delta)
Request:
{}
Response (400 Bad Request):
{
"error": "Delta is required",
"timestamp": "2026-08-05T23:39:56.076845",
"path": "/stock/SKU-BOLT-M4"
}
Application log after a change whose PRICING handler had a scripted fault:
ERROR stock.listeners : Handler PRICING failed for SKU-BOLT-M4
