org.springframework.boot.web.embedded.tomcat.TomcatWebServer Java Examples

The following examples show how to use org.springframework.boot.web.embedded.tomcat.TomcatWebServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    servletWrapper.setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #2
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #3
Source File: ExploreSpring5URLPatternUsingRouterFunctions.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    servletWrapper.setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #4
Source File: SpringBootWebTwoConnectorsApplicationTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
    Service service = ((TomcatWebServer) event.getWebServer()).getTomcat().getService();
    for (Connector connector : service.findConnectors()) {
        if (connector.getSecure()) {
            this.httpsPort = connector.getLocalPort();
        } else {
            this.httpPort = connector.getLocalPort();
        }
    }
}
 
Example #5
Source File: ExternalizedConfigurationWebApplicationBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public TomcatServletWebServerFactory servletWebServerFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            // 激活 JNDI(默认失效)
            tomcat.enableNaming();
            return super.getTomcatWebServer(tomcat);
        }
    };
}
 
Example #6
Source File: HelloConfiguration.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts a Tomcat {@link Connector} from Spring webapp context.
 */
public static Connector getConnector(ServletWebServerApplicationContext applicationContext) {
    final TomcatWebServer container = (TomcatWebServer) applicationContext.getWebServer();

    // Start the container to make sure all connectors are available.
    container.start();
    return container.getTomcat().getConnector();
}
 
Example #7
Source File: SpringTomcatApplication.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Bean to configure Armeria Tomcat service.
 * @return configuration bean.
 */
@Bean
public ArmeriaServerConfigurator armeriaTomcat() {
    final WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
    if (webServer instanceof TomcatWebServer) {
        final Tomcat tomcat = ((TomcatWebServer) webServer).getTomcat();

        return serverBuilder -> serverBuilder.service("prefix:/tomcat/api/rest/v1",
                                                      TomcatService.of(tomcat));
    }
    return serverBuilder -> { };
}
 
Example #8
Source File: SpringTomcatApplicationItTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySingleConnector() {
    // Relevant to Tomcat 9.0
    assertThat(applicationContext).isInstanceOf(WebServerApplicationContext.class);
    final WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
    assertThat(webServer).isInstanceOf(TomcatWebServer.class);
    assertThat(((TomcatWebServer) webServer).getTomcat()
                                            .getEngine()
                                            .getService()
                                            .findConnectors()).hasSize(1);
}
 
Example #9
Source File: EmbeddedTomcatCustomizer.java    From pulsar-manager with Apache License 2.0 4 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    log.info("Starting servletContainer");
    return new TomcatServletWebServerFactory() {
        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            try {
                log.info("Catalina base is " + tomcat.getServer().getCatalinaBase().getAbsolutePath());
                File lib = new File("lib").getAbsoluteFile();
                if (lib.isDirectory()) {
                    File bkvmWar = searchWar(lib, "bkvm", ".war");
                    if (bkvmWar != null) {
                        File configFile = new File("bkvm.conf");
                        log.info("looking for BKVM configuration file at " + configFile.getAbsolutePath());
                        if (configFile.isFile()) {
                            Properties props = new Properties();
                            try (FileReader reader = new FileReader(configFile)) {
                                props.load(reader);
                            }
                            boolean bkvmEnabled = Boolean.parseBoolean(props.getProperty("bkvm.enabled", "false"));
                            log.info("Read bkvm.enabled = {}", bkvmEnabled);
                            if (bkvmEnabled) {
                                System.setProperty("bookkeeper.visual.manager.config.path", configFile.getAbsolutePath());
                                File file = new File(tomcat.getServer().getCatalinaBase(), "/webapps");
                                log.info("Tomcat Webapps directory is " + file.getAbsolutePath());
                                file.mkdirs();
                                File bkvmDirectory = new File(file, "bkvm");
                                log.info("Deploying BKVM to " + bkvmDirectory.getAbsolutePath());
                                unZip(bkvmWar, bkvmDirectory);
                                Context context = tomcat.addWebapp("/bkvm", bkvmDirectory.getAbsolutePath());
                                WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
                                context.setLoader(loader);
                            }
                        }
                    }
                }
                return super.getTomcatWebServer(tomcat);
            } catch (IOException | ServletException ex) {
                throw new RuntimeException(ex);
            }
        }

    };
}