I'm reading the following documentation regarding the deployment architecture of embedded Jetty, and I tried to configure a very simple jetty server that loads initially without any WebApps or handlers deployed. The server should hot deploy WARs which are copied to a static directory.

My Code:

public class JettyServerDeploymentManager implements JettyServer {

    private Server server;

    private DeploymentManager deployer;

    private AppProvider appProvider;

    public void start() throws Exception {
        server  = new Server(8090);

        // Setup Connector
        ServerConnector connector = new ServerConnector(server);
        server.setConnectors(new Connector[] {connector});

        // Handler Tree location for all webapps
        ContextHandlerCollection contexts = new ContextHandlerCollection();


        // Deployment Management
        deployer = new DeploymentManager();
        deployer.setContexts(contexts);
        appProvider = new WebAppProvider();
        ((WebAppProvider) appProvider).setMonitoredDirName("/mytmpdir");
        deployer.addAppProvider(appProvider);
        server.addBean(deployer);


        // Handler Tree
        HandlerCollection handlers = new HandlerCollection();
        handlers.addHandler(contexts);
        handlers.addHandler(new DefaultHandler());
        server.setHandler(handlers);


        server.start();
        server.join();
    }

}

This impl should be equivalent to the code provided in the link above (xml configuration).

The server inits without any errors, but when I copy a war file to the scanned directory, nothing happens.

What am I missing? I'm not even sure if the link between my server to the deployment manager is sufficient.

Update:

It seems to be working, at least the scanning part, I've added

static
{
    // Make jetty's own logging use java.util.logging
    org.eclipse.jetty.util.log.Log.setLog(new JavaUtilLog());
}

And now I see in the logs that the appProvider loads a new war (it fails on NullPtr now)

Apr 08, 2019 2:51:43 PM org.eclipse.jetty.webapp.WebAppContext doStart
WARNING: Failed startup of context [email protected]{/simple1,jar:file:///mytmpdir/simple1.war!/,null}{/mytmpdir/simple1.war}
java.lang.NullPointerException  
 at org.eclipse.jetty.webapp.MetaInfConfiguration.preConfigure(MetaInfConfiguration.java:77)
 at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:501)
 at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:539)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
 at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:41)
 at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:188)
 at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:499)
 at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:147)
 at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
 at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:452)
 at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
 at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610)
 at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529)
 at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392)
 at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
 at java.util.TimerThread.mainLoop(Timer.java:555)
 at java.util.TimerThread.run(Timer.java:505)

1 Answers

0
LiranBo On

Working solution:

public class JettyServerDeploymentManager implements JettyServer {

    private static final Logger log = Logger.getLogger(JettyServerDeploymentManager.class.getName());

    private Server server;

    private DeploymentManager deployer;

    private AppProvider appProvider;

    static
    {
        // Make jetty's own logging use java.util.logging
        org.eclipse.jetty.util.log.Log.setLog(new JavaUtilLog());
    }

    public void start() throws Exception {
        server  = new Server(8090);

        // Setup Connector
        ServerConnector connector = new ServerConnector(server);
        server.setConnectors(new Connector[] {connector});

        // Handler Tree location for all webapps
        ContextHandlerCollection contexts = new ContextHandlerCollection();

        // Handler Tree
        HandlerCollection handlers = new HandlerCollection();
        handlers.addHandler(contexts);
        handlers.addHandler(new DefaultHandler());
        server.setHandler(handlers);

        // Deployment Manager
        DeploymentManager deploymentManager = new DeploymentManager();
        deploymentManager.setContexts(contexts);

        // App provider
        WebAppProvider appProvider = new WebAppProvider();
        appProvider.setExtractWars(true);         // required for a proper deployment of inner jars
        appProvider.setMonitoredDirName("/mytmpdir");
        appProvider.setScanInterval(2);


        deploymentManager.addAppProvider(appProvider);

        server.addBean(deploymentManager);

        log.info("starting server");
        server.start();
        server.join();
    }


    private static URL findWebAppPath() {
        ClassLoader classLoader = SparkApp.class.getClassLoader();
        return classLoader.getResource("");
    }