2

I have the following configuration:

A tomcat7 server running a Spring MVC application which uses Apache mina to connect to a Java-Console application (or any other type of application for that matter) to retrieve some data.

Now obvioisly when i shut down my server, there is still an open session in a seperate Thread preventing Tomcat7 from shutting down. so what i do is the following:

package at.dauzinger.tcpservice;

import javax.servlet.ServletContextEvent;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.*;

public class ServerContextListener implements javax.servlet.ServletContextListener
{
    PriceDao priceDao;

    public void contextDestroyed(ServletContextEvent event)
    {   
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        priceDao = (PriceDao)context.getBean("priceDao");
        priceDao.closeConnection();
    }

    public void contextInitialized(ServletContextEvent arg0) {
        //Method Stub
    }
}

And inside the web.xml

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
   <listener-class>at.dauzinger.tcpservice.ServerContextListener</listener-class>
</listener>

The ContextListener works, and even my console-application (the server-side of my socket connection) tells me that the session was terminated. But when i'm trying to stop tomcat via the eclipse GUI it fails to shut down and eclipse tells me it isn't reacting and offers me to forcefully terminate the server.

The last logentry form the server is:

Dez 19, 2012 4:39:17 PM org.apache.coyote.AbstractProtocolHandler stop INFO: Stopping ProtocolHandler ["ajp-bio-8009"]

My code running inside the tomcat-servlet:

connector = new NioSocketConnector();
connector.getSessionConfig().setUseReadOperation(true);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
session = connector.connect( new InetSocketAddress(PORT)).awaitUninterruptibly().getSession();

session.write(request);
String resp = (String) session.read().awaitUninterruptibly().getMessage();  

Is there any other thread mina launches when opening a connection? What prevents the tomcat instance from shutting down properly?

1 Answers1

1

I hava the same issue, and I solved it finally, just need to call unbind and dispose when you want to close tomcat.

@PreDestroy
public void close() {
    System.out.println("MINAServer.close");
    if (null != socketAcceptor) {
        socketAcceptor.unbind();
        socketAcceptor.dispose(false);
    }
}
Jolking
  • 11
  • 1