Question

I am embedding Apache Tomcat 7.0.30 in my application. I am using the Tomcat class,and my application requires dynamic addition and removal of connectors(HTTP).

Now while removing the connectors,the application gets undeployed but the port remains occupied and the connector continue to listen on the port. I am using customized HTTP connector.

Here is a snippet from my source code:

for removing connector:

       if( connector != null )
           {
              connector.decUsage();
               if( connector.getUsage() == 0 )
           {
            connector.stop();
            this.tomcat.getService().removeConnector( connector );
        }
    }

for adding connector:

this.tomcat.getService().addConnector( connector );

I know the port is not getting free. Because when I try to deploy to the same port again,it gives me an address already in use exception. I have surfed for this issue a lot,but to no avail. Any help would be greatly appreciated.

Thanks

Was it helpful?

Solution

You need to call connector.destroy(). The port binding is done in init by default, so correspondingly, unbinding is done in destroy.

If you want to re-use the connector, then set bindOnInit to false. Then the port will be bound/unbound on start/stop.

connector.setProperty("bindOnInit", "false")

A connector is stopped before removing, so you need not explicitly stop it when calling removeConnector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top