简体   繁体   中英

Can I change the port of an embedded Tomcat at runtime?

I'm using Tomcat embedded in my Java 8 application. I tried to switch the port at runtime like this:

 Tomcat tomcat = new Tomcat();
    tomcat.setPort(18080);

    Timer timer =new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            tomcat.setPort(18181);
        }
    },240000L);

But it doesn't seem to work. The wepapp responds still only to port 18080 after the timer delay was reached.

Is switching even possible at runtime? How can i do it?

EDIT:

Using Tomcat embedded 8.0.3 on JDK 1.8.0b129

I'd like to avoid to restart the server because the startup process takes a long time.

you should try restart server:

timer.schedule(new TimerTask() {
        @Override
        public void run() {
            tomcat.stop();
            tomcat.setPort(18181);
            tomcat.start();
            tomcat.getServer().await();
        }
},240000L);

您需要调用其默认的http连接器destroy方法,但是我不知道在Tomcat 8中是否可行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM