Backend Development 10 min read

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.

macrozheng
macrozheng
macrozheng
Master Distributed Task Scheduling with PowerJob: A Step‑by‑Step Guide

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:

<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 required database:

<code>CREATE DATABASE IF NOT EXISTS `powerjob-product` DEFAULT CHARSET utf8mb4;</code>

Start MongoDB in a Docker container:

<code>docker run -p 27017:27017 --name mongo \
  -v /mydata/mongo/db:/data/db \
  -d mongo:4.2.5</code>

Install the Scheduler Center

Pull the PowerJob server image:

<code>docker pull tjqq/powerjob-server:latest</code>

Run the server container, linking it to MySQL and MongoDB:

<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 web UI at

http://<server-ip>:7700/

(ensure ports 7700 and 10086 are open).

PowerJob UI
PowerJob UI

Initialize the Worker in a Spring Boot Application

Add the

powerjob-worker-spring-boot-starter

dependency to

pom.xml

:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.github.kfcfans&lt;/groupId&gt;
    &lt;artifactId&gt;powerjob-worker-spring-boot-starter&lt;/artifactId&gt;
    &lt;version&gt;3.2.3&lt;/version&gt;
&lt;/dependency&gt;</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 name for isolation
    server-address: 192.168.3.101:7700 # scheduler address
    store-strategy: disk # persistence mode</code>

Create a processor by implementing

BasicProcessor

and its

process

method:

<code>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!");
    }
}
</code>

Package the Spring Boot application into a Docker image and run it, ensuring the timezone matches the scheduler:

<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 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

.

Register Application
Register Application

In the “Task Management” page, add a new task using a CRON expression (e.g., every 20 seconds) that invokes the processor method.

Create Task
Create Task

Start the task from the task list; view execution logs via “More → Run Records”.

Run Records
Run Records

Inspect the processor’s reported logs and the

ProcessResult

returned by the task.

Task Details
Task Details

References

Official documentation: https://github.com/KFCFans/PowerJob

Project source code: https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-powerjob

Dockerdistributed schedulingmicroservicesSpring BootPowerJobtask automation
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.