use of io.micronaut.http.server.exceptions.ServerStartupException in project micronaut-gcp by micronaut-projects.
the class InvokerHttpServer method start.
@Override
public EmbeddedServer start() {
if (running.compareAndSet(false, true)) {
int retryCount = 0;
while (retryCount <= 3) {
try {
this.server = new Server(port);
ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
server.setHandler(NotFoundHandler.forServlet(servletContextHandler));
HttpFunction httpFunction = new HttpFunction() {
@Override
protected ApplicationContext buildApplicationContext(@Nullable Object context) {
ApplicationContext ctx = InvokerHttpServer.this.getApplicationContext();
this.applicationContext = ctx;
return ctx;
}
};
HttpServlet servlet = new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
try {
httpFunction.service(new HttpRequestImpl(req), new HttpResponseImpl(resp));
} catch (Exception e) {
throw new ServletException(e);
}
}
};
ServletHolder servletHolder = new ServletHolder(servlet);
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(""));
servletContextHandler.addServlet(servletHolder, "/*");
server.start();
logServerInfo();
break;
} catch (BindException e) {
if (randomPort) {
this.port = SocketUtils.findAvailableTcpPort();
retryCount++;
} else {
throw new ServerStartupException(e.getMessage(), e);
}
} catch (Exception e) {
throw new ServerStartupException("Error starting Google Cloud Function server: " + e.getMessage(), e);
}
}
}
return this;
}
use of io.micronaut.http.server.exceptions.ServerStartupException in project micronaut-test by micronaut-projects.
the class TestExecutableEmbeddedServer method start.
@Override
public EmbeddedServer start() {
if (process == null) {
PropertySource testPropertySource = environment.getPropertySources().stream().filter(ps -> ps.getName().equals(AbstractMicronautExtension.TEST_PROPERTY_SOURCE)).findFirst().orElse(null);
CompletableFuture<Process> processFuture = new CompletableFuture<>();
Integer p = httpServerConfiguration.getPort().orElse(null);
int port;
if (p == null) {
if (environment.getActiveNames().contains(Environment.TEST)) {
port = findAvailableTcpPort();
} else {
port = 8080;
}
} else {
if (p == -1) {
port = findAvailableTcpPort();
} else {
port = p;
}
}
new Thread(() -> {
ProcessBuilder processBuilder = new ProcessBuilder();
List<String> commandArgs = new ArrayList<>(Arrays.asList("-Dmicronaut.environments=test", "-Dmicronaut.server.host=localhost", "-Dmicronaut.server.port=" + port));
if (executable.endsWith(".jar")) {
commandArgs.addAll(0, Arrays.asList("java", "-jar", executable));
} else {
commandArgs.add(0, executable);
}
if (testPropertySource != null) {
for (String prop : testPropertySource) {
commandArgs.add("-D" + prop + "=" + testPropertySource.get(prop));
}
}
processBuilder.command(commandArgs);
processBuilder.inheritIO();
try {
Process start = processBuilder.start();
processFuture.complete(start);
} catch (IOException e) {
processFuture.completeExceptionally(new RuntimeException("Error starting native image server: " + e.getMessage(), e));
}
}).start();
try {
this.process = processFuture.get();
int max = 10000;
int timeout = 0;
while (timeout < max) {
try {
URLConnection urlConnection = new URL("http://localhost:" + port).openConnection();
urlConnection.setConnectTimeout(max);
urlConnection.setReadTimeout(max);
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
in.readLine();
in.close();
} catch (IOException e) {
if (!(e instanceof FileNotFoundException)) {
timeout += 100;
if (timeout < max) {
Thread.sleep(100);
} else {
throw new ServerStartupException("Timeout occurred starting Micronaut process server");
}
} else {
// response from server
break;
}
}
}
this.port = port;
} catch (InterruptedException | ExecutionException e) {
throw new ServerStartupException(e.getMessage(), e);
}
}
return this;
}
Aggregations