Skip to content

Question: how to change the configuration of ResourceConfig at runtime? #5156

@SkyCrawl

Description

@SkyCrawl

What I would like is to be able to change the configuration of my singleton ResourceConfig upon external command. Right now, the configuration is locked after initialization and once that happens, it can not be changed. My ResourceConfig class is as follows:

@Singleton
public class MyApplication extends ResourceConfig {
    public RestApplication() {
        ...; // init (configuration is not locked and can be changed)
    }
    
    public void reload() {
        // called from a resource class
        ...; // reload (configuration is locked and can not be changed)
    }
}

And my web.xml is as follows:

<servlet>
    <servlet-name>REST API</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>cz.example.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>REST API</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

When trying to solve this, I stumbled upon the org.glassfish.jersey.servlet.ServletContainer.reload() method, but couldn't figure out how to obtain the class's instance (injection doesn't work). If I understand correctly, this method will create and initialize a new MyApplication object, and discard the previous one. I think that would achieve what I'm after.

To clarify a little more, I would like to be able to turn monitoring and statistics counting on and off as needed, but perhaps it could be useful for request tracing and other things as well. Originally, my ResourceConfig was not a Singleton and a new instance was created for each request (at least, that was the impression I got), which might also achieve what I'm after, but personally, I consider that needlessly inefficient and wasn't sure about the effects of turning monitoring and statistics on and off (and the correctness of data).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions