Re: [jetty-users] Run multiple instances of the same WAR servlet and pass it a config value
In your /var/www/jetty-base-de/webapps/de.xml, you can set a init-parameter that can be accessed from the ServletContext.getInitParameter() Example: http://www.eclipse.org/jetty/configure_10_0.dtd;> /de /var/www/words-5.0.war mycontext.language de Then you can access it ... @Override public void init() throws ServletException { String language = getServletContext().getInitParameter("mycontext.language"); } Joakim Erdfelt / joa...@webtide.com On Wed, Oct 4, 2023 at 3:00 AM Alexander Farber via jetty-users < jetty-users@eclipse.org> wrote: > Good morning, > > I have an "overengineered" setup with Jetty 10.0.16 and haproxy 2.7.8, > where I currently run 6 different Jetty instances to serve the same servlet > - > > I have programmed that WAR servlet using Maven and it supports 6 languages: > > src/main/resources/strings.properties > src/main/resources/strings_de.properties > src/main/resources/strings_fr.properties > src/main/resources/strings_nl.properties > src/main/resources/strings_pl.properties > src/main/resources/strings_ru.properties > > and then in the configure method I fetch the language value from the > "COUNTRY" env var: > > @Override > public void configure(JettyWebSocketServletFactory factory) { > mLanguage = System.getenv("COUNTRY"); > mBundle = ResourceBundle.getBundle("strings", > LOCALES.get(mLanguage)); > > factory.setIdleTimeout(Duration.ofSeconds(IDLE_TIMEOUT_SECONDS)); > factory.setMaxBinaryMessageSize(0); > factory.setMaxTextMessageSize(64 * 1024); > factory.setCreator(new WordsCreator(this, > mBundle.getString(STR_DATABASE_URL))); > } > > Then I have the following 6 scripts on my Rocky Linux 8.8 to start the > Jetty instances > > /etc/systemd/system/jetty-de.service > /etc/systemd/system/jetty-en.service > /etc/systemd/system/jetty-fr.service > /etc/systemd/system/jetty-nl.service > /etc/systemd/system/jetty-pl.service > /etc/systemd/system/jetty-ru.service > > And the only differences the scripts have is the port and the "COUNTRY" > env var: > > [Unit] > Description=Jetty > After=network-online.target > > [Service] > Environment=COUNTRY=de > > Type=simple > User=jetty > Group=jetty > ExecStart=/usr/bin/java -Djdbc.drivers=org.postgresql.Driver -jar > /usr/share/java/jetty-home-10.0.16/start.jar > jetty.home=/usr/share/java/jetty-home-10.0.16 > jetty.base=/var/www/jetty-base-de jetty.http.host=127.0.0.1 > jetty.http.port=8081 > SuccessExitStatus=143 > Restart=always > RestartSec=180 > PrivateTmp=true > > [Install] > WantedBy=multi-user.target > > In the /etc/haproxy/haproxy.cfg I look at the request path and forward the > request to one of the ports: > > backend jetty_de > server domain 127.0.0.1:8081 send-proxy > backend jetty_en > server domain 127.0.0.1:8082 send-proxy > backend jetty_fr > server domain 127.0.0.1:8083 send-proxy > backend jetty_nl > server domain 127.0.0.1:8084 send-proxy > backend jetty_pl > server domain 127.0.0.1:8085 send-proxy > backend jetty_ru > server domain 127.0.0.1:8080 send-proxy > > frontend wordsbyfarber_com > bind 95.216.113.90:80 > bind 95.216.113.90:443 ssl crt > /etc/pki/tls/certs/wordsbyfarber.com.pem no-sslv3 > > redirect scheme https if !{ ssl_fc } > > use_backend jetty_de if { path_beg /de } > use_backend jetty_en if { path_beg /en } > use_backend jetty_fr if { path_beg /fr } > use_backend jetty_nl if { path_beg /nl } > use_backend jetty_pl if { path_beg /pl } > use_backend jetty_ru if { path_beg /ru } > > default_backend jetty_en > > Finaly my config XML files look like this to "bind" the servlet to a > certain context path: > > # cat /var/www/jetty-base-de/webapps/de.xml > > "http://www.eclipse.org/jetty/configure_9_0.dtd;> > > > /de > /var/www/words-5.0.war > > > > Thank you for reading my mail, it is a bit longer, because I am trying to > provide enough details. > > My question is, if it is possible to handle my task with a single Jetty > instance? > > Could I set the env var in the /var/www/jetty-base-de/webapps/de.xml file? > > Or maybe alternatively access the value of "contextPath" from the > configure() method? > > Thank you for any suggestions > Alex > > > ___ > jetty-users mailing list > jetty-users@eclipse.org > To unsubscribe from this list, visit > https://www.eclipse.org/mailman/listinfo/jetty-users > ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] Run multiple instances of the same WAR servlet and pass it a config value
You could call the method setInitParameter(String,String) via the context xml that defines the webapp, then you can retrieve it in the init() of your servlet via the ServletConfig.getInitParameter(String). Jan On Thu, 5 Oct 2023 at 06:42, Alexander Farber via jetty-users < jetty-users@eclipse.org> wrote: > Apologies, the getServletConfig().getContextPath() in the init() of my > servlet works well, that probably had been a problem in my VS Code. > > Still I wonder, if I would set a "property" with a "name" and "value" as > described in the "Jetty XML Syntax" section > https://eclipse.dev/jetty/documentation/jetty-10/operations-guide/index.html#og-xml > - how could I retrieve the value in the servlet? > > Best regards > Alex > > > ___ > jetty-users mailing list > jetty-users@eclipse.org > To unsubscribe from this list, visit > https://www.eclipse.org/mailman/listinfo/jetty-users > -- Jan Bartel www.webtide.com *Expert assistance from the creators of Jetty and CometD* ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] Run multiple instances of the same WAR servlet and pass it a config value
Apologies, the getServletConfig().getContextPath() in the init() of my servlet works well, that probably had been a problem in my VS Code. Still I wonder, if I would set a "property" with a "name" and "value" as described in the "Jetty XML Syntax" section https://eclipse.dev/jetty/documentation/jetty-10/operations-guide/index.html#og-xml - how could I retrieve the value in the servlet? Best regards Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] Run multiple instances of the same WAR servlet and pass it a config value
I try to use the following but the method is not available: @Override public void init() throws ServletException { super.init(); mLanguage = getServletConfig().getContextPath(); } ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
[jetty-users] Run multiple instances of the same WAR servlet and pass it a config value
Good morning, I have an "overengineered" setup with Jetty 10.0.16 and haproxy 2.7.8, where I currently run 6 different Jetty instances to serve the same servlet - I have programmed that WAR servlet using Maven and it supports 6 languages: src/main/resources/strings.properties src/main/resources/strings_de.properties src/main/resources/strings_fr.properties src/main/resources/strings_nl.properties src/main/resources/strings_pl.properties src/main/resources/strings_ru.properties and then in the configure method I fetch the language value from the "COUNTRY" env var: @Override public void configure(JettyWebSocketServletFactory factory) { mLanguage = System.getenv("COUNTRY"); mBundle = ResourceBundle.getBundle("strings", LOCALES.get(mLanguage)); factory.setIdleTimeout(Duration.ofSeconds(IDLE_TIMEOUT_SECONDS)); factory.setMaxBinaryMessageSize(0); factory.setMaxTextMessageSize(64 * 1024); factory.setCreator(new WordsCreator(this, mBundle.getString(STR_DATABASE_URL))); } Then I have the following 6 scripts on my Rocky Linux 8.8 to start the Jetty instances /etc/systemd/system/jetty-de.service /etc/systemd/system/jetty-en.service /etc/systemd/system/jetty-fr.service /etc/systemd/system/jetty-nl.service /etc/systemd/system/jetty-pl.service /etc/systemd/system/jetty-ru.service And the only differences the scripts have is the port and the "COUNTRY" env var: [Unit] Description=Jetty After=network-online.target [Service] Environment=COUNTRY=de Type=simple User=jetty Group=jetty ExecStart=/usr/bin/java -Djdbc.drivers=org.postgresql.Driver -jar /usr/share/java/jetty-home-10.0.16/start.jar jetty.home=/usr/share/java/jetty-home-10.0.16 jetty.base=/var/www/jetty-base-de jetty.http.host=127.0.0.1 jetty.http.port=8081 SuccessExitStatus=143 Restart=always RestartSec=180 PrivateTmp=true [Install] WantedBy=multi-user.target In the /etc/haproxy/haproxy.cfg I look at the request path and forward the request to one of the ports: backend jetty_de server domain 127.0.0.1:8081 send-proxy backend jetty_en server domain 127.0.0.1:8082 send-proxy backend jetty_fr server domain 127.0.0.1:8083 send-proxy backend jetty_nl server domain 127.0.0.1:8084 send-proxy backend jetty_pl server domain 127.0.0.1:8085 send-proxy backend jetty_ru server domain 127.0.0.1:8080 send-proxy frontend wordsbyfarber_com bind 95.216.113.90:80 bind 95.216.113.90:443 ssl crt /etc/pki/tls/certs/wordsbyfarber.com.pem no-sslv3 redirect scheme https if !{ ssl_fc } use_backend jetty_de if { path_beg /de } use_backend jetty_en if { path_beg /en } use_backend jetty_fr if { path_beg /fr } use_backend jetty_nl if { path_beg /nl } use_backend jetty_pl if { path_beg /pl } use_backend jetty_ru if { path_beg /ru } default_backend jetty_en Finaly my config XML files look like this to "bind" the servlet to a certain context path: # cat /var/www/jetty-base-de/webapps/de.xml http://www.eclipse.org/jetty/configure_9_0.dtd;> /de /var/www/words-5.0.war Thank you for reading my mail, it is a bit longer, because I am trying to provide enough details. My question is, if it is possible to handle my task with a single Jetty instance? Could I set the env var in the /var/www/jetty-base-de/webapps/de.xml file? Or maybe alternatively access the value of "contextPath" from the configure() method? Thank you for any suggestions Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users