停止和重新启动嵌入式Tomcat 8失败,端口已在使用中。

huangapple go评论70阅读模式
英文:

Stoping and restarting embedded Tomcat8 fails with port already in use

问题

以下是您要翻译的内容:

我有一个Java(Kotlin)应用程序,启动了多个嵌入式服务器。这没问题。我还有一些单元测试,在每个单元测试之前启动这些服务器,并在之后停止它们。

Tomcat上的停止方法没有异常,但随后的服务器启动失败,报端口被占用。

我的基类相当标准。

open class TomcatBase(
    val tomcat: Tomcat = Tomcat()
) {
    val context: Context
    val contextPath: String

    init {
        tomcat.engine.name = UUID.randomUUID().toString()
        contextPath = ""
        val dir_url: URL = ClassLoader.getSystemResource("static")
        val docBase = File(dir_url.toURI()).absolutePath
        context = tomcat.addContext(contextPath, docBase)
    }

    fun addServlet(path: String, servlet: Servlet) {...}

    fun start() {
        tomcat.start()
        context.resources.isCachingAllowed = false
    }

    fun stop() {
        tomcat.stop()
    }
}
英文:

I have java (kotlin) app starting multiple embedded servers. That's allright. I have also few unit tests, which starts these servers before each unit and stops them after.

The stop method on tomcat finishes with no exception, but consequent server start starts fails with port is in use.

My base class is pretty standard

open class TomcatBase(
    val tomcat: Tomcat = Tomcat()
) {
    val context:Context
    val contextPath:String

    init {
        tomcat.engine.name=UUID.randomUUID().toString()
        contextPath = ""
        val dir_url: URL = ClassLoader.getSystemResource("static")
        val docBase = File(dir_url.toURI()).absolutePath
        context = tomcat.addContext(contextPath, docBase)
    }

    fun addServlet(path: String, servlet: Servlet) {...}

    fun start() {
        tomcat.start()
        context.resources.isCachingAllowed = false
    }

    fun stop() {
        tomcat.stop()        
    }
}

答案1

得分: 1

原来还有一个 destroy() 方法,可以完成这项工作

fun stop() {
tomcat.stop()
tomcat.destroy()
}


<details>
<summary>英文:</summary>

Turns out that there&#39;s also destroy() method, which does the job

fun stop() {
tomcat.stop()
tomcat.destroy()
}


</details>



huangapple
  • 本文由 发表于 2020年10月13日 23:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定