# Batch processing

## Introduction

The goal is to provide a batch life-cycle for nodes to integrate with

Nodes may want to process events as part of a larger transaction. For example a set of bank transfer events may make up a single transaction that prints a confirmation on completion. We could tie the confirmation node to a batch life-cycle.

Fluxtion provides two annotations that configure batch processing:

| Annotation                                                                                                                                  | Behaviour                                                                              |
| ------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| [@OnBatchPause](https://github.com/v12technology/fluxtion/blob/master/builder/src/main/java/com/fluxtion/api/annotations/OnBatchPause.java) | Notifies the node more messages are expected but there is a pause in the message flow. |
| [@OnBatchEnd](https://github.com/v12technology/fluxtion/blob/master/builder/src/main/java/com/fluxtion/api/annotations/OnBatchEnd.java)     | Notifies the node that the batch has completed.                                        |

The generated SEP implements the [BatchHandler](https://github.com/v12technology/fluxtion/blob/master/api/src/main/java/com/fluxtion/runtime/lifecycle/BatchHandler.java) interface. Applications can call the BatchHandler methods and the SEP will handle all dispatch logic to nodes on the execution graph. Batch handlers are invoked in reverse topological order.

{% hint style="info" %}
Fluxtion event processors can be exceedingly fast, even waiting between network packets. Batch pause permits actions to execute if enough data has been received.
{% endhint %}

## Example

The example shows two nodes that declare `batchPause` and `batchEnd` methods using the annotations defined. Batch invocations are in reverse topological order.

The code for the example is located [here](https://github.com/v12technology/fluxtion/tree/master/examples/documentation-examples/src/main/java/com/fluxtion/example/core/events/batch).

### The node classes

```java
public class DataHandler {

    @EventHandler
    public void dataEvent(DataEvent event) {}

    @OnBatchPause
    public void batchPause() {}

    @OnBatchEnd
    public void batchEnd() {}
}


public class BatchNode {
    
    private final DataHandler handler;

    public BatchNode(DataHandler handler) {
        this.handler = handler;
    }
    
    @OnBatchPause
    public void batchPause(){}
    
    @OnBatchEnd
    public void batchEnd(){}
}
```

### The generated SEP

As described the node batch methods are invoked in the SEP BatchHandler interface methods. The child node does not declare any batch methods as is not included in batch callbacks.

```java
public class SampleProcessor implements EventHandler, BatchHandler, Lifecycle {

  //Node declarations
  private final DataHandler dataHandler_1 = new DataHandler();
  private final BatchNode batchNode_3 = new BatchNode(dataHandler_1);
  private final ChildNode childNode_5 = new ChildNode(batchNode_3);
  //code omitted for clarity

  public void handleEvent(DataEvent typedEvent) {
    //Default, no filter methods
    dataHandler_1.dataEvent(typedEvent);
    childNode_5.recalculate();
    //event stack unwind callbacks
    afterEvent();
  }

  @Override
  public void batchPause() {
    batchNode_3.batchPause();
    dataHandler_1.batchPause();
  }

  @Override
  public void batchEnd() {
    batchNode_3.batchEnd();
    dataHandler_1.batchEnd();
  }
}
```

### png

The png generated by the Fluxtion ESC:

![Sample SEP demonstrating batch processing](/files/-LU4WwdJcxU_W1Qd_ggV)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fluxtion.gitbook.io/docs/examples/reference/child-2/batch-processing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
