Is there a way to change the value of a java global variable at runtime in websphere application server? I use a properties file in my java application to specify the values of global variables. But I need to change the values of the variables at runtime sometimes. For example, if i have a variable by the name 'fileServerLocation' and the value of it is 'C:\Users\test\Downloads' and if I want to change it to 'C:\Users\test\Desktop', is there a way I can change the value of the variable without having to re-deploy the application on the server?
-
3What is a _java global variable_? – Sotirios Delimanolis Aug 22 '13 at 18:05
-
You should use JNDI for this. Please check http://stackoverflow.com/questions/8259181/configuring-and-looking-up-a-simple-string-via-jndi-in-websphere. – ᄂ ᄀ Aug 22 '13 at 19:43
-
So you do not mean a global value? You mean a configuration value of some kind? – Raedwald Aug 22 '13 at 21:51
-
If you meant `java:global`, you should probably clarify your question because the current wording has confused several people. – Brett Kail Aug 23 '13 at 14:04
-
yeah, I should have said 'configuration value'. sorry for the confusion. – ancora imparo Aug 23 '13 at 18:52
3 Answers
You can build a web service or an EJB call that is triggered via an HTTP request and all it does is to change the field value at runtime. You may need to make this "admin request" to require authentication (e.g. username/password) to be executed. Also, you might need to declare this field as volatile
in order to be visible across all threads once changed.
Be aware that every ClassLoader
has its own value of the static field (the global variable). Usually all EJBs share the same class loader but each war
module has its own class loader.

- 115,165
- 71
- 313
- 417
-
do we have to pass the value of the variable as a web service request parameter every single time? – ancora imparo Aug 23 '13 at 18:35
-
@Learner It's up to you. You could pass simple integer in which it corresponds to a string value. – Eng.Fouad Aug 23 '13 at 18:46
Don't use "global variables". Create an internal service that provides the values to clients at runtime, and design the service so it has an interface that can accept changes at runtime, via a servlet, message-driven-bean, and/or some other mechanism. Also, have the service allow clients to register callbacks ("listeners") that can be notified when a specific variable value (or any variable value) changes.

- 85,615
- 20
- 155
- 190
-
the client doesn't have to know anything about the variable. the variable is used to represent a folder on the server side. – ancora imparo Aug 23 '13 at 18:34
I will not try to understand the details of the global variable but I suppose that the "Global variable" is a per Application global variable.
I will focus on how the dynamic update of in what way a state change should be performed.
I think that the correct way to do this typically management operation is to use the JMX standard. You must expose an MBean with an operation to dynamically update the value in runtime. The update of the value should be "thread safe" using some kind of memory barriers.
The update of the value will be executed using some kind of management tool (JMX client).

- 954
- 9
- 12