Search in sources :

Example 1 with ServerStartupException

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;
}
Also used : Server(org.eclipse.jetty.server.Server) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) HttpServlet(javax.servlet.http.HttpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) HttpServerException(io.micronaut.http.server.exceptions.HttpServerException) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ApplicationContext(io.micronaut.context.ApplicationContext) MultipartConfigElement(javax.servlet.MultipartConfigElement) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) HttpFunction(io.micronaut.gcp.function.http.HttpFunction) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Nullable(io.micronaut.core.annotation.Nullable)

Example 2 with ServerStartupException

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;
}
Also used : URLConnection(java.net.URLConnection) URL(java.net.URL) PropertySource(io.micronaut.context.env.PropertySource) CompletableFuture(java.util.concurrent.CompletableFuture) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ServerStartupException (io.micronaut.http.server.exceptions.ServerStartupException)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 PropertySource (io.micronaut.context.env.PropertySource)1 Nullable (io.micronaut.core.annotation.Nullable)1 HttpFunction (io.micronaut.gcp.function.http.HttpFunction)1 HttpServerException (io.micronaut.http.server.exceptions.HttpServerException)1 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)1 IOException (java.io.IOException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Server (org.eclipse.jetty.server.Server)1