英文:
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's also destroy() method, which does the job
fun stop() {
tomcat.stop()
tomcat.destroy()
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论