private void checkThatConnectorsHaveStarted() { for (Connector connector : this.tomcat.getService().findConnectors()) { if (LifecycleState.FAILED.equals(connector.getState())) { throw new PortInUseException(connector.getPort()); } } }
@Override public void start() throws EmbeddedServletContainerException { synchronized (this.monitor) { try { if (!this.autoStart) { return; } if (this.undertow == null) { this.undertow = createUndertowServer(); } this.undertow.start(); this.started = true; UndertowEmbeddedServletContainer.logger .info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { if (findBindException(ex) != null) { List<Port> failedPorts = getConfiguredPorts(); List<Port> actualPorts = getActualPorts(); failedPorts.removeAll(actualPorts); if (failedPorts.size() == 1) { throw new PortInUseException( failedPorts.iterator().next().getNumber()); } } throw new EmbeddedServletContainerException( "Unable to start embedded Undertow", ex); } } }
@Override protected FailureAnalysis analyze(Throwable rootFailure, PortInUseException cause) { return new FailureAnalysis( "Embedded servlet container failed to start. Port " + cause.getPort() + " was already in use.", "Identify and stop the process that's listening on port " + cause.getPort() + " or configure this " + "application to listen on another port.", cause); }
public static void main(String[] args) { SpringApplication app = new SpringApplication(ShinyProxyApplication.class); boolean hasExternalConfig = Files.exists(Paths.get("application.yml")); if (!hasExternalConfig) app.setAdditionalProfiles("demo"); try { app.run(args); } catch (Exception e) { // Workaround for bug in UndertowEmbeddedServletContainer.start(): // If undertow.start() fails, started remains false which prevents undertow.stop() from ever being called. // Undertow's (non-daemon) XNIO worker threads will then prevent the JVM from exiting. if (e instanceof PortInUseException) System.exit(-1); } }
@Override public synchronized void start() throws EmbeddedServletContainerException { try { if (!this.autoStart) { return; } if (this.undertow == null) { this.undertow = createUndertowServer(); } this.undertow.start(); this.started = true; UndertowEmbeddedServletContainer.logger .info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { if (findBindException(ex) != null) { List<Port> failedPorts = getConfiguredPorts(); List<Port> actualPorts = getActualPorts(); failedPorts.removeAll(actualPorts); if (failedPorts.size() == 1) { throw new PortInUseException( failedPorts.iterator().next().getNumber()); } } throw new EmbeddedServletContainerException( "Unable to start embedded Undertow", ex); } }
protected static ConfigurableApplicationContext shutdownAndRun(Class<?> clazz, String[] args) { try { return run(clazz, args); } // 二重起動 catch (PortInUseException e) { HttpURLConnection connection = null; try { AppConfig appConfig = AppConfig.getInstance(); String url = "http://localhost:" + appConfig.getManagementPort() + "/shutdown"; connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); int responseCode = connection.getResponseCode(); if (responseCode == 200) { logger.info("Shutdown OK"); // launch again return run(clazz, args); } else { throw new IllegalStateException("Invalid response code : " + responseCode); } } catch (Exception e2) { throw new IllegalStateException("Shutdown failed", e2); } finally { IOUtils.close(connection); } } }
@PostConstruct public void fail() { throw new PortInUseException(8080); }