0

I have strange problem with tomcat, I have a spring boot application deployed on tomcat(Stand alone not embedded). I am able to start tomcat successfully and it is working fine with out any issues. But when I run shutdown(invoking shutdown.sh script) it is showing connection refused. I have tried out this

Tomcat doesn't stop. How can I debug this?, but does not solve my problem. I have tried removing my spring boot application and it is working fine with out any issues. I am able to start and stop tomcat. After further analysis I could find it is because of the ehache configured in spring boot application. The shutdown listener of ehcache need to configured, what is best way to do that in spring boot.

I have tried this option as well.

How to shutdown a Spring Boot Application in a correct way?

./shutdown.sh
Using CATALINA_BASE:   /app/tomcat/myapp
Using CATALINA_HOME:   /app/tomcat/myapp
Using CATALINA_TMPDIR: /app/tomcat/myapp/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_71
Using CLASSPATH:   

    /app/tomcat/myapp/bin/bootstrap.jar:/app/tomcat/myapp/bin/tomcat-juli.jar
    Oct 18, 2016 5:36:33 AM org.apache.catalina.startup.Catalina stopServer
  SEVERE: Could not contact localhost:8005. Tomcat may not be running.
Oct 18, 2016 5:43:31 AM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at java.net.Socket.<init>(Socket.java:434)
        at java.net.Socket.<init>(Socket.java:211)
        at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:476)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:408)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:497)

.

#-> netstat -anpl|grep -i 8005
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      21668/java

I am not sure why this is happening? Any idea why this would happen? Why it is not able to listen to shutdown port? also why shut port has 127.0.0.1? while other process do not have it?

I tried to do a telnet to the port and it is connecting

telnet 127.0.0.1 8005
 Trying 127.0.0.1...
 Connected to 127.0.0.1.
 Escape character is '^]'.

Any idea why this will be an issue.

--------Update

I have figured out this is happening when we deploy spring boot app with ehcache as standalone. The ehcache shutdown hook has to be called....What is the best way to do that in spring boot?

Community
  • 1
  • 1
Harry
  • 383
  • 2
  • 6
  • 21

1 Answers1

0

By default, Spring Boot applications are configured to run standalone.

If you want to run inside an Application Server (like Tomcat), the first thing you need to do is remove the embedded container dependencies to prevent any conflicts or contention. You can find a nice guide on how to do that here.

If you've done that and are still having issues, it means you have non-daemon threads left running inside the Application Server (which prevent it from terminating). You can use jstack to identify these and then write your own ServletContextFinalizer to gracefully stop these threads on contextDestroyed.