Java连接Azure Notification Hub出现连接超时问题求助
Hey there, let's tackle this connection timeout issue you're hitting with Azure Notification Hubs in Java—especially since your .NET team can push notifications without any problems. That kind of discrepancy usually boils down to a few key differences between the two stacks, so let's walk through the most likely fixes:
1. Update to the Official Azure SDK for Java
The com.windowsazure:NotificationHubs dependency you're using is an older, legacy SDK that's no longer actively maintained. Azure now recommends using the modern azure-messaging-notificationhubs library, which has better network handling and compatibility with current Azure services.
Replace your existing dependency with this one (for Maven):
<dependency> <groupId>com.azure</groupId> <artifactId>azure-messaging-notificationhubs</artifactId> <version>1.2.0</version> <!-- Use the latest version available --> </dependency>
This alone might resolve the timeout, as the new SDK uses updated HTTP clients and TLS configurations that align with Azure's current requirements.
2. Enforce TLS 1.2 for Java Connections
Azure Notification Hubs requires TLS 1.2 or higher for all connections. .NET applications often enable this by default, but older Java runtimes or libraries might default to TLS 1.0 or 1.1, which will get blocked.
Add this code early in your application (before initializing the Notification Hub client) to force TLS 1.2:
System.setProperty("https.protocols", "TLSv1.2");
You can also verify your Java runtime's default TLS version by running this command:
java -Djavax.net.debug=ssl:handshake -version
Look for the "Protocol Version" line in the output to confirm it's using TLS 1.2.
3. Check Proxy Configuration
If you're working behind a corporate proxy, .NET might automatically pick up system proxy settings, but Java requires explicit configuration.
Option 1: Set proxy via JVM arguments
When running your Java application, add these arguments:
java -Dhttp.proxyHost=your-proxy-host -Dhttp.proxyPort=your-proxy-port -Dhttps.proxyHost=your-proxy-host -Dhttps.proxyPort=your-proxy-port -jar your-app.jar
Option 2: Configure proxy in code
If you're using the new Azure SDK, you can set up a proxy for the Notification Hub client:
ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("your-proxy-host", your-proxy-port)); NotificationHubClient client = new NotificationHubClientBuilder() .connectionString("<your-connection-string>") .hubName("<your-hub-name>") .proxyOptions(proxyOptions) .buildClient();
4. Verify Network Access & Firewall Rules
Even if .NET works, your Java machine might have firewall rules blocking outbound traffic to Azure Notification Hubs (which uses port 443 for HTTPS).
Test connectivity to your hub's endpoint using telnet or curl:
telnet your-hub-endpoint.azure.net 443
If the connection fails, check your local firewall settings, corporate network policies, or any security groups that might be restricting outbound HTTPS traffic.
5. Double-Check Connection String & Hub Configuration
While .NET works, it's worth confirming that your Java code is using the exact same connection string (Primary Connection String from the Azure Portal's Access Policies section). A tiny typo (like a missing character or extra space) could cause authentication issues that manifest as timeouts.
Make sure you're using the full connection string format:
Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<key-name>;SharedAccessKey=<key-value>
Give these steps a try—start with updating the SDK and enforcing TLS 1.2, since those are the most common fixes for this kind of cross-stack discrepancy. If you're still stuck, share a snippet of your Java client initialization code, and we can dig deeper!
内容的提问来源于stack exchange,提问作者Swamy

