Search code examples
javajax-rsjersey-2.0

Jersey 2 ApplicationEventListener onEvent() not called


The ApplicationEventListener#onEvent is never called. What could be wrong here.The resource config class is used and i am able to call APIs.

<servlet>
    <servlet-name>MyApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.MyApplication</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>MyApplication</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

App configuration:

public class MyApplication extends ResourceConfig{
    @NameBinding
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Secured {}

    public MyApplication(){

        register(com.UserLogin.class);

        register(com.MyApplicationEventListener.class);
    }
}

Solution

  • Unless you make a request, Jersey will not be fully started, so the listener will never be called by just starting and stopping the server. You need to set <load-on-startup>1</load-on-startup> in the web.xml for the servlet configuration. This will cause Jersey to fully load when the server starts.