我们的 Web 应用程序管理流程的一部分是在与我们的 Web 服务器(在 Amazon EC2 中运行)共享的保管箱目录上创建 WAR。我们有一个在 Web 服务器上运行的 .NET 脚本,它监视 WAR 文件是否出现在保管箱文件夹中,然后将它们移动(而不是复制)到 Tomcat webapps 文件夹中。 .NET 代码的相关部分是:
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
Write-Host "Found new WAR file: $path"
### Wait until file is not in use, e.g. copy/move is complete
while (!(IsFileAccessible $path)) {
Write-Host "Waiting for WAR file to be unused."
Start-Sleep -s 10
}
### Move the new WAR file to the server deployment folder.
### Tomcat will detect the new file and deploy it automatically.
Move-Item "$path" "C:\Program Files (x86)\Apache Software Foundation\Tomcat 8.0\webapps" -force
Write-Host "WAR file moved to deployment directory."
}
通常这工作正常,但有时 Tomcat 无法展开整个 WAR,导致网站瘫痪(例如,缺少 index.html)。一些文件和文件夹在那里,但不是全部。只需删除部署文件夹并让 Tomcat 再次部署即可解决问题(因此这表明所有文件都在移动的 WAR 中)。
我想我实际上并不知道 (1) Tomcat 未能清理先前部署的所有文件并且根本没有爆炸,或者 (2) 它没有提取 WAR 的所有文件。
知道什么会导致这种行为吗?
最佳答案
以防其他人遇到这种特殊情况,我终于找到了提供线索的关键日志消息,由于未关闭文件的代码导致部署失败:
org.apache.catalina.startup.HostConfig.undeploy Undeploying context []
[ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.ExpandWar.deleteDir [C:\Program Files (x86)\Apache Software Foundation\Tomcat 8.0\webapps\ROOT\WEB-INF\classes\com\...\web\templates] could not be completely deleted. The presence of the remaining files may cause problems
有问题的 Java 代码:
InputStream in = Util.class.getClassLoader().getResourceAsStream("/com/.../web/templates/"+templateName)
并且流从未关闭。将它放在 try() 中以自动关闭解决了这个问题。
关于Tomcat autodeploy有时只爆炸部分WAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48306561/