Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
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:

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

        <Set name="contextPath">/de</Set>
        <Set name="war">/var/www/words-5.0.war</Set>
        <Call name="setInitParameter">
            <Arg>mycontext.language</Arg>
            <Arg>de</Arg>
        </Call>
    </Configure>


Then you can access it ...

    @Override
    public void init() throws ServletException {
        String language = getServletContext().getInitParameter("mycontext.language");
    }


Joakim Erdfelt / joakim@xxxxxxxxxxx


On Wed, Oct 4, 2023 at 3:00 AM Alexander Farber via jetty-users <jetty-users@xxxxxxxxxxx> 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
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">

        <Set name="contextPath">/de</Set>
        <Set name="war">/var/www/words-5.0.war</Set>

    </Configure>

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@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top