标签: embedded-tomcat-7

Spring Boot - 如何获取正在运行的端口

我有一个spring boot应用程序(使用嵌入式tomcat 7),我已经设置server.port = 0了我的application.properties所以我可以有一个随机端口.服务器启动并在端口上运行后,我需要能够获得所选的端口.

我不能使用,@Value("$server.port")因为它是零.这是一个看似简单的信息,为什么我不能从我的java代码中访问它?我该如何访问它?

port spring embedded-tomcat-7 spring-boot

62
推荐指数
8
解决办法
7万
查看次数

如何使用嵌入式Tomcat容器在Spring Boot中创建JNDI上下文

import org.apache.catalina.Context;
import org.apache.catalina.deploy.ContextResource;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:applicationContext.xml")
public class Application {

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder()
                .showBanner(false)
                .sources(Application.class)
                .run(args);
}

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
    return new EmbeddedServletContainerCustomizer() …
Run Code Online (Sandbox Code Playgroud)

jndi embedded-tomcat-7 spring-boot

45
推荐指数
4
解决办法
10万
查看次数

嵌入式vs独立Tomcat(HTTP)服务器

我正在开发一个新项目,它将是一个带有前端UI和后端Web服务的Web应用程序.我开始研究像Tomcat/Jetty那样使用哪些服务器......我还注意到这些HTTP服务器有一个嵌入式版本.我不明白何时对独立版本使用嵌入式版本.我尝试使用谷歌搜索,但无法找到令人信服的答案,所以如果有人向我解释嵌入式服务器的用例,我将不胜感激.提前致谢.

tomcat jetty embedded-jetty httpserver embedded-tomcat-7

14
推荐指数
2
解决办法
1万
查看次数

tomcat 7 embedded没有正确关闭ClassNotFoundException ContainerBase $ StopChild

我正在尝试使用此配置与Tomcat7嵌入式插件进行工作集成测试:

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>29360</port>
                    <systemProperties>
                        <logback.configurationFile>${project.build.testOutputDirectory}/logback-test.xml</logback.configurationFile>
                        <psw.config>${project.build.testOutputDirectory}</psw.config>
                        <spring.profiles.active>test-e2e</spring.profiles.active>
                    </systemProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                        <version>${slf4j.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                        <version>${logback.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>run-tomcat</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run-war-only</goal>
                        </goals>
                        <configuration>
                            <fork>true</fork>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-tomcat</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>shutdown</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

但是当关闭发生时,我一直有这个错误:引起:java.lang.ClassNotFoundException:org.apache.catalina.core.ContainerBase $ StopChild

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2:49.618s
[INFO] Finished at: Sun Dec 22 07:58:06 CET 2013
[INFO] Final Memory: 163M/259M
[INFO] ------------------------------------------------------------------------

ERROR: IllegalAccessException for stop method in class org.apache.tomcat.maven.plugin.tomcat7.run.ExtendedTomcat …
Run Code Online (Sandbox Code Playgroud)

tomcat maven maven-tomcat-plugin embedded-tomcat-7

9
推荐指数
1
解决办法
2451
查看次数

为嵌入式tomcat指定自定义web.xml

使用嵌入式tomcat实例时,有没有办法指定web.xml与标准不同的方法WEB-INF/web.xml

我想web.xml在我的src/test/resources(或其他一些区域)中添加一个并web.xml在启动嵌入式tomcat时引用它.

这是我现有的启动tomcat实例的代码

tomcat = new Tomcat();
String baseDir = ".";
tomcat.setPort(8080);
tomcat.setBaseDir(baseDir);
tomcat.getHost().setAppBase(baseDir);
tomcat.getHost().setAutoDeploy(true);
tomcat.enableNaming();

Context ctx = tomcat.addWebApp(tomcat.getHost(), "/sandbox-web", "src\\main\\webapp");
File configFile = new File("src\\main\\webapp\\META-INF\\context.xml");
ctx.setConfigFile(configFile.toURI().toURL());

tomcat.start();
Run Code Online (Sandbox Code Playgroud)

我从tomcat实例启动此服务器,我想在运行单元测试时执行以下操作

  • 关闭 contextConfigLocation
  • 指定ContextLoaderListener用于设置ApplicationContext嵌入式tomcat 的父级的自定义.

可以像这样指定此文件:

File webXmlFile = new File("src\\test\\resources\\embedded-web.xml");
Run Code Online (Sandbox Code Playgroud)

编辑

太多的无奈,我意识到,无论我做什么,我无法从寻找在劝说的Tomcat WEB-INFweb.xml.似乎我必须web.xml完全忽略它并以web.xml编程方式设置项目.

我最终得到了这个配置:

用于配置测试的cucumber.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="applicationContextProvider" class="ca.statcan.icos.sandbox.ApplicationContextProvider"/> …
Run Code Online (Sandbox Code Playgroud)

spring tomcat tomcat7 embedded-tomcat-7

7
推荐指数
1
解决办法
2万
查看次数

tomcat7-maven-plugin/tomcat7:运行自定义context.xml的位置?

我喜欢使用tomcat7-maven-plugin开发,特别是mvn tomcat7:run/tomcat7:run-war目标,以便快速测试我的应用程序,

这个插件允许你指定一个自定义的Context.xml(这对于为jndi数据源提供存根非常方便)

我的问题是,我想不出一个存储这个Context.xml的好地方.它根本不适合maven标准目录布局......

任何的想法 ?最佳做法?:d

谢谢,

maven maven-tomcat-plugin embedded-tomcat-7

7
推荐指数
1
解决办法
5734
查看次数

向Spring Boot嵌入式tomcat添加战争

我有一个spring-boot 2.1.2.RELEASE应用程序,该应用程序使用嵌入式tomcat Web服务器并通过其SDK 使用OpenKM

现在,我有一些集成测试,这些测试使用restassuredlib进行REST调用并验证响应结构。我的想法是将其集成OpenKM.war到此嵌入式tomcat中,并能够运行此测试,而无需在其他服务器上运行openkm应用程序。

这就是我使嵌入式tomcat读取和部署openkm.war的方式:

@Configuration
public class TomcatConfig {
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }

            @Override
            protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                new File(tomcat.getServer().getCatalinaBase(), "webapp").mkdirs();
                try {
                    tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
                } catch (Exception ex) {
                    throw new IllegalStateException("Failed to add okm", ex);
                }
                return super.getTomcatWebServer(tomcat);
            }
        }; …
