You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Graph Java Client调用接口后JVM无法退出的问题求助

问题:Microsoft Graph Java Client 发起请求后JVM无法退出

环境配置

  • Microsoft Graph Java Client v5.36.0
  • azure-identity 1.6.0
  • azure-core-http-okhttp 1.11.2
  • 测试环境:Eclipse

问题现象

  • 仅构建Graph客户端但不发起请求时,JVM可正常退出
  • 发起任意Graph请求后,主线程执行完毕退出,但JVM始终处于活跃状态(Eclipse中显示红色运行图标)

核心复现代码

final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
        .clientId("--------------------------------------------")
        .clientSecret("----------------------------------------")
        .tenantId("--------------------------------------------")
        .build();

List<String> scopes=new ArrayList<>();
scopes.add("https://graph.microsoft.com/.default");

final TokenCredentialAuthProvider tokenCredentialAuthProvider = 
        new TokenCredentialAuthProvider(scopes, clientSecretCredential);

GraphServiceClient graphClient = GraphServiceClient
                                    .builder()
                                    .authenticationProvider(tokenCredentialAuthProvider)
                                    .buildClient();

MessageCollectionRequest req=graphClient
                                    .users("--").messages()
                                    .buildRequest(queryUnread,queryNoOfMails,queryExpandAttachment);
MessageCollectionPage page=req.get();
List<Message> messageList=page.getCurrentPage();

排查尝试

自行创建独立OKHttp客户端并绑定到GraphClient,请求执行后手动清理资源:

自定义OKHttp客户端初始化代码

OkHttpClient httpClient=HttpClients.createDefault(tokenCredentialAuthProvider);

GraphServiceClient graphClient=GraphServiceClient
                                .builder()
                                .authenticationProvider(tokenCredentialAuthProvider)
                                .httpClient(httpClient)
                                .buildClient();

资源清理代码

System.out.println(httpClient.connectTimeoutMillis());//Prints 10000
System.out.println(httpClient.connectionPool().connectionCount());//prints 1
httpClient.connectionPool().evictAll();
System.out.println(httpClient.connectionPool().connectionCount());//prints 0
httpClient.dispatcher().executorService().shutdown();
System.out.println(httpClient.dispatcher().executorService().isShutdown());//prints true

执行上述清理后,OKHttp连接池已清空、调度器线程池已标记为关闭,但JVM仍无法正常退出,排除OKHttp连接池问题。

疑问

是代码中遗漏了必要的资源释放步骤,还是相关库本身存在未终止后台线程的问题?

内容的提问来源于stack exchange,提问作者Ironluca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:20:28