2016-04-05 11 views
1

コンテキストが指定されておらず、コンテキストハンドラを持たないjettyインスタンスを起動し、サーバが起動したらコンテキストを追加してください。変更可能なHandlerCollectionを使用してこれを行うことができましたが、ログにはサーバーとコンテキストが開始され、使用可能であると表示されますが、URLでアクセスできません。または、起動時に少なくとも1つのルートコンテキストとcontexthandlerをサーバーに追加する必要がありますか?埋め込みJetty - Jettyサーバの起動後にコンテキストを追加する

以下のリンクの例に似たようなことをしました。 Jetty 9 (embedded): Adding handlers during runtime

マイ桟橋のバージョンが実行しているサーバーへのハンドラの添加が一般的なパターンである9.3.7.v20160115

答えて

3

ですが、ドキュメントは(すべてでは明らかではないが、「埋め込む桟橋」チュートリアルのすべての例サーバー設定後に開始)の人々私の知る限り、これらのアプローチに従っている:。

1)ハンドラ

2を追加/削除するHandlerCollection(ブールmutableWhenRunning)ctorのを使って)手を加えると起動します明示的に

私は、Jetty 9.1.4では#2は必要ないが、Jetty 9.2.14以降であることを確認しました(これらのバージョン番号は、この問題とは全く関係のないジャージー依存関係としてMavenによって選ばれました)。 )例えば:これは(9.2.14に、それは404を報告する)9.1.4の "動作するように使用される" という符号であるSOAPコンテキストを追加するために

// 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); 
     } 

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; 
    } 
関連する問題