1

I have a problem when to stop spring application context using: ConfigurableApplicationContext.close();

The http ports are stoped, but the spring actuator management port management.server.port 9001 was not shutdown.

So when I try to restart the application, spring give below exception: org.springframework.boot.web.server.PortInUseException: Port 9001 is already in use

Thanks.


update with snippet of application.properties:

management.server.port=9001
management.server.ssl.enabled=false
management.endpoints.web.exposure.include=info,health

1 Answers1

0

I'm not sure if I'm going to be any help but first I would try to kill whatever is running on 9001 manually.

I use the command: lsof -i :9001 to get the PID then kill -9 <PID>.

Then try to run application again, which should run fine. Wait for the shutdown and start again.

I set up a very basic Spring app locally and could not replicate the problem.

package com.example.demo;

import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication implements ApplicationContextAware {

  private static ApplicationContext appContext;

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);

    ((ConfigurableApplicationContext)appContext).close();
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    appContext = applicationContext;
  }
}

My yaml file then looks like this:

management:
  server:
    port: 9001

server:
  port: 8080

Would you be able to maybe provide a sample of your config file and where you are calling ConfigurableApplicationContext.close();?

Hexcii
  • 117
  • 1
  • 12
  • I update the question with piece of application.properties. But I found even I remove them problem still happen on next port, e.g. http port. did the close really close all the ports? – user3675334 Oct 18 '18 at 15:41
  • @user3675334 I put a Thread.sleep() in my application for 5 seconds to see whether the application actually runs on 8080 and then checked again after 5 seconds. Everything was closed. What I'm thinking is that your Spring context is being closed but your server connections aren't. I run my app in IntelliJ and there is a tomcat server configuration you can set. `Run/Debug Configuration` -> `+` -> `Tomcat Server` -> `On update action: Restart Server`. Some answers here might be helpful too https://stackoverflow.com/questions/29169916/how-correctly-close-the-applicationcontext-in-spring – Hexcii Oct 22 '18 at 11:55
  • Thank you. recently too busy. currently not on this, but i will try to figure out more when back to this. – user3675334 Nov 25 '18 at 16:11