Run Code Online (Sandbox Code Playgroud)

java spring embedded-tomcat-7 spring-boot openkm

7
推荐指数
1
解决办法
288
查看次数

tomcat7-maven-plugin:运行多个webapps但端口8080已经在使用中

我正在尝试使用tomcat7-maven-plugin设置嵌入式tomcat容器,pre-integration-test同步运行webapps ,运行集成测试,然后关闭tomcat post-integration-test.该项目是一个多模块Maven项目(包含app1,app2等等).父母pom.xml看起来像以下,

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <version>2.1</version>
      <configuration>
        <contextFile>path/to/context.xml</contextFile>
        <tomcatUsers>path/to/tomcat-users.xml</tomcatUsers>
        <fork>true</fork>
      </configuration>
      <executions>
        <execution>
          <id>tomcat-run</id>
          <goals>
            <goal>run</goal>
          </goals>
          <phase>pre-integration-test</phase>
        </execution>
        <execution>
          <id>tomcat-shutdown</id>
          <goals>
            <goal>shutdown</goal>
          </goals>
          <phase>post-integration-test</phase>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

然后我就跑了mvn clean integration-test --projects=app1,app2.当第二个应用程序开始,我得到的java.net.BindException,

SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:406)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:610)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:429)
    at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:981) …
Run Code Online (Sandbox Code Playgroud)

java web-applications maven tomcat7 embedded-tomcat-7

6
推荐指数
1
解决办法
1万
查看次数

带有球衣的嵌入式 tomcat 上的 java.net.SocketTimeoutException

我的服务器应用程序使用带有 Jersey 的嵌入式 tomcat。
我不时收到以下错误:

    02-03-2014 10:06:05 [com.sun.jersey.spi.container.ContainerResponse] [http-nio-8243-exec-4] [ERROR] - The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
java.net.SocketTimeoutException
    at org.apache.tomcat.util.net.NioBlockingSelector.read(NioBlockingSelector.java:191)
    at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:246)
    at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:227)
    at org.apache.coyote.http11.InternalNioInputBuffer.readSocket(InternalNioInputBuffer.java:419)
    at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:789)
    at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:814)
    at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:124)
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:346)
    at org.apache.coyote.Request.doRead(Request.java:422)
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:290)
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:449)
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:315)
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:200)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.Reader.read(Unknown Source)
    at com.google.common.io.CharStreams.copy(CharStreams.java:202)
    at com.google.common.io.CharStreams.toStringBuilder(CharStreams.java:248)
    at com.google.common.io.CharStreams.toString(CharStreams.java:222)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at …
Run Code Online (Sandbox Code Playgroud)

java tomcat jersey embedded-tomcat-7

6
推荐指数
1
解决办法
4389
查看次数

Java+Maven+Embedded Tomcat:项目拒绝识别网页

我一直试图通过Apache Tomcat嵌入到应用程序中来使我的 Java 应用程序托管一个网页(一个 HTML 页面,而不是 JSP)。我使用Maven了在构建系统NetBeans IDE 8.0.2.出于某种原因,Tomcat拒绝承认index.html我已经放置在应用程序页面,尽管多次尝试和创建各种文件夹一样的WEB-INF。但它仍然404向我抛出错误。

这是我在我的项目中设置的一些相关代码(一些代码已被省略但与情况无关):

1. MainApplication.java - 启动 Tomcat

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import java.util.Optional;
import org.apache.catalina.startup.Tomcat;

public class MainApplication{

    public static final Optional<String> port = Optional.ofNullable(System.getenv("PORT"));

    public static void main(String[] args) throws Exception {
        String contextPath = "/";
        String appBase = ".";
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(Integer.valueOf(port.orElse("8080")));
        tomcat.getHost().setAppBase(appBase);
        tomcat.addWebapp(contextPath, appBase);
        tomcat.start();
        tomcat.getServer().await(); …
Run Code Online (Sandbox Code Playgroud)

html java tomcat http-status-code-404 embedded-tomcat-7

5
推荐指数
1
解决办法
3576
查看次数