---
schema_version: "1"
id: 83c974d15cd558af4b4efaad502c10ecbea564787829d0fd98ead5815520c8db
title: Internal Handler Buffering
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/runtime-manager/internal-handler-buffering.md
canonical_url: https://docs.mulesoft.com/runtime-manager/internal-handler-buffering
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 1de6f9c86ab60dd528cd95fa64c27e337975ca232cb5f04e88126d6b92735251
description: MuleSoft Documentation Site
latest: true
---
---
product: Runtime Manager
keywords: agent, mule, esb, servers, monitor, notifications, external systems, third party, get status, metrics
---

# Internal Handler Buffering

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

[![logo hybrid active](/mulesoft/current/en-us/_/img/icons/hybrid-cloud.svg "Hybrid")Hybrid](deployment-strategies#hybrid-deployments)[ ![logo server active](/mulesoft/current/en-us/_/img/icons/server-cloud.svg "Anypoint Platform PCE")Private Cloud Edition](deployment-strategies#anypoint-platform-private-cloud-edition)

The Anypoint Runtime Manager agent provides a simple way of implementing internal handlers with
buffering support. This document explains how to implement and configure buffers for the Runtime
Manager agent internal handlers.

## Implement a Buffered Internal Handler

### BufferedHandler Abstract Class

To implement a buffered internal handler, you need to extend the `BufferedHandler<T>` abstract
class. The methods to overwrite are:

- `boolean canHandle(T message)`

  Returns true if the message can be handled by the internal handler.

- `boolean flush(Collection<T> collectionOfMessages)`

  Provides the implementation to send the messages stored in the buffer to the external service.

### Example

```java
@Named("com.mulesoft.agent.mypublisher")
@Singleton
public class MyPublisher extends BufferedHandler<List<Metric>>
{
    @Configurable(value = "mule")
    String metricPrefix;

    @Inject
    public MyPublisher()
    {
        super();
    }

    public MyPublisher(OnOffSwitch enabledSwitch)
    {
        super();
        this.enabledSwitch = enabledSwitch;
    }

    @Override
    public boolean canHandle(@NotNull List<Metric> metrics)
    {
        // Evaluates if the message could be handled
    }

    @Override
    public boolean flush(@NotNull Collection<List<Metric>> listOfMetrics)
    {
        // Send messages to external service
    }
}
```

## Configure a Buffering Internal Handler

### Buffering Configuration Object

If the internal handler includes buffering support, you can configure the buffer in the
`mule-agent.yml` file. The following table lists the available configuration parameters.

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Field</th><th>Description</th><th>Default value</th></tr></thead><tbody><tr><td><p><code>type</code></p></td><td><p>Buffer type. Valid values are <code>MEMORY</code> or <code>DISK</code>.</p></td><td></td></tr><tr><td><p><code>retryCount</code></p></td><td><p>Number of retries after an exception was thrown in the flush method.</p></td><td></td></tr><tr><td><p><code>maximumCapacity</code></p></td><td><p>Maximum number of events to keep in buffer.</p></td><td></td></tr><tr><td><p><code>flushFrequency</code></p></td><td><p>Frequency at which the flush method will run, in milliseconds.</p></td><td></td></tr><tr><td><p><code>filePath</code></p></td><td><p>Path to the buffer file. Only valid for buffers of type <code>DISK</code>.</p></td><td><p><code>PATH/bufferFile</code></p></td></tr><tr><td><p><code>whenExhausted</code></p></td><td><p>Strategy to follow when the buffer is exhausted. If not specified, the buffer will grow automatically. Valid values are <code>FAIL</code> and <code>FLUSH.</code></p></td><td><p>N/A</p></td></tr><tr><td><p><code>flushThreadsNumber</code></p></td><td><p>Number of threads used by the FlushThreadExecutor when the buffer is exhausted and the strategy defined by <code>whenExhausted</code> is <code>FLUSH</code> (see row above).</p></td><td><p><code>60</code></p></td></tr><tr><td><p><code>discardMessagesOnFlushFailure</code></p></td><td><p>If true, messages will not be returned to the buffer if the flush is not successful.</p></td><td><p><code>false</code></p></td></tr></tbody></table>

> [!IMPORTANT] The maximum size capacity for the buffer is configured based on a number of events
> and not in bytes. Keep in mind that if the events are of a considerable size and you don’t set
> `maximumCapacity` to a low value, the heap might suffer stress and this might impact the
> application’s performance.

If you configure a `DISK` buffer, the agent will also instantiate an in-memory buffer in order to
improve the performance of the I/O operations. This memory buffer has a default value of 10000
events and is flushed to disk every 500ms, and in batches of 10000 events each. These values are
configurable through the system properties `agent.disk.buffering.cache.size`,
`agent.disk.buffering.cache.flush.frequency` and `agent.disk.buffering.max.flushing.size`,
respectively.

> [!NOTE] Flushing from disk to the external system in batches of 10000 events each and the
> `agent.disk.buffering.max.flushing.size` property are supported for Mule agent plugin versions
> 2.1.9 and 1.11.3 and later.

### Sample `mule-agent.yml` Files

#### Configure Buffering in a Publisher

```yaml
---
muleInstanceUniqueId: validId
organizationId: organizationId

transports:
  websocket.transport:
    security:
      keyStorePassword: mykeystorePassword
      keyStoreAlias: agent
      keyStoreAliasPassword: agentpassword

  rest.agent.transport:
    security:
      keyStorePassword: mykeystorePassword
      keyStoreAlias: agent
      keyStoreAliasPassword: agentpassword
    port: 9997

services:
  mule.agent.application.service:
    enabled: true

  mule.agent.domain.service:
    enabled: true

  mule.agent.jmx.publisher.service:
    enabled: true
    frequency: 15
    frequencyTimeUnit: MINUTES
    beans:
      - beanQueryPattern: java.lang:type=Runtime
        attribute: Uptime
        monitorMessage: Monitoring memory up-time
      - beanQueryPattern: java.lang:type=MemoryPool,*
        attribute: Usage.used
        monitorMessage": Used Memory

internalHandlers:
  domaindeploymentnotification.internal.message.handler:
    enabled: true

  applicationdeploymentnotification.internal.message.handler:
    enabled: false

  com.mulesoft.agent.test.buffering.jmx.internal.handler:
    enabled: true
    buffer:
      type: DISK
      retryCount: 1
      flushFrequency: 10000
      maximumCapacity: 30
      filePath: publisher-buffer.log

externalHandlers:
  applications.request.handler:
    enabled: true

  domains.request.handler:
    enabled: true
```

#### Configure Buffering in Events Tracking

```yaml
---
muleInstanceUniqueId: validId
organizationId: organizationId

transports:
  websocket.transport:
    security:
      keyStorePassword: mykeystorePassword
      keyStoreAlias: agent
      keyStoreAliasPassword: agentpassword

  rest.agent.transport:
    security:
      keyStorePassword: mykeystorePassword
      keyStoreAlias: agent
      keyStoreAliasPassword: agentpassword
    port: 9997

services:
  mule.agent.application.service:
    enabled: true

  mule.agent.domain.service:
    enabled: true

  mule.agent.jmx.publisher.service:
    enabled: true
    frequency: 15
    frequencyTimeUnit: MINUTES
    beans:
      - beanQueryPattern: java.lang:type=Runtime
        attribute: Uptime
        monitorMessage: Monitoring memory up-time
      - beanQueryPattern: java.lang:type=MemoryPool,*
        attribute: Usage.used
        monitorMessage": Used Memory

internalHandlers:
  domaindeploymentnotification.internal.message.handler:
    enabled: true

  applicationdeploymentnotification.internal.message.handler:
    enabled: false

  tracking.notification.internal.message.handler:
    enabled: true
    buffer:
      type: MEMORY
      retryCount: 1
      flushFrequency: 10000
      maximumCapacity: 30

externalHandlers:
  applications.request.handler:
    enabled: true

  domains.request.handler:
    enabled: true
```
