Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Need Help with Embedded Jetty - Adding multiple context through Mutable Handle Collection

Is it right to start a jetty instance with no context specified and no context handler, then keep adding context to it once the server has started. Although I was able to do this using mutable HandlerCollection and the logs says the Server and the Contexts are Started and Available, I am not able to access it with a URL with the context [ Not Found error / 404]. 

Should we add at least one root context and contexthandler to the server while starting it?

My main thread starts the server. Post that it will spawn couple of threads which each creates WebAppContext and start them. 

My jetty version is 9.3.7.v20160115

Bit of the code where I am starting the server and contexts are attached, along with few inline comments

P.S If I start Server and context in one go without any handlercollection, i.e each context in its own server instance and finally just start the server, it's working. I am able to access it with the contextpath for the same jars in the classpath.

Thanks in advance,
Varsha
public void startWebContext() throws ClassNotFoundException
    {
		// Comment : My parent Classloader is loading and starting the server,
		// child URLClassloader has the url for the jar which has to be started in this context.
		// This method is started in a new thread whose contextclass loader is set as below.
		Thread.currentThread().setContextClassLoader( childCL );
	  
		// Comment : This url is an absolute jar path
        URL url = Thread.currentThread().getContextClassLoader().getResource( "webcontent" );
        String resourceBase = lUrl.toString();

		//Comment : this is the context for the jar
        String appCtx = "/someContext";

        WebAppContext appContext = new WebAppContext();
        lAppContext.setAvailable( true );
        lAppContext.setParentLoaderPriority( true );
        lAppContext.setContextPath( appCtx );
        lAppContext.setResourceBase( resourceBase );
        lAppContext.setThrowUnavailableOnStartupException( true );

        try
        {
            lAppContext.setClassLoader( new WebAppClassLoader( Thread.currentThread().getContextClassLoader(),
                                                               lAppContext ) );

        }
        catch( IOException lExcp )
        {
            lExcp.printStackTrace();
        }	
		
        System.out.println( "Adding Context : " + lAppContext + "in to Jetty : " + server );
		
		//Comment : The above line prints as 
		/* 
		
		Adding Context : o.e.j.w.WebAppContext@39ccdf72{/someContext,jar:file:/D:/Project/lib/someJar.jar!/webcontent,UNAVAILABLE}in to Jetty : org.eclipse.jetty.server.Server@366ef90e
		
		*/
		
        HandlerCollection contextHandlerCollection = (HandlerCollection) server.getHandler();
        contextHandlerCollection.addHandler( lAppContext );

		//Comment: server is the same instance created as before
        lAppContext.setServer( server );
		//Comment : getServerLifeCycleListener method is mostly logs.
        lAppContext.addLifeCycleListener( getServerLifeCycleListener( contextHandlerCollection.getHandlers() ) );

        try
        {
            lAppContext.start();
			//Comment : Post this lAppContext's status shows as available. No exceptions at all thrown in AbstractLifeCycle.class
        }
        catch( Exception lExcp )
        {
            lExcp.printStackTrace();
            //  System.out.println( this.getClass().getName() + " :: " + lExcp );
        }
    }
		Server server = new Server( new QueuedThreadPool( 50 ) ) );

        server.setAttribute( "org.eclipse.jetty.server.Request.maxFormContentSize",
                              new Integer(-1 ) );

        HttpConfiguration httpConfig = new HttpConfiguration();
        ServerConnector httpConnector = new ServerConnector( server,
                                                             new HttpConnectionFactory( httpConfig ) );

        String port = 8080;
		httpConnector.setPort( new Integer( port ) );
        httpConnector.setReuseAddress( false );
		
        server.addConnector( httpConnector );

        HandlerCollection lHandlerCollection = new HandlerCollection( true );
        HandlerList lHandlers = new HandlerList();
        lHandlers.addHandler( new MultipartInjectionHandler() );
        lHandlers.addHandler( new ShutdownHandler( server,
                                                       "XXXX" ) );

        Handler lOldHandler = server.getHandler();
        if( lOldHandler != null )
        {
            lHandlers.addHandler( lOldHandler );
        }
       
        lHandlerCollection.addHandler( lHandlers );

        server.setHandler( lHandlerCollection );

        server.addLifeCycleListener( getServerLifeCycleListener( lHandlers.getHandlers() ) );
		
        server.start();
  

Back to the top