Abhi
Abhi

Reputation: 330

Setting up different java system properties for two applications deployed in jetty as a service

I am running jetty(9.2.1) as a service for deploying two applications. These are the steps that I followed

But I have two wars to deploy and both these applications need different value for the system property 'appConfigPath'. How can I achieve this?

Solutions tried out

If it was only one application and it is not run as a service, I can run it like this - java -Dappconfig=service.properties -jar start.jar

If there was only one application and it is run as a service, I could specify the system property in start.ini.

Referred this - Jetty - set system property and I tried to add setProperty in the xml files that I created in webapps like below, but it didn't work.

jetty-app1.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/app1</Set>
  <Set name="war">/opt/jetty/app1.war</Set>
  <Call class="java.lang.System" name="setProperty">     
     <Arg>appConfigPath</Arg>     
    <Arg>opt/jetty/service1.properties</Arg>   
  </Call>
</Configure>


jetty-app2.xml

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/app2</Set>
  <Set name="war">/opt/jetty/app2.war</Set>
  <Call class="java.lang.System" name="setProperty">     
     <Arg>appConfigPath</Arg>     
    <Arg>opt/jetty/service2.properties</Arg>   
  </Call>
</Configure>

Upvotes: 0

Views: 1157

Answers (1)

&#201;tienne Miret
&#201;tienne Miret

Reputation: 6650

As Andreas said. This isn't possible with Java.

System properties are a stored in a static field of the java.lang.System class. A static field is instantiated once per loaded class. So you can have different instances of a static field in different class loader. Hawever Java "system classes" (java.lang.* and others) are required to be loaded by the root classloader, hence there can be only one java.lang.System class loaded into a JVM, and hence only one value to a given system property inside a JVM.

Basically, what you tried in XML set twice the same property to two different values, and the last one to execute would override the other.

While your requirements (running two app in the same Jetty instance with different system properties) cannot be met, maybe you can relax them:

  • either use two Jetty instances,
  • either run those apps with the same system properties (but different configuration, using JNDI or context params, as explained in the question you linked).

Again, as explained in the question you linked (and in Andreas comment): using a system property for configuration in a Java web app is a bug. I would suggest reporting it to whoever made that app and ask them to fix it.

Upvotes: 2

Related Questions