---
title: "Async Methods"
description: "Learn about how to propagate Scope to Spring async methods."
url: https://docs.sentry.io/platforms/java/guides/spring-boot/async/
---

# Async Methods | Sentry for Spring Boot

Sentry's SDK for Java stores the scope and the context in a thread-local variable. To make sure that an async method has access to the correct Sentry context, a `SentryTaskDecorator` must be set on the `ThreadPoolTaskExecutor`.

## [Configuring Spring MVC Async Task Executor](https://docs.sentry.io/platforms/java/guides/spring-boot/async.md#configuring-spring-mvc-async-task-executor)

To propagate Sentry scope to Spring MVC methods returning a `Callable` or a `StreamingResponseBody`, a `ThreadPoolTaskExecutor` decorated with `SentryTaskDecorator` must be set on the `AsyncSupportConfigurer` through an implementation of `WebMvcConfigurer`:

```java
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import io.sentry.spring.SentryTaskDecorator;

@Configuration
class AsyncWebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(asyncExecutor());
    }

    private AsyncTaskExecutor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setTaskDecorator(new SentryTaskDecorator());
        executor.initialize();
        return executor;
    }
}
```

Alternatively, if there is a single bean of type `TaskDecorator` in the context, instead of custom `WebMvcConfigurer`, it is enough to create a `SentryTaskDecorator` bean.

```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.sentry.spring.SentryTaskDecorator;

@Configuration
class SentryConfig {

    @Bean
    public SentryTaskDecorator sentryTaskDecorator() {
        return new SentryTaskDecorator();
    }
}
```

## [Configuring @Async Methods Task Executor](https://docs.sentry.io/platforms/java/guides/spring-boot/async.md#configuring-async-methods-task-executor)

To propagate Sentry scope to Spring `@Async` annotated methods, a custom `AsyncConfigurerSupport` must be configured to return a `ThreadPoolTaskExecutor` decorated with `SentryTaskDecorator`:

```java
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import io.sentry.spring.SentryTaskDecorator;

import java.util.concurrent.Executor;

@Configuration
class AsyncMethodConfiguration extends AsyncConfigurerSupport {
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setTaskDecorator(new SentryTaskDecorator());
        executor.initialize();
        return executor;
    }
}
```
