如果我尝试使用带有 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;
}
}