Master Distributed Task Scheduling with PowerJob: A Step‑by‑Step Guide
This article introduces PowerJob, a next‑generation distributed scheduling and computation framework, explains why a central scheduler is needed in microservice architectures, and provides detailed installation, configuration, and code examples for setting up the server, worker, and tasks using Docker, MySQL, MongoDB, and Spring Boot.
PowerJob Overview
PowerJob is a new generation distributed scheduling and computation framework that simplifies task scheduling and distributed computation in microservice systems.
Simple to use: provides a web UI for visual management of tasks and logs.
Rich timing policies: supports CRON, fixed rate, fixed delay, and API‑based scheduling.
Multiple execution modes: standalone, broadcast, Map, and MapReduce.
Lightweight dependencies: requires only a relational database (e.g., MySQL) and optionally MongoDB for large online logs.
Why a Central Scheduler?
In microservice architectures, embedding scheduling code (e.g., Quartz or Spring Task) in many services leads to duplication and management difficulties. A dedicated scheduler like PowerJob centralizes task definitions, allowing services to expose executable methods while the scheduler triggers them.
Installation Preparation
PowerJob’s server component (powerjob‑server) stores data in MySQL and logs in MongoDB, so both services must be running.
Start MySQL in a Docker container:
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7Create the required database:
CREATE DATABASE IF NOT EXISTS `powerjob-product` DEFAULT CHARSET utf8mb4;Start MongoDB in a Docker container:
docker run -p 27017:27017 --name mongo \
-v /mydata/mongo/db:/data/db \
-d mongo:4.2.5Install the Scheduler Center
Pull the PowerJob server image: docker pull tjqq/powerjob-server:latest Run the server container, linking it to MySQL and MongoDB:
docker run -p 7700:7700 -p 10086:10086 --name powerjob-server \
--link mysql:db \
--link mongo:mongo \
-e TZ="Asia/Shanghai" \
-e JVMOPTIONS="" \
-e PARAMS="--spring.profiles.active=product --spring.datasource.core.jdbc-url=jdbc:mysql://db:3306/powerjob-product?useUnicode=true&characterEncoding=UTF-8 --spring.datasource.core.username=root --spring.datasource.core.password=root --spring.data.mongodb.uri=mongodb://mongo:27017/powerjob-product" \
-v ~/docker/powerjob-server:/mydata/powerjob/powerjob-server \
-v ~/.m2:/mydata/powerjob/.m2 \
-d tjqq/powerjob-server:latestAfter the container starts, access the web UI at http://<server-ip>:7700/ (ensure ports 7700 and 10086 are open).
Initialize the Worker in a Spring Boot Application
Add the powerjob-worker-spring-boot-starter dependency to pom.xml:
<dependency>
<groupId>com.github.kfcfans</groupId>
<artifactId>powerjob-worker-spring-boot-starter</artifactId>
<version>3.2.3</version>
</dependency>Configure the worker in application.yml (pay attention to powerjob.worker.app-name):
powerjob:
worker:
akka-port: 27777 # akka port
app-name: mall-tiny-powerjob # application name for isolation
server-address: 192.168.3.101:7700 # scheduler address
store-strategy: disk # persistence modeCreate a processor by implementing BasicProcessor and its process method:
package com.macro.mall.tiny.job;
@Slf4j
@Component
public class StandaloneProcessor implements BasicProcessor {
@Override
public ProcessResult process(TaskContext context) {
// OmsLogger can report logs to powerjob-server
OmsLogger omsLogger = context.getOmsLogger();
omsLogger.info("StandaloneProcessor start process, context is {}.", context);
log.info("jobParams is {}", context.getJobParams());
return new ProcessResult(true, "Process success!");
}
}Package the Spring Boot application into a Docker image and run it, ensuring the timezone matches the scheduler:
docker run -p 8080:8080 --name mall-tiny-powerjob \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny-powerjob/logs:/var/logs \
-e TZ="Asia/Shanghai" \
-d mall-tiny/mall-tiny-powerjob:1.0-SNAPSHOTTask Configuration and Execution
With the worker and scheduler running, tasks can be created in the PowerJob web UI.
Register the application by setting powerjob.worker.app-name (e.g., mall-tiny-powerjob:123456) in application.yml.
In the “Task Management” page, add a new task using a CRON expression (e.g., every 20 seconds) that invokes the processor method.
Start the task from the task list; view execution logs via “More → Run Records”.
Inspect the processor’s reported logs and the ProcessResult returned by the task.
References
Official documentation: https://github.com/KFCFans/PowerJob
Project source code: https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-powerjob
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
