-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for async methods in Spring MVC. (#1652)
- Loading branch information
1 parent
733f0e0
commit 9c64c24
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.sentry.spring; | ||
|
||
import io.sentry.IHub; | ||
import io.sentry.Sentry; | ||
import java.util.concurrent.Callable; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.core.task.TaskDecorator; | ||
import org.springframework.scheduling.annotation.Async; | ||
|
||
/** | ||
* Sets a current hub on a thread running a {@link Runnable} given by parameter. Used to propagate | ||
* the current {@link IHub} on the thread executing async task - like MVC controller methods | ||
* returning a {@link Callable} or Spring beans methods annotated with {@link Async}. | ||
*/ | ||
public final class SentryTaskDecorator implements TaskDecorator { | ||
@Override | ||
public @NotNull Runnable decorate(final @NotNull Runnable runnable) { | ||
final IHub oldState = Sentry.getCurrentHub(); | ||
final IHub newHub = Sentry.getCurrentHub().clone(); | ||
return () -> { | ||
Sentry.setCurrentHub(newHub); | ||
try { | ||
runnable.run(); | ||
} finally { | ||
Sentry.setCurrentHub(oldState); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters