0

如果我尝试使用带有 HTTPS 的 JettyHelper 在嵌入式 Jetty 中运行 Restlet。如果我运行 jar 并在端口 443 上侦听,则 Restlet 从 Jetty 开始。但是在第一个请求中,我从 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;
    }
}
4

2 回答 2

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

这里有一些解释。组件是应用程序的容器,它旨在为这些应用程序提供一些服务。例如,它提供服务器连接器,帮助使用多种协议通过客户端请求访问应用程序。Restlet 框架提供了几种服务器连接器的实现;一个是内部的,其他的基于特定的框架,例如 Jetty。由于我们不希望您的代码依赖于特定的扩展,因此它的理念是让组件使用充当描述符的 Server 类声明其所需的服务器连接器。然后,引擎发现位于类路径中的实现,并透明地创建真正的服务器助手。

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

于 2013-08-14T17:33:33.950 回答
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();
于 2017-11-07T21:13:47.603 回答