servletsjettyembedded-jettyjetty-8

How do I dynamically add servlets to a jetty server?


I create a jetty server and want to programmatically and dynamically add servlets.

I setup logging and a console then proceed to create the server as follows:

Connector               httpConn        = null;
Connector               httpsConn       = null;
HandlerCollection       handlers        = new HandlerCollection();
httpConn = new SelectChannelConnector();
httpConn.setPort( httpPort );
httpConn.setName( "HTTPConnector" );  // simillar setup for httpsConn if needed

contextHandlers = new ContextHandlerCollection();
if( launchServlets ) {
    // this is more or less the same servlet creation code as below but for launching
    // static servlets (in this example, there are none)
    holders = initializeServlets( configFilePath );
}
handlers.addHandler( contextHandlers );

server = new Server();
if( httpConn != null ) {
    server.addConnector( httpConn );
}
if( httpsConn != null ) {
    server.addConnector( httpsConn );
}

server.setGracefulShutdown( 1000 ); /
server.setStopAtShutdown( true );
server.setHandler( handlers );
server.start();

if( launchServlets ) {
    // Catch any exception that occurs during servlet init()
    // (must be called after server.start() )
    for( int i = 0; i < holders.length; i++ ) {
        if( holders[ i ] != null ) {
            Exception initEx = holders[ i ].getUnavailableException();
            if( initEx != null ) {
                server.stop();
                throw initEx;
            }
        }
    }
}

So when I need to start a servlet, I do the following:

boolean isSecureOnly = false;  // function that decides suppressed for now
String[] connectors;

MyServlet myServlet = new MyServlet();

ServletHolder holder = new ServletHolder( myServlet );
holder.setInitParameter( Constants.CONF_INIT_STRING_PARAM, configString );
holder.setInitParameter( Constants.CONF_INIT_NAME_PARAM, myName );
ServletContextHandler handler = new ServletContextHandler();
if( details != null ) {
    details.sch = handler;
}
String contextPath = MyConfig.getContextPath( myProps, myName );
handler.setContextPath( contextPath );
handler.setAllowNullPathInfo( true );

// bind the servlet to the connectors it should be listening to
if( isSsl ) {
    if( isSecureOnly ) {
        connectors = new String[ 1 ];
        connectors[0] = Constants.CONF_HTTPS_CONNECTOR;
    } else {
        connectors = new String[ 2 ];
        connectors[0] = Constants.CONF_HTTPS_CONNECTOR;
        connectors[1] = Constants.CONF_HTTP_CONNECTOR;
    }
} else {
    if( isSecureOnly ) {
    throw new ConfigException( 50051 );
    }
    connectors = new String[ 1 ];
    connectors[0] = Constants.CONF_HTTP_CONNECTOR;
}
handler.setConnectorNames( connectors );

if( myName != null ) {
    handler.setDisplayName( MyMessage.message( 10025, myName ) );
} else {
    handler.setDisplayName( MyMessage.message( 10001 ) );
}
handler.addServlet( holder, "/*" );
contextHandlers.addHandler( handler );

contextHandlers.mapContexts();
Exception initEx = holder.getUnavailableException();
if( initEx != null ) {
    // deal with error
}

Although my dynamic servlet launching code appears to work without error, the console does not show the servlet initializing. When I have launchServlets=true, the code will statically launch the servlets and all is well but I want to launch them dynamically.

Do I have to manually make the servlet initialize (I thought servlets were isolated into their own memory space)? How do I tell Jetty to start the new Servlet?

Update: So far the only way I have managed to get the servlet to start is by server.stop()/server.join()/server.start(). I would like adding servlets (and removing - but that's a bonus question) not to interrupt service to existing servlets)?

I'm using jetty-all-server-8.1.3 and servlet api 3.0

Thanks in advance for your help/hints!


Solution

  • I'm not sure if it is the safe way to do it but I have gotten the serlvlet to start by calling start() on the ServletContextHandler() instead of stopping and starting the server.

    This worked for both 8.1.3 and 9.2.4 (which I upgraded to along with upgrading to 3.1 of the servlet api)

    Hopefully this will help someone else who is interested in doing something similar.