Skip to content

Proper way of passing manually generated Context when using CompletableFuture #9356

Answered by laurit
zbytt asked this question in Q&A
Discussion options

You must be logged in to vote

Activating the scope the way you are doing it is definitely the most reliable way. Java agent also has automatic propagation for completable futures where callbacks like thenAccept are called with the context where the future was completed. Note that if thenAccept is called on an already completed future then thenAccept will be called with the current context. So the alternative you could try is

try (final Scope ignore = context.makeCurrent()) {
        CompletableFuture.supplyAsync(() -> {
                //do something with the msg
                return processMessage(message);
            }, executor)
            .thenAccept(msg -> {
                ack(msg);
            })
            .

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@zbytt
Comment options

Answer selected by zbytt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants