Master Distributed Task Scheduling with PowerJob: Setup, Code, and Deployment
Learn how to install and configure PowerJob, a next‑generation distributed scheduling and computation framework, by setting up MySQL and MongoDB, deploying the server and worker via Docker, adding Maven dependencies, defining processors, and managing tasks through its web UI for reliable microservice job orchestration.
PowerJob Introduction
PowerJob is a next‑generation distributed scheduling and computation framework that simplifies task scheduling and distributed processing.
Main features:
Simple to use: provides a front‑end web UI for visual task management and log viewing.
Rich timing strategies: supports CRON, fixed rate, fixed delay, and API‑based scheduling.
Multiple execution modes: single‑node, broadcast, Map, and MapReduce.
Light dependencies: only requires a relational database (MySQL) and optionally MongoDB for large logs.
Why a Scheduling Center?
Typical frameworks like
QuartZor
Spring Taskembed scheduling code in each microservice, which becomes unwieldy at scale. A dedicated scheduling center centralizes task definitions, allowing services to focus on business logic while the center handles execution.
Installation Preparation
PowerJob’s server component (
powerjob-server) stores data in MySQL and logs in MongoDB, so both services must be running first.
Start a MySQL container:
<code>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.7</code>Create the PowerJob database:
<code>CREATE DATABASE IF NOT EXISTS `powerjob-product` DEFAULT CHARSET utf8mb4;</code>Start a MongoDB container:
<code>docker run -p 27017:27017 --name mongo \
-v /mydata/mongo/db:/data/db \
-d mongo:4.2.5</code>Install the Scheduling Center
Pull the Docker image:
<code>docker pull tjqq/powerjob-server:latest</code>Run the server container (exposing ports 7700 and 10086):
<code>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:latest</code>After the container starts, access the PowerJob web UI at
http://<your‑host>:7700/(ensure ports 7700 and 10086 are open in the firewall).
Initialize the Executor in a SpringBoot Application
Add the worker starter dependency to
pom.xml:
<code><dependency>
<groupId>com.github.kfcfans</groupId>
<artifactId>powerjob-worker-spring-boot-starter</artifactId>
<version>3.2.3</version>
</dependency></code>Configure the worker in
application.yml(pay attention to
powerjob.worker.app-name):
<code>powerjob:
worker:
akka-port: 27777 # akka port
app-name: mall-tiny-powerjob # application identifier
server-address: 192.168.3.101:7700 # PowerJob server URL
store-strategy: disk # persistence mode</code>Create a processor by implementing
BasicProcessor:
<code>package com.macro.mall.tiny.job;
@Slf4j
@Component
public class StandaloneProcessor implements BasicProcessor {
@Override
public ProcessResult process(TaskContext context) {
// OmsLogger can directly push 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!");
}
}
</code>Package the SpringBoot application into a Docker image and run it (ensure the timezone matches the server):
<code>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-SNAPSHOT</code>Task Configuration and Execution
With the server and worker running, tasks can be created and managed via the PowerJob UI.
Register the application name (the same as
powerjob.worker.app-name) in the UI.
After registration, a machine entry appears in the dashboard.
Create a new task using a CRON expression (e.g., every 20 seconds) that invokes the processor method.
Click **Run** to start the task immediately.
View execution records under **More → Run Records**.
Check logs for the processor’s output and the job parameters passed.
Inspect the task details to see the
ProcessResultreturned by the processor.
Project Source Code
Source repository: https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-powerjob
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.