Skip to content
Permalink
Browse files
fix attachments of async sample (#410)
  • Loading branch information
chickenlj committed Dec 29, 2021
1 parent 8353cea commit 7e286cf17725f6188a1099c58f6ff8eef9ca59ab
Showing 4 changed files with 15 additions and 10 deletions.
@@ -20,5 +20,5 @@
# Supported component versions of the test case

# Spring app
dubbo.version=2.7*, 3.*
dubbo.version=3.*
spring.version=4.*, 5.*
@@ -40,10 +40,9 @@ public static void main(String[] args) throws Exception {
RpcContext.getContext().setAttachment("consumer-key1", "consumer-value1");

CompletableFuture<String> future = asyncService.sayHello("async call request");
RpcContext savedServerContext = RpcContext.getServerContext();
CountDownLatch latch = new CountDownLatch(1);
future.whenComplete((v, t) -> {
System.out.println((String) savedServerContext.getAttachment("server-key1"));
System.out.println((String) RpcContext.getServerContext().getAttachment("server-key1"));
if (t != null) {
logger.warn("Exception: ", t);
} else {
@@ -44,7 +44,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
if (StringUtils.isEmpty(filters)) {
filters = "";
}
filters += " legacy-block-filter";
filters += " legacy-listenable-filter";
context.setAttachment("filters", filters);

return invoker.invoke(invocation);
@@ -19,7 +19,9 @@

package org.apache.dubbo.samples.async.impl;

import org.apache.dubbo.rpc.AsyncContext;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcContextAttachment;
import org.apache.dubbo.samples.async.api.AsyncService;

import org.slf4j.Logger;
@@ -32,16 +34,20 @@ public class AsyncServiceImpl implements AsyncService {

@Override
public CompletableFuture<String> sayHello(String name) {
RpcContext savedContext = RpcContext.getContext();
RpcContext savedServerContext = RpcContext.getServerContext();
// If attachments and context are going to be used in the new thread, startAsync() and signalContextSwitch() must be called.
// Otherwise, it is not necessary to call these two methods.
AsyncContext asyncContext = RpcContext.startAsync();
return CompletableFuture.supplyAsync(() -> {
String received = (String) savedContext.getAttachment("consumer-key1");
asyncContext.signalContextSwitch();
RpcContextAttachment attachmentFromClient = RpcContext.getServerAttachment();
RpcContextAttachment attachmentToClient = RpcContext.getServerContext();
String received = (String) attachmentFromClient.getAttachment("consumer-key1");
logger.info("consumer-key1 from attachment: " + received);
savedServerContext.setAttachment("server-key1", "server-" + received);
attachmentToClient.setAttachment("server-key1", "server-" + received);

received = (String) savedContext.getAttachment("filters");
received = (String) attachmentFromClient.getAttachment("filters");
logger.info("filters from attachment: " + received);
savedServerContext.setAttachment("filters", received);
attachmentToClient.setAttachment("filters", received);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {

0 comments on commit 7e286cf

Please sign in to comment.