Tomcat关闭时的资源清理

Spring配置文件:
package com.ghca.config;

import *

/**
 * Created by JialeLi on 6/9/2014.
 * Spring的配置
 */
@Configuration
@ComponentScan("com.ghca")
@EnableWebMvc
@PropertySource("classpath:config.properties")
public class SpringConfig {

    @Value("${selfService.xmlEncoding}")
    private String xmlCharSet;
    @Value("${selfService.port}")
    private int selfServicePort;
    @Value("${selfService.idleTime}")
    private int selfServiceIdleTime;

    /**
     * 配合@PropertySource 注解 处理properties文件
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Bean
    @Autowired
    @Qualifier("SelfServiceHandler")
    public IoAcceptor minaInitial(IoHandler handler) throws IOException {
        IoAcceptor acceptor = new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
        chain.addLast("logger", new LoggingFilter());
        chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName(xmlCharSet))));
        acceptor.setHandler(handler);
        acceptor.getSessionConfig().setReadBufferSize(2048);
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, selfServiceIdleTime);
        acceptor.bind(new InetSocketAddress(selfServicePort));
        return acceptor;
    }

}

在tomcat关闭时会抛出如下警告:

^C19-May-2015 13:57:28.036 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-80"]
19-May-2015 13:57:28.091 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
19-May-2015 13:57:28.142 INFO [Thread-5] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
13:57:28 [localhost-startStop-1][INFO ] org.springframework.web.context.support.AnnotationConfigWebApplicationContext doClose 873 - Closing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Tue May 19 13:57:21 CST 2015]; root of context hierarchy
19-May-2015 13:57:28.188 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads The web application [/authServer] appears to have started a thread named [NioSocketAcceptor-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
 sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
 sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
 sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
 sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
 sun.nio.ch.SelectorImpl.select(SelectorImpl.java:102)
 org.apache.mina.transport.socket.nio.NioSocketAcceptor.select(NioSocketAcceptor.java:234)
 org.apache.mina.core.polling.AbstractPollingIoAcceptor$Acceptor.run(AbstractPollingIoAcceptor.java:417)
 org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
 java.lang.Thread.run(Thread.java:745)
19-May-2015 13:57:28.218 INFO [Thread-5] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-80"]

表示mina线程没有正确的关闭, 可能会造成内存泄漏.

解决方法是在Spring配置中指定资源清理方法. 即在@Bean注解中配置destroyMethod属性


@Bean(destroyMethod = "dispose")
@Autowired
@Qualifier("SelfServiceHandler")
public IoAcceptor minaInitial(IoHandler handler) throws IOException {
	IoAcceptor acceptor = new NioSocketAcceptor();
	DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
	chain.addLast("logger", new LoggingFilter());
	chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName(xmlCharSet))));
	acceptor.setHandler(handler);
	acceptor.getSessionConfig().setReadBufferSize(2048);
	acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, selfServiceIdleTime);
	acceptor.bind(new InetSocketAddress(selfServicePort));
	return acceptor;
}

参考 http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html

转载于:https://my.oschina.net/CasparLi/blog/416734

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值