0

I'm trying to add a port to tomcat at runtime. Is there a way to do this? Or is there a way to listen on all ports? I'm using spring and I want the user to be able to add call an API endpoint to add a new port to listen on.

I know that you can add additional ports in the config file, but I don't know what ports I will need to add.

@RequestMapping("/port/{port}")
void setPort(@PathVariable("port") int port){
    //add new tomcat port to listen on
}
trevor
  • 11
  • 2
  • Welcome to Stack Overflow! Questions here are expected to show research effort; that is, what *you've* already done to solve your problem. We're happy to help, but only after you've tried yourself. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Good luck! – Pika Supports Ukraine May 29 '19 at 19:52
  • 1
    Why? What is the point of the web server serving exactly the same content on different ports? – Andreas May 29 '19 at 20:18
  • This is going to emulate multiple services and I want to separate them by port. I won't know how many or what they are until runtime. – trevor May 29 '19 at 20:25
  • Probably possible, but you'll have to deal with connectors, valves, ... You can have a look at https://howtodoinjava.com/tomcat/tomcats-architecture-and-server-xml-configuration-tutorial/ to understand some of Tomcat basics concept. But I think it will be a long and hard job. – Alexandre Cartapanis May 29 '19 at 21:11
  • Probably a better idea to look at a simpler server, like grizzly (https://javaee.github.io/grizzly/) that is the server of jersey, or even directly netty (https://netty.io/). – Alexandre Cartapanis May 29 '19 at 21:14
  • the answer is here https://stackoverflow.com/questions/57491231 – Sam Sep 30 '21 at 06:26

1 Answers1

-1
@Component
public class CustomizationBean implements
  WebServerFactoryCustomizer {

    @Override
    public void customize(ConfigurableServletWebServerFactory container) {
        container.setPort(8083);
    }
}