2016-04-05 183 views
1

是否啓動沒有指定上下文且沒有上下文處理程序的jetty實例,然後在服務器啓動後向其添加上下文。儘管我可以使用可變的HandlerCollection來完成此操作,並且日誌顯示服務器和上下文已啓動且可用,但我無法通過URL訪問它。或者我們應該在啓動時至少向服務器添加一個根上下文和上下文處理程序?嵌入式Jetty - 啓動Jetty服務器後添加上下文

我做了類似於下面的鏈接建議的例子。 Jetty 9 (embedded): Adding handlers during runtime

我的碼頭的版本是9.3.7.v20160115

回答

3

加處理程序來運行服務器是一個常見的模式,但該文件並不清楚在所有(在「嵌入碼頭」教程的所有實例啓動服務器後的配置)AFAIK人的是以下這些方法:使用HandlerCollection(布爾mutableWhenRunning)構造函數添加

1)/移除處理程序

2)添加並啓動手明確指出

我觀察到#2在Jetty 9.1.4中並不需要,但是它在Jetty 9.2.14之後(隨後這些版本號被Maven選爲澤西依賴關係,這與這個問題完全無關。 )例如:

// after server creation ... 
    ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection(); 
      jettyServer.setHandler(contextHandlerCollection); 
    jettyServer.start(); 
    // ... 
    ServletContextHandler newSCH= new ServletContextHandler(ServletContextHandler.SESSIONS); 
     newSCH.setResourceBase(System.getProperty("java.io.tmpdir")); 
     newSCH.setContextPath("/servlets"); 
     ServletHolder newHolder = new SwServletHolder(servlet); 
     newSCH.addServlet(newHolder, "/*"); 
     contextHandlerCollection.addHandler(newSCH); 
     try { 
       newSCH.start(); // needed from about 9.2 
     } catch (Exception e) { 
       logger.info("Exception starting ServletContextHandler for Jetty", e); 
     } 

爲了增加這樣的SOAP上下文是 「用於工作」 上9.1.4(上9.2.14代碼它報告404):

import java.lang.reflect.Method; 
import java.net.InetSocketAddress; 

import javax.jws.WebService; 
import javax.xml.ws.Endpoint; 

import org.eclipse.jetty.http.spi.JettyHttpServerProvider; 
import org.eclipse.jetty.http.spi.HttpSpiContextHandler; 
import org.eclipse.jetty.http.spi.JettyHttpContext; 
import org.eclipse.jetty.http.spi.JettyHttpServer; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.server.handler.ContextHandlerCollection; 

import com.sun.net.httpserver.HttpContext; 

public class JettyJaxWs { 

    public static void main(String[] args) throws Exception { 
     Server server = new Server(7777); 
     ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection(); 
     server.setHandler(contextHandlerCollection); 
     server.start(); 
     HttpContext context = buildOrig(server, "/ws"); 
     MyWebService ws = new MyWebService(); 
     Endpoint endpoint = Endpoint.create(ws); 
     endpoint.publish(context); 
     // access wsdl on http://localhost:7777/ws/MyWebService?wsdl 
    } 

    @WebService 
    public static class MyWebService { 

     public String hello(String s) { 
      return "hi " + s; 
     } 
    } 

    public static HttpContext buildOrig(Server server, String contextString) throws Exception { 
     JettyHttpServerProvider.setServer(server); 
     return new JettyHttpServerProvider().createHttpServer(new InetSocketAddress(7777), 5).createContext(contextString); 
    } 

後來,我必須爲最後一種方法做這件事(不確定是否有更好的方法):

public static HttpContext buildNew(Server server, String contextString) { 
     JettyHttpServer jettyHttpServer = new JettyHttpServer(server, true); 
     JettyHttpContext ctx = (JettyHttpContext) jettyHttpServer.createContext(contextString); 
     try { 
      Method method = JettyHttpContext.class.getDeclaredMethod("getJettyContextHandler"); 
      method.setAccessible(true); 
      HttpSpiContextHandler contextHandler = (HttpSpiContextHandler) method.invoke(ctx); 
      contextHandler.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return ctx; 
    }