问题定位及排查

上周无意中调试程序在Linux上ps -ef|grep tomcat发现有许多tomcat的进程,当时因为没有影响系统运行就没当回事。而且我内心总觉得这可能是tomcat像nginx一样启动多个进程。

后来测试在一次升级后反馈说怎么现在tomcat进程无法shutdown?这让我有点意外,看来这个问题并没有这么简单。于是开始思考问题会出在哪里。

复现问题

先是另外一台服务器部署,然后shutdown后再ps进程是空的,这说明tomcat不会自动产生新的进程。那就有可能系统代码出了什么问题吧?最近另一个位同事有比较多的修改,可能是因为这些修改吧。光猜想也找不到问题,只好用jvisuale来看一下系统的dump,发现shutdown之后进程没有退出,而且里面有许多线程还在运行,有些还是线程池。

看来是有线程没有释放导致的泄露吧?于是用tail命令打开catalina.out查看最后shutdown.sh,在控制台输出了下面这些内容:

Nov 28, 2016 10:41:08 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/] appears to have started a thread named [Component socket reader] but has failed to stop it. This is very likely to create a memory leak.

确实有许多的线程没有关闭,在关闭时还提示了泄漏。从这些线程的名字可以确认了,是这近新增了一个openfire的whack外部组件导致的。这个whack可以连接到openfire服务器,实现一套扩展组件服务的功能,我们主要用来发送IM消息。这样做的好处是开启线程数少,效率高,并发性能很不错。

查看代码

先看一下ExternalComponentManager的实现,因为它是用来外部扩展组件的管理者,我们的操作基本是根据它来完成的。

下面的代码便是是创建一个ExternalComponentManager,并且设置参数同时连接到服务器。

private void CreateMessageSender() {
manager = new ExternalComponentManager(configHelper.getOpenfireHost(),
configHelper.getOpenfireExternalCompPort());
manager.setSecretKey(SENDER_NAME, configHelper.getOpenfirePwd());
manager.setMultipleAllowed(SENDER_NAME, true);
try {
msc = new MessageSenderComponent("senderComponent", manager.getServerName());
manager.addComponent(SENDER_NAME, msc);
} catch (ComponentException e) {
logger.error("CreateMessageSender error.", e);
}
}

那么最重要的是在哪里启动了线程?毕竟最终影响系统的是线程没有关闭。所以沿着addComponent这调用看看吧:

public void addComponent(String subdomain, Component component, Integer port) throws ComponentException {
if (componentsByDomain.containsKey(subdomain)) {
if (componentsByDomain.get(subdomain).getComponent() == component) {
// Do nothing since the component has already been registered
return;
}
else {
throw new IllegalArgumentException("Subdomain already in use by another component");
}
}
// Create a wrapping ExternalComponent on the component
ExternalComponent externalComponent = new ExternalComponent(component, this);
try {
// Register the new component
componentsByDomain.put(subdomain, externalComponent);
components.put(component, externalComponent);
// Ask the ExternalComponent to connect with the remote server
externalComponent.connect(host, port, subdomain);
// Initialize the component
JID componentJID = new JID(null, externalComponent.getDomain(), null);
externalComponent.initialize(componentJID, this);
}
catch (ComponentException e) {
// Unregister the new component
componentsByDomain.remove(subdomain);
components.remove(component);
// Re-throw the exception
throw e;
}
// Ask the external component to start processing incoming packets
externalComponent.start();
}

代码也比较简单,就是创建了一个wapper类ExternalComponent将我们自己的Component包装了一下。其中最为重要的是最后一句:externalComponent.start();

public void start() {
// Everything went fine so start reading packets from the server
readerThread = new SocketReadThread(this, reader);
readerThread.setDaemon(true);
readerThread.start();
// Notify the component that it will be notified of new received packets
component.start();
}

原来这里启动了一个读取线程,用于接收Openfire服务器发来的数据流。查看线程构造函数:

public SocketReadThread(ExternalComponent component, XPPPacketReader reader) {
super("Component socket reader");
this.component = component;
this.reader = reader;
}

可以看到,这个线程的名字是“Component socket reader”,在前面的日志里确实有这个线程。

解决问题

那么接下来的主要问题是如何关闭这个SocketReadThread,按理说会有相应的实现,发现externalComponent.start()这个方法有名字叫star,那么是不是有与其匹配的方法呢?确实有的一个shutdown的方法:

public void shutdown() {
shutdown = true;
// Notify the component to shutdown
component.shutdown();
disconnect();
}

原来这里调用了component.shutdown();最后还调用了一个disconnect,继续看代码:

private void disconnect() {
if (readerThread != null) {
readerThread.shutdown();
}
threadPool.shutdown();
TaskEngine.getInstance().cancelScheduledTask(keepAliveTask);
TaskEngine.getInstance().cancelScheduledTask(timeoutTask);
if (socket != null && !socket.isClosed()) {
try {
synchronized (writer) {
try {
writer.write("</stream:stream>");
xmlSerializer.flush();
}
catch (IOException e) {
// Do nothing
}
}
}
catch (Exception e) {
// Do nothing
}
try {
socket.close();
}
catch (Exception e) {
manager.getLog().error(e);
}
}
}

发现这里就有了线程shutdown的调用,OK,说明就是它了。

