Servletcontainer.init() not invoked after upgrade karaf from 4.3.7 to 4..4.3

146 Views Asked by At

`We were upgrading from karaf 4.3.7 to 4.4.3

we were facing an issue like servletcontainer.init(webconfig) not invoked after upgraded to 4.4.3 .

our class is below,

`import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
@Component (xmlns = "http://www.osgi.org/xmlns/scr/v1.2.0",
        immediate = true,
        service = { Servlet.class, ProbeRestartService.class },
        property = {
                     HTTP_WHITEBOARD_SERVLET_PATTERN + "=/app/*",
                     HTTP_WHITEBOARD_SERVLET_NAME + "=ApplicationConfiguration API",
                     HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + ServletProperties.JAXRS_APPLICATION_CLASS
                             + "=com.project.example.AppConfApplication"
        })
public class ApplicationConfigurationServlet extends ServletContainer implements ProbeRestartService, ProbeService
{

private boolean isServletinitialised = false;
@Override
    protected void init(WebConfig webConfig) throws ServletException
    {
        LOG.error("NN AppServletconfig init");
        isServletinitialised  = true;
        ------
    }`

isServletinitialised is set to true within init() but init method not invoked hence isServletinitialised is false and After karaf startup, we have a healthchecker which checks this boolean isServletinitialised for false on every 5 mins. if isServletinitialised is false for 5 tries in healthcheck then it restarts the pod in kubernetes.

My question is why servletcontainer.init not getting invoked after karaf 4.4.3 upgrade.

Pls help on this.

1

There are 1 best solutions below

4
Grzegorz Grzybek On

Karaf 4.4.x uses Pax Web 8 which includes complete refactoring of Whiteboard implementation (and entire structure).

One of the changes is related to specification compliance and servlets are not initialized when registered - only lazily initialized on first request.

In servlet specification you can use <load-on-startup> in your web.xml, but OSGi CMPN Whiteboard specification doesn't mention this.

In Pax Web you can use load-on-startup service registration property though. So use:

property = {
    HTTP_WHITEBOARD_SERVLET_PATTERN + "=/app/*",
    HTTP_WHITEBOARD_SERVLET_NAME + "=ApplicationConfiguration API",
    HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + ServletProperties.JAXRS_APPLICATION_CLASS
        + "=com.project.example.AppConfApplication",
    "load-on-startup=1"
})