In Oracle WebLogic, what is the best way to set an environmental variable so that it can be accessed by your code? We have third-party apps running WebLogic that look for an environment variable.
Note: We start our managed servers using Node Manager.
I would prefer to be able to set it somewhere in the domain configuration, like in the Server Start tab in the Admin Console, but there seems no good place to do that.
The only way I can see to do it would be
bin/setDomainEnv.sh
to export the environmental variablenodemanager.properties
to have StartScriptEnabled=true
What this does is, forces NodeManager to use the <ms_home>/bin/startManagedWebLogic.sh
, which sources setDomainEnv.sh
and they will be picked up when NodeManager starts. But you also have to do this on every machine.
Wondering if there is a cleaner way of doing this than mucking with Oracle's startup scripts.
WebLogic can also be slow to start up on Linux if it's connecting to a database and you haven't set up DNS properly. Depending on your setup, you might be able to edit /etc/resolv. conf and comment out the nameserver line. This can reduce WebLogic's startup time from 20 minutes to less than a minute.
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
If you know for certain that none of the common frameworks are in use, like the Spring Framework, and you have code that strictly looks for environment variables, then you must set the environment variables outside of any of the usual configuration files, before the Java process that will be expecting it, is started. Once the Java process is started, environment variables are read-only and final for that process.
Note: If you need Environment Variables for the entire system, use /etc/profile, /etc/bash_profile, /etc/environment, etc. Keep in mind, that setting the variables in these global locations requires that you restart the Node Manager from a fresh login. You do not need to reboot, but profile/environment files are usually only sourced on login.
For apps within just one domain or node, environment variables should be in the startup scripts for the server(s). Editing setDomainEnv.[sh|cmd] or start(Managed)Weblogic.[sh|cmd], is the best option for setting WebLogic environment variables.
However, if the app is using Spring, system properties and environment variables are combined. System properties are highly encouraged and easier to maintain and control.
Ref: What is best practice for setting java system properties, -D or System.setProperty()?
Weblogic Domain environment variables
One of the places to set both system properties or environment variables, is to edit the domain environment script used to start all nodes or servers that share the same WebLogic server installation and domain. Inside < weblogic_domain >/bin/setDomainEnv.sh, (setDomainEnv.cmd on windows), for environment variables, just add them near the top and add comments to document their use.
export CUSTOM_VAR="test" # UNIX comment to describe environment variable.
For System Properties, you can add command line arguments that will be added to every server by adding a line for EXTRA_JAVA_PROPERTIES, near the top of the file, near the WL_HOME definition, but after the functions and comments.
EXTRA_JAVA_PROPERTIES="-Denv=TEST"
export EXTRA_JAVA_PROPERTIES
WL_HOME="/appl/oracle/middleware/wls/12.1.2.0.0/wlserver"
export WL_HOME
Weblogic Node-specific environment variables
If you need different Environment Variables for each node that is started up by the same Node Manager, you will have to customize the startup scripts a little more. In that case, edit the < weblogic_domain >/bin/startManagedWeblogic.[sh|cmd] and insert some scripting logic after _export SERVER_NAME_. This way, you can drive your settings based on SERVER_NAME, etc.
Tip: Windows Environment Variables are not case-sensitive with System.getenv(..).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With