# Interceptors

> For the complete documentation index, see [llms.txt](https://docs.temporal.io/llms.txt).
> Any documentation page is available as raw Markdown by appending `.md` to its URL.
> For related questions, query https://docs.temporal.io/api/retrieve?q=<question>&jtb=<job-to-be-done>&src=md-page&agent=<your-name> (no auth; returns JSON `{content, source_url, page_url}`, with `source_url` pointing to Markdown for docs pages). Don't include personal data or credentials.
> If this page is wrong or missing something you needed, or Temporal behaves differently from what it says, report it with https://docs.temporal.io/api/feedback?title=<summary>&message=<what-happened>&category=documentation&page_url=/develop/java/workers/interceptors&src=md-page&agent=<your-name> (use `category=product` when the product, not the page, is wrong). Report only a specific problem you hit, send it once, and don't include personal data or credentials.

Interceptors wrap inbound and outbound Temporal calls so you can apply shared behavior such as tracing, logging, or
authorization. See [Interceptors](/encyclopedia/interceptors) for the inbound vs outbound model.

Register them on the Client (`WorkflowClientOptions.Builder.setInterceptors`) or the Worker Factory
(`WorkerFactoryOptions.Builder.setWorkerInterceptors`). Full API:
[`io.temporal.common.interceptors`](https://www.javadoc.io/doc/io.temporal/temporal-sdk/latest/io/temporal/common/interceptors/package-summary.html).

Several interceptor APIs are marked `@Experimental`.

## Implement a Client interceptor

Extend `WorkflowClientInterceptorBase` and override `workflowClientCallsInterceptor` to return a
`WorkflowClientCallsInterceptor` that wraps `next`. Override only the calls you want to intercept; forward the rest to
`next` (or to `super`, if you extend `WorkflowClientCallsInterceptorBase`).

<!--SNIPSTART java-interceptors-client-interceptor-->
[java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/TracingClientInterceptor.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/TracingClientInterceptor.java)
```java
public class TracingClientInterceptor extends WorkflowClientInterceptorBase {
    @Override
    public WorkflowClientCallsInterceptor workflowClientCallsInterceptor(
        WorkflowClientCallsInterceptor next) {
        return new WorkflowClientCallsInterceptorBase(next) {
            @Override
            public WorkflowStartOutput start(WorkflowStartInput input) {
                System.out.println("starting workflow " + input.getWorkflowId());
                return super.start(input);
            }
        };
    }
}
```
<!--SNIPEND-->

Register the interceptor when you build the Client:

<!--SNIPSTART java-interceptors-register-client {"enable_code_dedenting": true}-->
[java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/RegisterInterceptors.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/RegisterInterceptors.java)
```java
WorkflowClientOptions clientOptions =
    WorkflowClientOptions.newBuilder().setInterceptors(new TracingClientInterceptor()).build();
WorkflowClient client = WorkflowClient.newInstance(service, clientOptions);
```
<!--SNIPEND-->

## Implement a Worker interceptor

Extend `WorkerInterceptorBase` and override `interceptWorkflow`, `interceptActivity`, or both, for the inbound call
chains you need. Each inbound interceptor must forward every override to `next`. To also intercept outbound calls made
from inside the Workflow, wrap the `WorkflowOutboundCallsInterceptor` passed to `init` and forward that wrapper to
`next.init(...)`:

<!--SNIPSTART java-interceptors-worker-interceptor-->
[java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/TracingWorkerInterceptor.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/TracingWorkerInterceptor.java)
```java
public class TracingWorkerInterceptor extends WorkerInterceptorBase {
    @Override
    public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
        return new WorkflowInboundCallsInterceptorBase(next) {
            @Override
            public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
                next.init(
                    new WorkflowOutboundCallsInterceptorBase(outboundCalls) {
                        @Override
                        public <R> ActivityOutput<R> executeActivity(ActivityInput<R> input) {
                            Workflow.getLogger(TracingWorkerInterceptor.class)
                                .info("starting activity " + input.getActivityName());
                            return super.executeActivity(input);
                        }
                    });
            }
        };
    }
}
```
<!--SNIPEND-->

Register the interceptor on the Worker Factory:

<!--SNIPSTART java-interceptors-register-worker {"enable_code_dedenting": true}-->
[java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/RegisterInterceptors.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workers/interceptors/src/main/java/io/temporal/docs/workers/interceptors/RegisterInterceptors.java)
```java
WorkerFactoryOptions factoryOptions =
    WorkerFactoryOptions.newBuilder().setWorkerInterceptors(new TracingWorkerInterceptor()).build();
WorkerFactory factory = WorkerFactory.newInstance(client, factoryOptions);
```
<!--SNIPEND-->

> **⚠️ Warning:**
> Workflow interceptors and replay
>
> Workflow interceptor methods also run during [replay](/develop/java/best-practices/testing-suite#replay). Use replay-safe
> APIs for logging, randomness, and time.
>

## Related

- [Observability - Tracing](/develop/java/platform/observability#tracing)
- [Error handling - Worker Interceptor](/develop/java/best-practices/error-handling#centralize-with-interceptor)
- [Spring Boot - Interceptors](/develop/java/integrations/spring-boot-integration#interceptors)
- [Plugins - Interceptors](/develop/plugins-guide#interceptors)
