Async Methods
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
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
:
Copied
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;
}
}
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
:
Copied
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;
}
}
- Package:
- maven:io.sentry:sentry-spring
- Version:
- 7.13.0
- Repository:
- https://github.com/getsentry/sentry-java