Azure Notification Hub 1.1.0在Java 8遗留项目中调用异常求助
问题分析与解决建议
核心问题原因
抛出的java.lang.NoSuchMethodError: javax.net.ssl.SSLParameters.setApplicationProtocols([Ljava/lang/String;)V本质是JDK版本不兼容:
setApplicationProtocols是JDK 9才新增的API- JBoss 7.0 EPA通常配套JDK 8(或更低版本),但Azure Notification Hub 1.1.0 SDK依赖的Apache HttpClient 5.x组件直接调用了这个仅JDK9+存在的方法,导致反射调用失败
- 请求陷入无限循环,是因为HttpClient的IO reactor在遇到错误后不断重试连接,形成死循环
可行解决办法
1. 强制HttpClient使用HTTP/1.1而非HTTP/2
Azure Notification Hub SDK默认可能启用了HTTP/2,而HttpClient 5.x在HTTP/2模式下会触发这个TLS扩展方法。可以通过自定义配置关闭HTTP/2,强制使用HTTP/1.1:
// 创建NotificationHub时传入自定义HttpClient配置 HttpClientBuilder clientBuilder = HttpClients.custom() .setVersion(HttpVersion.HTTP_1_1) // 强制使用HTTP/1.1 .disableAutomaticRetries(); // 关闭自动重试,避免无限循环 NotificationHub notificationHubClient = new NotificationHub( "notHubConnString", notificationHubName, clientBuilder.build() );
2. 降级HttpClient依赖版本
如果自定义配置无效,可以尝试将SDK自带的HttpClient 5.x替换为兼容JDK8的HttpClient 4.x版本,需要先排除SDK自带的高版本依赖:
以Maven为例,在pom.xml中配置:
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>azure-notificationhubs</artifactId> <version>1.1.0</version> <exclusions> <exclusion> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents.core5</groupId> <artifactId>httpcore5</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加兼容JDK8的HttpClient 4.x --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.14</version> </dependency>
注意:替换后需要测试SDK功能是否正常,因为1.1.0版本可能基于HttpClient5开发,部分功能可能受影响。
3. 调整JBoss的SSL配置(备选)
在JBoss 7.0的standalone.xml或domain.xml中修改SSL连接器配置,禁用ALPN(应用层协议协商),避免触发HTTP/2协商:
<subsystem xmlns="urn:jboss:domain:web:1.1" default-virtual-server="default-host" native="false"> <connector name="https" protocol="HTTP/1.1" scheme="https" socket-binding="https" secure="true"> <ssl name="https-ssl" key-alias="your-key-alias" password="your-password" certificate-key-file="path/to/keystore" protocol="TLSv1.2" disable-alpn="true"/> </connector> </subsystem>
代码基础错误修复
你的代码里有个明显的类型不匹配错误,String[] regIds = "";应该根据模板数量初始化数组:
// 根据模板集合大小初始化数组 String[] regIds = new String[androidNotificationHubRegistrationTemplates.size()];
否则后续赋值会抛出ArrayStoreException,建议先修复这个问题。
内容的提问来源于stack exchange,提问作者user6090970
相关产品推荐
相关产品推荐

