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

Greenhouse Sensor Poller

medium0%0 of 1 solved

Problem statement

Implement the background polling loop of a greenhouse monitoring service that stores a temperature reading on every cycle and rides out sensor outages.

Requirements

SchedulingConfig

Out of the box this application never executes any periodic work. Switch on the execution of periodically scheduled tasks here so the poller below can run.

SensorPoller

The sensor is reached through the provided SensorClient, which either reports the current temperature or throws its unreachable error when the greenhouse link is down. Implement pollSensor so the sensor is polled continuously in the background, without any HTTP request or other outside trigger:

  • pollSensor must run repeatedly on a fixed cadence: a new cycle starts one interval after the previous cycle started, regardless of how long a cycle takes
  • The interval in milliseconds is read from the greenhouse.poll-rate-ms configuration property; the project ships the production value 30000, one poll every 30 seconds, and that shipped value must stay in place. The tests run the poller much faster by supplying a smaller value for the same property
  • Each cycle asks SensorClient for the current temperature. On success, store one new reading carrying exactly the temperature the sensor reported and the time the reading was taken. Readings keep accumulating cycle after cycle for as long as the application runs, and when the greenhouse temperature changes, later readings carry the new value
  • When the sensor client raises its unreachable error, log a warning with exactly the message: "Sensor unreachable, skipping cycle", store nothing for that cycle, and leave every previously stored reading untouched
  • A failed cycle must not stop the schedule: while the sensor stays unreachable, the same warning is logged again on every new cycle and nothing is stored
  • The warning is never logged while the sensor is reachable
  • Once the sensor is reachable again, storing resumes on its own, without a restart, and the new readings carry the temperature the sensor currently reports; this holds across repeated outages

Provided readings endpoint

GET /readings is provided and complete: it returns every stored reading newest first, each carrying exactly id, temperature, and recordedAt. With the poller in place it serves whatever the cycles have stored, including readings from before and after an outage.

Sample Requests & Responses

Expand to view details on sample requests and responses.

GET /readings

Response (200 OK), shortly after startup:

[
    {
        "id": 1,
        "temperature": 21.5,
        "recordedAt": "2026-08-05T22:34:50.2518"
    }
]

GET /readings

Response (200 OK), about two minutes after startup:

[
    {
        "id": 4,
        "temperature": 21.5,
        "recordedAt": "2026-08-05T22:36:20.253433"
    },
    {
        "id": 3,
        "temperature": 21.5,
        "recordedAt": "2026-08-05T22:35:50.253755"
    },
    {
        "id": 2,
        "temperature": 21.5,
        "recordedAt": "2026-08-05T22:35:20.255476"
    },
    {
        "id": 1,
        "temperature": 21.5,
        "recordedAt": "2026-08-05T22:34:50.2518"
    }
]

More in Scheduling and Async Work

See the module
Overdue 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 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.