2013-08-05 54 views
0

如果我試圖在JettyHelper與HTTPS的嵌入式Jetty中運行Restlet。該的Restlet是先從碼頭,如果我運行jar和偵聽的端口443上,但在第一次請求我從碼頭獲得一個綁定的異常,但一切運作良好:Restlet與Jetty在HTTPS上運行BindException

INFO | jvm 1 | 2013/08/05 07:35:43 | 2013-08-05 07:35:43.720:INFO:oejs.Server:jetty-7.6.5.v20120716 
INFO | jvm 1 | 2013/08/05 07:35:43 | 2013-08-05 07:35:43.813:INFO:oejs.AbstractConnector:Started [email protected]:443 
INFO | jvm 1 | 2013/08/05 07:35:43 | 2013-08-05 07:35:43.814:INFO:oejs.Server:jetty-7.6.5.v20120716 
INFO | jvm 1 | 2013/08/05 07:35:43 | 2013-08-05 07:35:43.820:INFO:oejs.AbstractConnector:Started [email protected]:8182 
INFO | jvm 1 | 2013/08/05 07:36:35 | 2013-08-05 07:36:35.730:INFO:oejs.Server:jetty-7.6.5.v20120716 
INFO | jvm 1 | 2013/08/05 07:36:35 | 2013-08-05 07:36:35.733:WARN:oejuc.AbstractLifeCycle:FAILED [email protected]:443: java.net.BindException: Die Adresse wird bereits verwendet 
INFO | jvm 1 | 2013/08/05 07:36:35 | java.net.BindException: Die Adresse wird bereits verwendet 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.Net.bind0(Native Method) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.Net.bind(Net.java:444) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.Net.bind(Net.java:436) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:182) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:311) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:260) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.ssl.SslSelectChannelConnector.doStart(SslSelectChannelConnector.java:626) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.Server.doStart(Server.java:281) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.ext.jetty.JettyServerHelper.start(JettyServerHelper.java:398) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.Server.start(Server.java:590) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.Restlet.handle(Restlet.java:315) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.Server.handle(Server.java:513) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.engine.ServerHelper.handle(ServerHelper.java:72) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:152) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.ext.jetty.JettyServerHelper$WrappedServer.handle(JettyServerHelper.java:170) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:452) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:884) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:938) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:630) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:191) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:622) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at java.lang.Thread.run(Thread.java:724) 
INFO | jvm 1 | 2013/08/05 07:36:35 | 2013-08-05 07:36:35.735:WARN:oejuc.AbstractLifeCycle:FAILED [email protected]: java.net.BindException: Die Adresse wird bereits verwendet 
INFO | jvm 1 | 2013/08/05 07:36:35 | java.net.BindException: Die Adresse wird bereits verwendet 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.Net.bind0(Native Method) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.Net.bind(Net.java:444) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.Net.bind(Net.java:436) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:182) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:311) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:260) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.ssl.SslSelectChannelConnector.doStart(SslSelectChannelConnector.java:626) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.Server.doStart(Server.java:281) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.ext.jetty.JettyServerHelper.start(JettyServerHelper.java:398) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.Server.start(Server.java:590) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.Restlet.handle(Restlet.java:315) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.Server.handle(Server.java:513) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.engine.ServerHelper.handle(ServerHelper.java:72) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:152) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.restlet.ext.jetty.JettyServerHelper$WrappedServer.handle(JettyServerHelper.java:170) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:452) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:884) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:938) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:630) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:191) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:622) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538) 
INFO | jvm 1 | 2013/08/05 07:36:35 | at java.lang.Thread.run(Thread.java:724) 

爲什麼碼頭努力開始兩次?我怎麼能得到這個例外?

源代碼:

import java.security.KeyStore; 

import org.restlet.Application; 
import org.restlet.Component; 
import org.restlet.Context; 
import org.restlet.Restlet; 
import org.restlet.Server; 
import org.restlet.data.Parameter; 
import org.restlet.data.Protocol; 
import org.restlet.engine.Engine; 
import org.restlet.ext.jetty.HttpServerHelper; 
import org.restlet.ext.jetty.HttpsServerHelper; 
import org.restlet.routing.Router; 
import org.restlet.util.Series; 

public class RestServer extends Application { 


    private RestServer(Context context) { 
     super(context);  
    } 

    public static void main(String[] args) throws Exception { 

     Engine.setLogLevel(java.util.logging.Level.INFO); 

     Component component = new Component(); 

     component.getLogService().setLoggerName("net.asdf.WebComponent.www"); 

     Context context = component.getContext().createChildContext(); 
     component.getDefaultHost().attach(new RestServer(context)); 


     startHttpsServer(context, component, 443).start(); 
     startHttpServer(context, component, 8182).start(); 

    } 

    private static HttpServerHelper startHttpServer(Context context, Component component, Integer port) throws Exception { 
     //create embedding http jetty server 
     Server embedingHttpJettyServer=new Server(
       context, 
       Protocol.HTTP, 
       port, 
       component 
      ); 
     //construct and start JettyServerHelper 
     HttpServerHelper jettyHttpServerHelper=new HttpServerHelper(embedingHttpJettyServer); 
     return jettyHttpServerHelper; 
    } 

