Backend Development 14 min read

Annotation‑Driven Responsibility Chain Framework (auto‑pipeline) Overview

The annotation‑driven auto‑pipeline framework generates a linked‑list responsibility chain from @AutoPipeline‑annotated interfaces, automatically wiring handler implementations to execute sequentially and return the first non‑null result, thereby boosting development efficiency, reusability, and correctness for complex business pipelines such as configuration sources and message throttling.

DaTaobao Tech
DaTaobao Tech
DaTaobao Tech
Annotation‑Driven Responsibility Chain Framework (auto‑pipeline) Overview

This article introduces the annotation‑based responsibility‑chain framework foldright/auto-pipeline , which aims to improve development efficiency and reusability when building complex business pipelines.

Background : Traditional procedural implementations split a workflow into N independent steps, leading to high coupling, low correctness, and poor reusability. The classic Responsibility Chain pattern solves part of the problem but still requires manual wiring of handlers.

Solution – auto‑pipeline : By annotating an interface with @AutoPipeline , the framework automatically generates a pipeline that links handler implementations. The generated pipeline executes handlers in order, returning the first non‑null result.

Basic Usage Example :

public void process(params){
    doFirst(params);
    doSecond(params);
    ...
    doLast(params);
}

After applying @AutoPipeline to a configuration interface:

@AutoPipeline
public interface ConfigSource {
    String get(String key);
}

Implement handlers:

public class MapConfigSourceHandler implements ConfigSourceHandler {
    private final Map
map;
    public MapConfigSourceHandler(Map
map) { this.map = map; }
    @Override
    public String get(String key, ConfigSourceHandlerContext ctx) {
        String v = map.get(key);
        return (v != null && !v.isBlank()) ? v : ctx.get(key);
    }
}
public class SystemConfigSourceHandler implements ConfigSourceHandler {
    public static final SystemConfigSourceHandler INSTANCE = new SystemConfigSourceHandler();
    @Override
    public String get(String key, ConfigSourceHandlerContext ctx) {
        String v = System.getProperty(key);
        return (v != null && !v.isBlank()) ? v : ctx.get(key);
    }
}

Assemble the pipeline:

Map
map = new HashMap<>();
map.put("hello", "world");
ConfigSourceHandler mapHandler = new MapConfigSourceHandler(map);
ConfigSource pipeline = new ConfigSourcePipeline()
        .addLast(mapHandler)
        .addLast(SystemConfigSourceHandler.INSTANCE);

String v1 = pipeline.get("hello"); // returns "world"
String v2 = pipeline.get("java.specification.version"); // returns system property

Implementation Principles : The generated pipeline class holds a linked list of handlers (pre, next, handler). During execution, each handler can either return a value or delegate to the next node. If the tail returns null, the pipeline returns null.

Code Generation : The annotation processor AutoPipelineProcessor is registered via SPI. At compile time it scans interfaces annotated with @AutoPipeline , validates they are public interfaces, and uses JavaPoet to generate the corresponding pipeline and handler interfaces. Key classes include:

AutoPipelineProcessor – entry point that processes annotations.

SourceGeneratorFacade – orchestrates source file generation.

HandlerGenerator – creates handler interfaces with an extra ConfigSourceHandlerContext parameter.

Advanced Example – Message Throttling : The article shows a real‑world use case where a message‑throttling pipeline is built using auto‑pipeline. Interfaces such as MessageThrottler are annotated, and concrete handlers ( ClassificationThrottlerHandler , DuplicateThrottlerHandler , AcquireThrottlerHandler ) implement specific throttling logic. A proxy class assembles these handlers into a pipeline and provides high‑level throttling APIs.

Team Introduction : The framework is developed by the Taobao Mobile Technology Platform team, which focuses on backend core capabilities, FaaS, QUIC, and mobile middleware.

backendJavacode generationDesign Patternannotation-processingPipeline
DaTaobao Tech
Written by

DaTaobao Tech

Official account of DaTaobao Technology

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.