bringwu commented on issue #13666:
URL: https://github.com/apache/dubbo/issues/13666#issuecomment-1895450063
@AlbumenJ there is my new test code:
api
```
public interface AsyncContext {
public CompletableFuture<Integer> newToken(String token);
CompletableFuture<Integer> sayHi(String name);
}
```
provider:
```
@DubboService
public class AsyncContextImpl implements AsyncContext {
private static final Logger logger =
LoggerFactory.getLogger(AsyncContextImpl.class);
public CompletableFuture<Integer> newToken(String token) {
CompletableFuture<Integer> completableFuture = new
CompletableFuture<>();
completableFuture = completableFuture.completeAsync(() -> {
logger.debug("tokenInfo:" + token);
return 1;
}, Executors.newFixedThreadPool(1));
RpcContextAttachment serverResponseContext =
RpcContext.getServerResponseContext();
completableFuture = completableFuture.thenApply(ret -> {
serverResponseContext.setAttachment("tokenInfo", token);
return ret;
});
return completableFuture;
}
@Override
public CompletableFuture<Integer> sayHi(String name) {
String text = "hi, " + name;
CompletableFuture<Integer> completableFuture = new
CompletableFuture<>();
completableFuture = completableFuture.completeAsync(() -> {
logger.debug(text);
return 1;
}, Executors.newFixedThreadPool(1));
RpcContextAttachment serverResponseContext =
RpcContext.getServerResponseContext();
completableFuture = completableFuture.thenApply(ret -> {
serverResponseContext.setAttachment("testInfo", text);
return ret;
});
return completableFuture;
}
}
```
consumer:
```
@Component
public class AsyncContextTest {
private static final Logger logger =
LoggerFactory.getLogger(AsyncContextTest.class);
@DubboReference
private AsyncContext asyncContext;
public void sayHi() {
CompletableFuture<Integer> future = asyncContext.sayHi("dubbo");
CompletableFuture<Void> completableFuture =
future.thenAccept(ret->{
logger.debug("ret: " + ret + ", testInfo: " +
RpcContext.getClientResponseContext().getAttachment("testInfo"));
});
try {
completableFuture.get();
} catch (Exception e) {
throw new RuntimeException("error", e);
}
}
public void newToken() {
String token = "dubboToken";
CompletableFuture<Integer> future =
asyncContext.newToken(token);
CompletableFuture<Void> completableFuture =
future.thenAccept(ret->{
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getClientResponseContext().getAttachment("tokenInfo"));
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getServerAttachment().getAttachment("tokenInfo"));
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getServiceContext().getAttachment("tokenInfo"));
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getServerContext().getAttachment("tokenInfo"));
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getCurrentServiceContext().getAttachment("tokenInfo"));
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getServerResponseContext().getAttachment("tokenInfo"));
logger.debug("newToken: " + ret + ", rsp: " +
RpcContext.getClientAttachment().getAttachment("tokenInfo"));
});
try {
completableFuture.get();
} catch (Exception e) {
throw new RuntimeException("error", e);
}
}
}
```
where Bootstrap class like that, it prints:
```
@SpringBootApplication
@EnableDubbo
public class Bootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(Bootstrap.class, args);
applicationContext.getBean(AsyncContextTest.class).newToken();
applicationContext.getBean(AsyncContextTest.class).sayHi();
applicationContext.getBean(AsyncContextTest.class).sayHi();
applicationContext.stop();
}
}
2024-01-17 17:39:25.121 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: dubboToken
2024-01-17 17:39:25.121 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: dubboToken
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.123 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.133 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:39:25.146 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
```
where Bootstrap class like that, it prints error:
```
@SpringBootApplication
@EnableDubbo
public class Bootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(Bootstrap.class, args);
applicationContext.getBean(AsyncContextTest.class).sayHi();
applicationContext.getBean(AsyncContextTest.class).sayHi();
applicationContext.getBean(AsyncContextTest.class).newToken();
// see this
applicationContext.stop();
}
}
2024-01-17 17:45:11.242 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:45:11.249 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:45:11.258 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.258 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.259 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG
c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
```
it there any error in my code?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]