    private static HttpsServerHelper startHttpsServer(Context context, Component component, Integer port) throws Exception { 
     //create embedding https jetty server 
     final Server embedingHttpsJettyServer=new Server(
       context, 
       Protocol.HTTPS, 
       port, // 8183, 
       component 
      ); 

     Series<Parameter> parameters = embedingHttpsJettyServer.getContext().getParameters(); 
     // parameters.add("sslContextFactory", "org.restlet.ext.ssl.PkixSslContextFactory"); 
     // parameters.add("sslContextFactory", "org.eclipse.jetty.http.ssl.SslContextFactory"); 
     parameters.add("keystore", "jks/keystore.jks"); 
     parameters.add("keyStorePath", "jks/keystore.jks"); 
     parameters.add("keyStorePassword", "xxxxx"); 
     parameters.add("keyManagerPassword", "xxxx"); 
     parameters.add("keyPassword", "xxxx"); 
     parameters.add("password", "xxx"); 
     parameters.add("keyStoreType", KeyStore.getDefaultType()); 
     parameters.add("tracing", "true"); 
     parameters.add("truststore", "jks/keystore.jks"); 
     parameters.add("trustStorePath", "jks/keystore.jks"); 
     parameters.add("trustStorePassword", "xxxxx"); 
     parameters.add("trustPassword", "xxxxx"); 
     parameters.add("trustStoreType", KeyStore.getDefaultType()); 
     parameters.add("allowRenegotiate", "true"); 
     parameters.add("type", "1"); 
     //construct and start JettyServerHelper 
     HttpsServerHelper jettyHttpsServerHelper = new HttpsServerHelper(embedingHttpsJettyServer); 
     return jettyHttpsServerHelper; 

    } 


    /** 
    * Creates a root Restlet that will receive all incoming calls. 
    */ 
    @Override 
    public Restlet createInboundRoot() { 
     // Create a router Restlet that routes each call to a 
     Context routerContext = getContext().createChildContext(); 
     Router router = new Router(routerContext); 
     router.attachDefault(DefaultResource.class); 
     return router; 
    } 
} 
+0

我剛看到這也正好與HTTP不僅HTTPS – patrick

回答

5

我已經更新了 「主」, 「startHttpServer」 和 「startHttpsServer」 的方法,以使代碼工作:

public static void main(String[] args) throws Exception { 
    Engine.setLogLevel(java.util.logging.Level.INFO); 
    Component component = new Component(); 
    component.getLogService().setLoggerName("net.asdf.WebComponent.www"); 
    component.getDefaultHost().attach(new TestPatrick()); 

    // Attach the server connectors descriptors to the component 
    component.getServers().add(startHttpsServer(component, 443)); 
    component.getServers().add(startHttpServer(component, 8182)); 
    // Start the component, which will lead to start, at the end, the real connectors. 
    component.start(); 
} 

private static Server startHttpServer(Component component, Integer port) throws Exception { 
    return new Server(Protocol.HTTP, port, component); 
} 

private static Server startHttpsServer(Component component, Integer port) throws Exception { 
    // create embedding https jetty server 
    Server server = new Server(new Context(), Protocol.HTTPS, port, component); 

    Series<Parameter> parameters = server.getContext().getParameters(); 
    parameters.add("keystore", "jks/keystore.jks"); 
    parameters.add("keyStorePath", "jks/keystore.jks"); 
    parameters.add("keyStorePassword", "xxx"); 
    parameters.add("keyManagerPassword", "xxx"); 
    parameters.add("keyPassword", "xxx"); 
    parameters.add("password", "xxx"); 
    parameters.add("keyStoreType", KeyStore.getDefaultType()); 
    parameters.add("tracing", "true"); 
    parameters.add("truststore", "jks/keystore.jks"); 
    parameters.add("trustStorePath", "jks/keystore.jks"); 
    parameters.add("trustStorePassword", "xxx"); 
    parameters.add("trustPassword", "xxx"); 
    parameters.add("trustStoreType", KeyStore.getDefaultType()); 
    parameters.add("allowRenegotiate", "true"); 
    parameters.add("type", "1"); 

    return server; 
} 

這裏有一些解釋。 Component是應用程序的容器,它旨在爲這些應用程序提供一些服務。例如,它提供服務器連接器,通過使用多種協議通過客戶端請求來幫助應用程序到達。 Restlet框架提供了幾種服務器連接器的實現;一個是內部的,另一個是基於特定的框架,如Jetty。因爲我們不希望代碼依賴於特定擴展,所以它允許Component使用作爲描述符的Server類聲明其必需的服務器連接器。然後,引擎發現位於類路徑中的實現,並透明地創建真實的服務器助手。

我希望我的解釋清楚。如果不是,請隨時索取更多。

+0

謝謝,我會嘗試一下。但我認爲這是問題。 – patrick

0

你的問題:爲什麼Jetty試圖啓動兩次?我怎麼能得到這個例外?

四年後回答你的問題讓我覺得自己是一個堅果工作,但我們走了。我真的爲其他人回答。

Jetty服務器啓動兩次(不太正確)的原因是Jetty幫助程序檢測到沒有任何服務器正在運行,因此它啓動了一個新實例。助手不知道該實例的原因是因爲你叫:

startHttpServer(context, component, 8182).start(); 

helper.start()方法不設置Restlet.start標誌,並且該標誌是由輔助檢查,當它試圖啓動服務器。所以是的,它是HttpServerHelper中的一個錯誤。

因此而不是調用:

startHttpsServer(context, component, 443).start(); 
startHttpServer(context, component, 8182).start(); 

您應將其更改爲:

Server s1 = startHttpServer(context, component, 8182); // startHttpServer should return Server instead of HttpServerHelper. The method still instantiates HttpServerHelper 
Server s2 = startHttpsServer(context, component, 443); 
component.getServers().add(s1); 
component.getServers().add(s2); 
component.start();