Using system properties in Java

I have a question about using System Properties in Java. Some classes like Authenticator require that we set system properties in relation to proxy settings and check if the proxy was actually valid or not.

My question is, should I remove Set Properties after I have finished using it?

There are other parts of the programs that can use these Properties, this change will automatically affect their functionality.

Is there a way I can set local properties to a function (some wrapper class)?

What are the best practices for setting system properties and using them?

+2


source to share


3 answers


Things that use System.properties must have properties that are globally meaningful for the JVM to start, so if you, for example, specify a proxy, it must be the appropriate proxy in the process.

Therefore, there is no need to install them back. In fact, disabling them can lead to confusion in some APIs as they may assume that they always return the appropriate value and do not just cache it when read.



Of course, if a given API does not use them in this way, you may have problems, but it really will be a problem with this API, more than a problem with good practice in system properties.

In general, due to threading and synchronization issues, it is probably a good idea to set system properties only at the beginning of the JVM startup (either on the command line or on the main thread before starting other threads) with the expectation that the values ​​remain unchanged for the remainder of the runtime. JVM.

+3


source


This doesn't answer your question about system properties in general, but in relation to your specific problem with proxy properties settings, perhaps you can use the ProxySelector to isolate the test proxy mentioned here in the comments?

You can subclass ProxySelector that you use for your test. Do this so that it only applies the test settings when the test URI was tested. This would isolate him from other requests.

This global inflexibility of proxy settings is what initially prompted me to use HttpClient for HTTP requests instead of the Sun API.



Edit:

I'm not sure how I missed this method, but it is possible to get the url connection and provide the proxy parameters only to that connection via java.net.Url.openConnection (proxy) .

+1


source


If there is a possibility that some other parts of your program (or some other web applications in a container, etc.) may be affected by "temporary" settings, then it is recommended to remove them.

The best practice would be to try to find another way to do what you are trying to do. For example, consider creating your own protocol class that overrides the standard scope in which it determines which proxy to use.

If you can't do this, try structuring your code so that the sequence is:

  • change properties,
  • perform the operation,
  • restore properties,

is executed in a mutex, which is enforced in everything that can be affected by the properties you change. This can be a difficult demand though ...

0


source







All Articles