Threadmill Examples
This module is deliberately written like an application, not like Threadmill
internals. Its package is com.example.threadmill so users can copy the shape
into their own package instead of inheriting a library-author namespace.
It contains three levels of copyable examples:
./gradlew :threadmill-example:run— a tiny single-processInMemoryJobStoreexample that enqueues one job, starts a node, waits for success, and exits.- Manual multi-instance Postgres and Redis examples — start two or three workers against one real store, submit jobs from another terminal, and watch the workers share the queue.
- Submitter examples that enqueue work into the shared Postgres or Redis stores while workers are already running.
Shared datastores
Section titled “Shared datastores”Start Postgres and Redis:
docker compose up -d postgres redisDefaults:
- Postgres:
jdbc:postgresql://localhost:55432/threadmill - Redis:
redis://localhost:56379/0
Override with:
THREADMILL_JDBC_URLTHREADMILL_DB_USERTHREADMILL_DB_PASSWORDTHREADMILL_REDIS_URI
Redis is started with AOF enabled because Redis durability depends on Redis persistence settings.
Single-process getting started
Section titled “Single-process getting started”./gradlew :threadmill-example:runRead:
src/main/java/com/example/threadmill/GettingStartedMain.java
That file shows the minimum wiring: create a store, create a Scheduler, start
a ProcessingNode, enqueue a payload, and wait until the job succeeds.
Manual Postgres cluster
Section titled “Manual Postgres cluster”Terminal 1:
./gradlew :threadmill-example:runWorker --args="postgres alpha"Terminal 2:
./gradlew :threadmill-example:runWorker --args="postgres bravo"Terminal 3:
./gradlew :threadmill-example:submit --args="postgres 100"The two workers race through the same Postgres threadmill_jobs table. Claims
are atomic, so one job is owned by one node at a time. Kill a worker with
Ctrl-C while jobs are running; the surviving node will recover abandoned work
after the heartbeat timeout.
Manual Redis cluster
Section titled “Manual Redis cluster”Terminal 1:
./gradlew :threadmill-example:runWorker --args="redis alpha"Terminal 2:
./gradlew :threadmill-example:runWorker --args="redis bravo"Terminal 3:
./gradlew :threadmill-example:submit --args="redis 100"The workers share Redis queues and processing indexes. The claim path commits the serialized body and all Redis indexes in one Lua script, so a process crash before commit leaves the job enqueued and a crash after commit leaves it recoverable from processing indexes.
Cleanup
Section titled “Cleanup”docker compose downAdd -v if you deliberately want to drop both datastore volumes.