问题
I am integrating with a third party, who has provided an Azure service bus queue for us to receive messages. (We're using the 0.9.0 Azure jars from the download link at https://azure.microsoft.com/en-us/documentation/articles/java-download-azure-sdk/)
I set up a connection like so:
Configuration config = new Configuration();
config = ServiceBusConfiguration.configureWithConnectionString(null, config, connectionString);
ServiceBusContract azureService = ServiceBusService.create(config);
And receive messages like so:
ReceiveQueueMessageResult resultQM = azureService.receiveQueueMessage(queueName, receiveMessageOptions);
This works fine under normal circumstances. However, at the office, I have to go through a proxy, and the connection fails with this error:
com.microsoft.windowsazure.exception.ServiceException: com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection timed out: connect
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusExceptionProcessor.receiveQueueMessage(ServiceBusExceptionProcessor.java:141)
at com.mycompany.dr.theircompany.TheirCompanyDataListener.receiveMessage(TheirCompanyDataListener.java:127)
at com.mycompany.dr.theircompany.TheirCompanyDataListener.lambda$0(TheirCompanyDataListener.java:75)
at java.lang.Thread.run(Unknown Source)
Caused by: com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection timed out: connect
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
at com.microsoft.windowsazure.services.servicebus.implementation.AuthorizationFilter.handle(AuthorizationFilter.java:39)
at com.microsoft.windowsazure.core.pipeline.jersey.ClientFilterRequestAdapter.handle(ClientFilterRequestAdapter.java:35)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
at com.sun.jersey.api.client.WebResource.post(WebResource.java:251)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusRestProxy.receiveMessage(ServiceBusRestProxy.java:248)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusRestProxy.receiveQueueMessage(ServiceBusRestProxy.java:216)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusExceptionProcessor.receiveQueueMessage(ServiceBusExceptionProcessor.java:137)
... 3 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler$1$1.getOutputStream(URLConnectionClientHandler.java:234)
at com.sun.jersey.api.client.CommittingOutputStream.commitWrite(CommittingOutputStream.java:117)
at com.sun.jersey.api.client.CommittingOutputStream.flush(CommittingOutputStream.java:100)
at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at com.sun.jersey.core.util.ReaderWriter.writeToAsString(ReaderWriter.java:191)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeToAsString(AbstractMessageReaderWriterProvider.java:128)
at com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:88)
at com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:58)
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:213)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
... 11 more
Now, when I look at the documentation for com.microsoft.windowsazure.Configuration, it shows constants for two properties:
- PROPERTY_HTTP_PROXY_HOST (String value "http.proxyHost")
- PROPERTY_HTTP_PROXY_PORT (String value "http.proxyPort")
However, these constants don't seem to exist in 0.9.0. I've tried adding the properties to the Configuration using the string literals,
config.setProperty("http.proxyHost", "proxy.mycompany.com");
config.setProperty("http.proxyPort", 8080);
but this had no noticeable effect.
Am I even on the right track here? Is there any way to set a proxy for the service bus contract in 0.9.0?
回答1:
According to the source code of Class Configuration, the properties PROPERTY_HTTP_PROXY_HOST
& PROPERTY_HTTP_PROXY_PORT
are defined as static final variables. So you can't change their values via the method setProperty
.
As I known, there are two ways support proxy for Java which can be used in your current scenario.
Command Line JVM Settings: The proxy settings are given to the JVM via command line arguments:
java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword
Setting System Properties in Code Add the following lines in your Java code so that JVM uses the proxy to make HTTP calls. This would, of course, require you to recompile your Java source. (The other methods do not require any recompilation):
System.setProperty("http.proxyPort", "someProxyPort"); System.setProperty("http.proxyUser", "someUserName"); System.setProperty("http.proxyPassword", "somePassword"); System.setProperty("http.proxyHost", "someProxyURL");
More information for Networking & Proxies & Properties in Java, Please refer to http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html and http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html.
来源:https://stackoverflow.com/questions/39922666/can-i-set-a-proxy-for-azure-service-bus-connections-in-java-sdk-0-9-0