0

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

  • Started the jetty service

  • I have two applications to deploy in the same jetty instance. I added two war files in jetty home directory. Created two xml files in 'webapps' folder to set the contextpath and war. The applications got launched in the context path.

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>
Abhi
  • 319
  • 1
  • 5
  • 19
  • 1
    If your webapp is expecting to be configured using a system property, then it is coded wrong, by which I mean that it hasn't been coded for multiple installation in a single container. A well-designed webapp will use Servlet Context Initialization Parameters, defined in the `web.xml` file, for any needed configuration values like an `appConfigPath` property. A webapp should be do or rely on anything that is global to the VM, such as system properties. – Andreas Feb 05 '21 at 16:00
  • I am deploying the application created by some one else. This appConfigPath system parameter is to specify service.properties for the application, that need to be a custom one in deployment scenario. – Abhi Feb 05 '21 at 16:04
  • If the webapp can only be told the path using a system property, then you have to load the two webapps in two separate Jetty instances. – Andreas Feb 05 '21 at 16:07
  • Thanks . Can I run two separate jetty instances as a service.? – Abhi Feb 05 '21 at 16:09

1 Answers1

2

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.

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • Is it possible to run two jetty instances as a service from the same server?https://www.eclipse.org/jetty/documentation/jetty-9/index.html#startup-unix-service – Abhi Feb 05 '21 at 18:22
  • @Abhi I think it is, but this is a different question. – Étienne Miret Feb 08 '21 at 09:52