Restlet在HTTPS BindException上运行Jetlet

时间:2013-08-05 07:47:24

标签: java exception jetty restlet

如果我尝试使用带有HTTPS的JettyHelper在嵌入式Jetty中运行Restlet。 Restlet是从Jetty开始的,如果我运行jar并在端口443上侦听。但是在第一个请求我从Jetty获得绑定异常但是一切都运行良好:

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 SslSelectChannelConnector@0.0.0.0: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 SelectChannelConnector@0.0.0.0: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 SslSelectChannelConnector@0.0.0.0: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 org.restlet.ext.jetty.JettyServerHelper$WrappedServer@60ce274e: 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)

为什么Jetty试图开始两次?我怎么能得到这个例外呢?

源代码:

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

2 个答案:

答案 0 :(得分:5)

我已经更新了“main”,“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。因为我们不希望您的代码依赖于特定的扩展,所以它允许组件使用Server类声明其所需的服务器连接器,该类充当描述符。然后,引擎会发现位于类路径中的实现,并透明地创建真正的服务器帮助程序。

我希望我的解释清楚。如果没有,请随时提出要求。

答案 1 :(得分: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();