因为最外层代码使用的是ExternalComponentManager,那么在ExternalComponentManager中调用了ExternalComponent shutdown的方法是removeComponent,那么就是它了。

也就是说只要在最后应用关闭时调用removeComponent方法就可以释放线程资源。这里当然就可以借助ServletContextListener来完成咯。

public class MessageSenderServletContextListener implements ServletContextListener{
private final static Logger logger = LoggerFactory
.getLogger(MessageSenderServletContextListener.class); @Override
public void contextInitialized(ServletContextEvent sce) {
logger.debug("contextInitialized is run.");
} @Override
public void contextDestroyed(ServletContextEvent sce) {
logger.debug("contextDestroyed is run.");
MessageSender msgSender = SpringUtil.getBean(MessageSender.class);
try {
msgSender.shutdown();
logger.debug("MessageSender is shutdown.");
} catch (ComponentException e) {
logger.error(e.getMessage());
}
} }

实现contextDestroyed方法,从spring中获得MessageSender类,调用shutdown释放资源即可。

Tomcat shutdown执行后无法退出进程问题排查及解决的更多相关文章

  1. linux tomcat shutdown.sh 有时不能结束进程,使用如下指令进度重启

    ps -ef | grep tomcat | grep -v grep | cut -c 9-15 | xargs kill -9 & ./startup.sh

  2. 外部调用Tomcat启动脚本后日志中文显示乱码问题的解决

    外部sh脚本如下 #!/bin/bash while read LINE do echo "Hello $LINE!" case $LINE in all) tail -f -n2 ...

  3. 项目部署到tomcat Root中后导致 WebApplicationContext 初始化两次的解决方法

    上一篇文章刚说项目部署到tomcat的ROOT中,今天就发现一个问题.通过eclipse启动tomcat时候,WebApplicationContext 初始化两次: 现象:   通过eclipse控 ...

  4. linux下tomcat shutdown后 java进程依然存在

    今天遇到一个非常奇怪的问题,如标题所看到的: linux下(之所以强调linux下,是由于在windows下正常),运行tomcat ./shutdown.sh 后,尽管tomcat服务不能正常訪问了 ...

  5. Jenkins-ssh远程执行nohup- java无法退出

    一,初步 #执行方式 ssh 192.168.2.103 " nohup java -jar /home/a/ipf/ight/feedback/ixxxedback-platform-1. ...

  6. Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行

    一运行告警系统 创建一个任务计划crontab -e 每一分钟都执行一次 调试时把主脚本里边log先注释掉 再次执行 没有发现502文件说明执行成功了,每日有错误,本机IP 负载不高 二.分发系统-e ...

  7. Docker - 避免启动container后运行shell脚本执行完成后docker退出container

    问题 最近在使用 Dockerfile 启动容器,发现使用Dockerfile调用容器里面的shell,当shell执行完成以后,docker会退出容器. 分析 Docker 在执行shell的时候, ...

  8. linux下Tomcat shutdown无效

    问题: linux下Tomcat shutdown无效 linux下关闭tomcat后,发现重新启动Tomcat后.port号提示被占用, 原因: 这时可能是项目中的后台线程或者socket依旧在执行 ...

  9. linux上监控tomcat down掉后自动重启tomcat

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...

随机推荐

  1. Hangfire项目实践分享

    Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget jobs) 延迟任务执行(De ...

  2. jQuery的61种选择器

    The Write Less , Do More ! jQuery选择器 1. #id : 根据给定的ID匹配一个元素 <p id="myId">这是第一个p标签< ...

  3. “.Net 社区虚拟大会”(dotnetConf) 2016 Day 1 Keynote: Scott Hunter

    “.Net 社区虚拟大会”(dotnetConf) 2016 今天凌晨在Channel9 上召开,在Scott Hunter的30分钟的 Keynote上没有特别的亮点,所讲内容都是 微软“.Net社 ...

  4. Android数据加密之MD5加密

    前言: 项目中无论是密码的存储或者说判断文件是否是同一文件,都会用到MD5算法,今天来总结一下MD5加密算法. 什么是MD5加密? MD5英文全称“Message-Digest Algorithm 5 ...

  5. TortoiseGit 文件比对工具使用 Beyond Compare 和 DiffMerge

    TortoiseGit 内置的文件比对工具是 TortoiseMerge,用于文件比对和解决冲突合并,TortoiseGit 还支持外部的比对工具使用,比如 Beyond Compare 和 Diff ...

  6. 高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)

    在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是”这个系统似乎就是专门操作文档的“.毕竟现在的很多办公中大都是在PC端操作文档等软件,在这些庞大而繁重的业务中,单 ...

  7. 中国CIO最关心的八大问题(下)

    中国CIO最关心的八大问题(下) 从调研数据还可以看出,在企业级IT建设与投资上,CIO们并非是一群狂热的技术信徒,他们更多的是从企业发展阶段.信息化程度.技术成熟度.ROI等方面进行综合评估. 五. ...

  8. SQL Server中SELECT会真的阻塞SELECT吗?

    在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...

  9. phpexcel读取输出操作

    //读取 <?php header("Content-Type:text/html;charset=utf-8"); include 'Classes/PHPExcel.ph ...

  10. 2-1 Linux 操作系统及常用命令

    根据马哥linux初级视频 2-1.2-2来编辑 1. GUI与CLI GUI: Graphic User Interface CLI: Command Line Interface 注:在Windo ...