> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/module-development/capabilities/interceptors.md).

# Interceptors

Interceptors follow the Observer/Intercepting Filter pattern, letting your module react to runtime events. Create them in **pure BoxLang** or **Java**.

{% tabs %}
{% tab title="🟦 BoxLang Interceptors" %}
Register interceptors in your `ModuleConfig.bx` `configure()` method:

**ModuleConfig.bx:**

```js
function configure() {
    settings = {};

    // Register interceptors
    interceptors = [
        {
            class: "interceptors.RequestLogger",
            properties: {
                logHeaders: true,
                logBody: false
            }
        }
    ];

    // Declare custom interception points
    customInterceptionPoints = [
        "onBeforeGreeting",
        "onAfterGreeting"
    ];
}
```

**File:** `interceptors/RequestLogger.bx`

```js
class {
    property name="logHeaders";
    property name="logBody";

    function onRequestStart( event ) {
        log.info( "Request started!" )

        if ( logHeaders ) {
            log.debug( "Headers: #event.getData()#" )
        }
    }

    function onRequestEnd( event ) {
        log.info( "Request ended!" )
    }
}
```

{% endtab %}

{% tab title="☕ Java Interceptors" %}
Implement the `IInterceptor` interface and annotate listener methods with `@InterceptionPoint`:

**File:** `src/main/java/ortus/boxlang/modules/mymodule/interceptors/RequestLogger.java`

```java
import ortus.boxlang.runtime.interceptors.IInterceptor;
import ortus.boxlang.runtime.interceptors.InterceptionPoint;
import ortus.boxlang.runtime.events.BoxEvent;
import ortus.boxlang.runtime.types.IStruct;
import ortus.boxlang.runtime.types.Struct;

public class RequestLogger implements IInterceptor {

    @InterceptionPoint( BoxEvent.ON_REQUEST_START )
    public void onRequestStart( IStruct data ) {
        System.out.println( "Request started!" );
    }

    @InterceptionPoint( BoxEvent.ON_REQUEST_END )
    public void onRequestEnd( IStruct data ) {
        System.out.println( "Request ended!" );
    }
}
```

**ServiceLoader Config:** `META-INF/services/ortus.boxlang.runtime.interceptors.IInterceptor`

```
ortus.boxlang.modules.mymodule.interceptors.RequestLogger
```

{% endtab %}
{% endtabs %}

## Lambda Interceptors

Register inline closures as interceptors for quick event handling:

```js
function configure() {
    interceptors = [
        {
            class: ( event ) => {
                log.info( "Quick listener fired!" )
            },
            name: "QuickListener@myModule"
        }
    ];
}
```

## Custom Interception Points

Declare your own events that other code can listen to:

```js
function configure() {
    customInterceptionPoints = [
        "onBeforeGreeting",
        "onAfterGreeting"
    ];
}

// Announce from module code
function doGreeting( required string name ) {
    announce( "onBeforeGreeting", { name: name } )

    var greeting = "Hello, #name#!"

    announce( "onAfterGreeting", { name: name, greeting: greeting } )

    return greeting
}
```

## Listening to Lifecycle Events

Your `ModuleConfig.bx` is automatically registered as an interceptor, so you can add any event method directly:

```js
class {
    // Listen for other modules loading
    function postModuleLoad( event ) {
        var moduleName = event.getData().moduleName
        log.info( "Module loaded: #moduleName#" )
    }

    // Listen for runtime startup
    function onModuleServiceStartup( event ) {
        log.info( "All modules are ready!" )
    }
}
```

{% hint style="info" %}
Interceptor names follow the convention `ClassName@moduleName`. If not specified, it's auto-derived from the class name.
{% endhint %}

## Next Steps

* [Services](/boxlang-framework/module-development/capabilities/services.md) — Global runtime services (Java only)
* [Schedulers](/boxlang-framework/module-development/capabilities/schedulers.md) — Scheduled task management (Java only)
* [Advanced Collaboration](/boxlang-framework/module-development/advanced-collaboration.md) — System settings and class resolvers


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/module-development/capabilities/interceptors.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
