apache/dubbo

Context loss when async invoke

Open

#13.666 geöffnet am 16. Jan. 2024

Auf GitHub ansehen
 (14 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Java (26.453 Forks)batch import
component/sdkhelp wantedtype/enhancement

Repository-Metriken

Stars
 (41.524 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 7T 14h) (20 gemergte PRs in 30 T)

Beschreibung

provider service method:

	public CompletableFuture<Integer> newToken(NewTokenInfo tReq) {
		TokenInfo tokenInfo = new TokenInfo(JsonUtils.toJson(tReq), 60);	
		// 可以设置回应附加信息
//		RpcContext.getServerResponseContext().setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
		// 可以设置回应附加信息
//		RpcContext.getServerContext().setAttachment("testInfo", JsonUtils.toJson(tokenInfo));

		CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
		completableFuture = completableFuture.completeAsync(()->{
			logger.debug("tokenInfo:" + JsonUtils.toJson(tokenInfo));
			return 1;
		}, Executors.newFixedThreadPool(1));
		
		// 不可以设置回应附加信息
		RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
//		RpcContextAttachment serverResponseContext = RpcContext.getClientAttachment();
//		RpcContextAttachment serverResponseContext = RpcContext.getServerContext();
//		RpcServiceContext serverResponseContext = RpcContext.getServiceContext();
//		RpcServiceContext serverResponseContext = RpcContext.getCurrentServiceContext();
//		RpcContextAttachment serverResponseContext = RpcContext.getClientResponseContext();
//		RpcContextAttachment serverResponseContext = RpcContext.getServerAttachment();
		completableFuture = completableFuture.thenApply(ret->{
			// 不可以设置回应附加信息
//			RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
//			RpcContextAttachment serverResponseContext = RpcContext.getClientAttachment();
//			RpcContextAttachment serverResponseContext = RpcContext.getServerContext();
//			RpcServiceContext serverResponseContext = RpcContext.getServiceContext();
//			RpcServiceContext serverResponseContext = RpcContext.getCurrentServiceContext();
//			RpcContextAttachment serverResponseContext = RpcContext.getClientResponseContext();
//			RpcContextAttachment serverResponseContext = RpcContext.getServerAttachment();
			
			serverResponseContext.setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
			return ret;
		});
		return completableFuture;
	}

consumer :

		NewTokenInfo tReq = new NewTokenInfo("testType", "testToken");
		CompletableFuture<Integer> future = tokenRpcProxy.newToken(tReq);
		CompletableFuture<Void> completableFuture = future.thenAccept(ret->{
			logger.debug("newToken: " + ret + ", testInfo: " + RpcContext.getClientResponseContext().getAttachment("testInfo"));
			logger.debug("newToken: " + ret + ", testInfo: " + RpcContext.getServerContext().getAttachment("testInfo"));
		});
		try {
			completableFuture.get();
		} catch (Exception e) {
			throw new RuntimeException("未知异常", e);
		}

provider的方法是异步执行,异步执行完后才能往RpcContext设置回应附加信息,这种情况回应附加信息没法传递到consumer这边

Contributor Guide