2

I know Java supports the use of proxies either by setting system property:

System.setProperty("http.proxyHost", "domain.com"); System.setPropery("http.proxyPort", "8080"); 

Or by using the ProxySelector class.

The benefit of using ProxySelector class is that it can be enabled just for a specific URL.

My needs are a bit different.

I want to set a specific proxy when making a connection to an external (SOAP) web service but I want to change the proxy for each thread. In other words, I will be connecting to SOAP web service using multiple threads and I want thread a to use proxy a, thread b to use proxy b and so on (instead of using one proxy throughout the entire JVM)

Is this possible?

Clarification: I would like to have access to two+ different proxies at the same time, not sequentially so any solution which requires me to lock/synchronize access will not work.

1 Answer 1

1

You already have the pieces for a solution. Implement your own ProxySelector, and in the select method, choose a different proxy depending on the thread that invoked the select method.

You could have a Map<Thread,Proxy> in your ProxySelector implementation to store and pick the proxy for each thread from (be sure to access/update this map in a thread-safe manner)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I'm trying to use the ProxySelector class in a way that it could be accessed by multiple threads at the same time (so that the proxy is set on a thread basis as appose to VM-wide basis). If I understand your solution correctly, the proxy is still set per VM so the proxy could only be accessed sequentially (hence the need to be thread safe) Is it possible to set proxy on a thread basis? Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.