4

I'm coding my server in java, and through the day, my server has to connect through 5 different proxies at once to other servers and gather data. However, reading about java proxy settings through stackexchange, I see that when you set a proxy, its effect is VM-wide, meaning whatever network activity that .jar was doing, it will do it through a proxy if somewhere a different thread sets a proxy setting within the jar.

I'm currently using this method of setting a proxy, which according to some tests it's actually pretty functional and works fast.

    System.getProperties().put( "http.proxyHost", host );
    System.getProperties().put( "http.proxyPort", port );

However, I can't really afford having 5 jars doing the same thing with different proxies, I tried it to, it would be a simple solution however I can't afford to use that much ram only for this, as my server is huge.

user3423978
  • 41
  • 1
  • 3
  • No idea, but maybe [this class](http://docs.oracle.com/javase/7/docs/api/java/net/Proxy.html) will interest you. And even more [this one](http://docs.oracle.com/javase/7/docs/api/java/net/ProxySelector.html) – fge Mar 15 '14 at 19:31
  • I suggest using AsyncHttpClient which has support for http proxies https://github.com/AsyncHttpClient/async-http-client – hmashlah Mar 15 '14 at 19:34
  • Thanks for the suggestions. I'm reading upon the java Proxy class, I read it can be used with the URLConnection class and it doesn't say anywhere that the effect will be VM-Wide, the way you use that class wouldn't make sense to have a vm-wide effect either way. – user3423978 Mar 15 '14 at 19:57
  • That's correct. It only applies to the connection(s) you use it with. The terminology in your question is incorrect. System properties are JVM-wide, not per jar. – user207421 Mar 15 '14 at 22:38

2 Answers2

1

You need to call each connection with its own proxy settings. The Answer here by NickDk defines how you can call a url with its own proxy settings. You will need to do the same with each of your 5 proxies separately.

Community
  • 1
  • 1
arkoak
  • 2,437
  • 21
  • 35
0

here is described the use a library embeded in the JRE, able to handle "proxypac" files in wich any combination of proxies can be defined.

since it is embeded in the JRE, standard ways to configure a Java application with a proxypac file (standard launch optional parameters) might exist, but I am not aware of it.

Howhever the solution described in the link provided should fit your needs since your usage is programatic.

Community
  • 1
  • 1
user1767316
  • 3,276
  • 3
  • 37
  • 46