Using WebAppProvider in Jetty 8, Jetty deploys a W

2019-08-30 02:55发布

问题:

I'm trying to use Jetty's WebAppProvider to watch a directory for WARs and deploy them. Everything seems to work fine: Jetty reports that it's deploying the WAR to the context I'd expect. But then when I try to visit the page, Jetty can't find anything there.

My server code is

public static void main(String[] args) throws Exception {
    Server httpServer = new Server(3030);
    DeploymentManager deploymentManager = new DeploymentManager();
    httpServer.setDumpBeforeStop(true);

    ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
    contextHandlerCollection.setServer(httpServer);
    deploymentManager.setContexts(contextHandlerCollection);

    WebAppProvider appProvider = new WebAppProvider();
    appProvider.setMonitoredDirName("/tmp/wars/");
    appProvider.setScanInterval(1);
    appProvider.setDeploymentManager(deploymentManager);
    appProvider.setExtractWars(true);

    httpServer.addBean(deploymentManager);
    httpServer.addBean(contextHandlerCollection);
    httpServer.addBean(appProvider);

    httpServer.start();
}

My WAR files deploy fine if I use the jetty-runner jar and deploy them individually, but with my above code I see output like this:

2013-01-16 15:34:30.571:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/sample,file:/private/var/folders/qg/1t5k32g9567cwwl7y6tfdhj40000gn/T/jetty-0.0.0.0-3030-sample.war-_sample-any-/webapp/},/private/tmp/wars/sample.war
2013-01-16 15:34:30.571:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/sample,file:/private/var/folders/qg/1t5k32g9567cwwl7y6tfdhj40000gn/T/jetty-0.0.0.0-3030-sample.war-_sample-any-/webapp/},/private/tmp/wars/sample.war
2013-01-16 15:34:30.571:DBUG:oejuc.AbstractLifeCycle:starting HelloServlet

(there's lots more debug text indicating that my webapp is started and lives at /sample)

When I visit 127.0.0.1:3030/sample (I put the server at port 3030), I get a 404 message, and Jetty logs

2013-01-16 15:34:47.799:DBUG:oejs.Server:REQUEST /sample on AsyncHttpConnection@63ce0072,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=10,c=0},r=1
2013-01-16 15:34:47.799:DBUG:oejs.Server:RESPONSE /sample  200 handled=false

I can't figure out what I'm missing. Does anyone have a working example of this sort of setup?

回答1:

You are using the WebAppProvider, but not attaching it to the DeploymentManager (at least not the right way)

Example usage: http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java?h=jetty-8

Remove:

appProvider.setDeploymentManager(deploymentManager);

Add somewhere after you setup the appProvider:

deploymentManager.addAppProvider(appProvider);

Also you need to add a handler to the server:

HandlerCollection handlers = new HandlerCollection();
RequestLogHandler requestLogHandler = new RequestLogHandler();
handlers.setHandlers(new Handler[] { contextHandlerCollection, new DefaultHandler(), requestLogHandler });
StatisticsHandler stats = new StatisticsHandler();
stats.setHandler(handlers);
httpServer.setHandler(stats);