# t3573393/jdjjob architecture

> Architecture diagram of the GitHub repository t3573393/jdjjob, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/t3573393/jdjjob
- Repository: https://github.com/t3573393/jdjjob

## Overview

##### Project type and purpose
- This is an open-source Java library (a port of Shopify’s delayed_job) that lets Java applications enqueue and process long-running tasks asynchronously.
- It provides an API for producers to enqueue “jobs” (plain Java objects), persists them in a MySQL table, and runs worker processes that poll the table, deserialize the job objects, execute them, and record success or failure.

##### File structure
- Top-level:
- pom.xml – Maven build, declares dependencies (MySQL JDBC driver, commons-dbutils, c3p0, Jackson).
- jobs.sql – DDL for the `jobs` table.
- Source layout (jdjjob/src/main/java/org/fartpig/jdjjob):
- DJJob & DJWorker – core enqueue/poll APIs.
- DJJobDao & DBUtils – DAO layer using commons-dbutils.
- Jackson integration for serializing job handlers into the `handler` column.
- DJRetryException, DJException – retry/failure control.
- Resources:
- c3p0-config.xml – database connection pool config.
- logback.xml – logging configuration.
- Tests mirror main packages, verifying DAO, serialization, utilities.

##### Key insights from the README
- System uses a single `jobs` table in MySQL to store job metadata and serialized handlers.
- Jackson is used for JSON serialization, c3p0 for connection pooling, commons-dbutils for DAO operations.
- Producer calls `DJJob.enqueue(job, queueName)`; worker is `new DJWorker(opts).start()`.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Producer Flow
    subgraph "Producer Flow"
        direction TB
        App["Producer App"]:::inproc
        DJJob["DJJob API Layer"]:::inproc
        Jackson["Serialization Engine (Jackson)"]:::lib
        DAO["DAO Layer"]:::lib
        C3P0["Connection Pool (c3p0)"]:::lib
        DB["MySQL (jobs table)"]:::db
    end

    App -->|"enqueue(job, queueName)"| DJJob
    DJJob -->|"serialize(handler)"| Jackson
    Jackson -->|"SQL operations"| DAO
    DAO -->|"getConnection()"| C3P0
    C3P0 -->|"INSERT INTO jobs"| DB

    %% Worker Flow
    subgraph "Worker Flow"
        direction TB
        Worker["Worker Process (DJWorker)"]:::inproc
        DAO2["DAO Layer"]:::lib
        C3P02["Connection Pool (c3p0)"]:::lib
        DB2["MySQL (jobs table)"]:::db
        Jackson2["Serialization Engine (Jackson)"]:::lib
        Handler["Job Handler Object"]:::inproc
        Error["Retry & Error Handling"]:::inproc
    end

    Worker -->|"poll SELECT ready jobs"| DAO2
    DAO2 -->|"getConnection()"| C3P02
    C3P02 -->|"SELECT * FROM jobs"| DB2
    DB2 -->|"result set"| DAO2
    DAO2 -->|"deserialize(handler)"| Jackson2
    Jackson2 -->|"handler instance"| Handler
    Handler -->|"perform()"| Handler
    Handler -->|"on success"| DAO2
    DAO2 -->|"DELETE or mark done"| DB2

    Handler -->|"throws DJRetryException/DJException"| Error
    Error -->|"update attempts, failed_at, error"| DAO2
    DAO2 -->|"UPDATE jobs"| DB2

    %% Logging
    Logger["Logging (logback)"]:::logging
    App -->|"log events"| Logger
    DJJob -->|"log events"| Logger
    Worker -->|"log events"| Logger
    Handler -->|"log events"| Logger
    Error -->|"log events"| Logger

    %% Click Events
    click DJJob "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/DJJob.java"
    click Worker "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/DJWorker.java"
    click Jackson "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/Utils.java"
    click Jackson2 "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/Utils.java"
    click DAO "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/dao/DJJobDao.java"
    click DAO2 "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/dao/DJJobDao.java"
    click C3P0 "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/resources/c3p0-config.xml"
    click C3P02 "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/resources/c3p0-config.xml"
    click DB "https://github.com/t3573393/jdjjob/blob/master/jdjjob/jobs.sql"
    click DB2 "https://github.com/t3573393/jdjjob/blob/master/jdjjob/jobs.sql"
    click HandlerInterface "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/DJJobHandlerInterface.java"
    click Job "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/Job.java"
    click Logger "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/resources/logback.xml"
    click RetryEx "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/DJRetryException.java"
    click Ex "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/DJException.java"
    click Base "https://github.com/t3573393/jdjjob/blob/master/jdjjob/src/main/java/org/fartpig/jdjjob/DJBase.java"

    %% Styles
    classDef inproc fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef lib fill:#FFA500,stroke:#000,stroke-width:1px
    classDef db fill:#90EE90,stroke:#000,stroke-width:1px
    classDef logging fill:#D3D3D3,stroke:#000,stroke-width:1px
```
