简体   繁体   中英

Set specified system property for web application

I have an JavaEE application which needs some certain system properties configured during the runtime.

During the development phase, we set the properties in the .pom.xml to make it work:

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <systemProperties>
                <xxx>true</xxx>
                </systemProperties>
            </configuration>
        </plugin>

However I wonder how about if we deploy the application in the production environment?

I have though of set the property during the initialization of the servlet, but it seems that I can not modify the system property once the jvm is runing(from this post ):

The JVM memory parameters for a Hotspot Java implementation can only be set via the command line options when the JVM is launched / started. Setting them in the system properties either before or after the JVM is launched will have no effect.

And I have tried to set the properties in the web.xml ( here ):

<env-entry>
    <env-entry-name>xx</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>xx</env-entry-value>
</env-entry>

But it seems like it does not take affect.

Then I wonder how to solve it?

BTW, in the production environment we may run more than one application under a shared tomcat, so we prefer to set the properties under the application context only.

Environmental Entries

The <env-entry> won't make it for you because Environmental Entries are availble through JNDI and not through the System properties facade. So if you configure some entry as below in your deployment descriptor file:

<env-entry>
  <env-entry-name>myProperty</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>some-value</env-entry-value>
</env-entry>

Accessing the property in codebase as below will return a null reference:

System.getProperty("myProperty");

Though it should be retrieved with a context lookup as follows:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up for your property "myProperty"
String myPropertyValue = (String) envCtx.lookup("myProperty");

The main advantage is that you will be able to have a property which value will vary from one web application to another. But this way, and as the amount of your deployed artifacts will rise, it will be hard to maintain this and I think, based on my humble experience, it is not advised for production environments.

System Properties

If you are targeting a production environment, I advice using system properties (Your code source should then be updated to follow the style). Since you are using Tomcat as an Application Server, you have one way that will let you have centric property definition, that is to declare a file named setenv.sh ( .bat for Windows OS ) where you export all the needed System Propeties, eg of content it might have:

export CATALINA_OPTS="$CATALINA_OPTS -DmyProperty=some-value"

There is no more additional configuration needed, and this file will be processed by default when launching the JVM instance if present under $CATLINA_HOME/bin or $CATLINA_BASE/bin .

you are confusing web.xml env entry with system environment with java system properties

ask your ee server admin to add those in server definition such as

-Dpropname=true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM