Here is a little background info on what I am trying to do. I work for a company that writes IVR software for small utility companies. I'm a recent college graduate and just started working here about 6 months ago. They are trying to slowly bring me into the project, so I'm working on something that could have easily been a stand-alone application, but is being included in the IVR package.
Right now, people who purchase an IVR system from us can log into it via a web app. What I am creating is a server/client. The server runs as a thread on the IVR, and the client goes on the clients computers (duh). It is basically an alert type system. If something goes wrong, then the IVR will hand my server an alert message and who to send it to (several utilities may use the same IVR), and it sends it to the client. Its actually almost full done and working great, and I'm in the stage of ironing out all the small problems it has. One of them has me completely stumped, and is why I'm here asking this question. I'll try to provide any details I think will be helpful.
The problem is that when I bind my server to a port, that port never gets released... ever. I can kill the Glassfish server (which is what we use) and kill the virtual machine, but the next time I try to restart the server I get a
java.net.BindException: Address already in use: bind
exception thrown. The only way I can reclaim them is to restart windows.
Here is how I bind the address
And yes, I call serverChannel.close() if an exception is caught that terminates the run method.
When I first started writing the server, I was writing it as a stand-alone app so I could test it easier. Once I got to the point though to test the user logging in, I had to tie it into the IVR so that it could interact with the database. This means I have to start Glassfish and republish to Glassfish every time I make changes to the server. This problem did not crop up until I had to do that. For now, I've just been incrementing the port number every single time I republish. This may even be a threading issue, and not a networking issue, I'm not really sure.
When Glassfish republishes the server, I'm not totally sure its stopping my server thread. I have no idea what goes on behind the scenes when Glassfish republishes. But... like isaid, I can totalyl stop Glassfish and restart it and I still get BindExceptions thrown. To create the thread when the IVR starts up, we decided to just make a little servlet to do it. Here is the code for that
sorry for this ignorant quetion.. but if you are still listening shouldn't you be using glassfish to listen for requestes and then dispatch the requests to your component, probably a servlet instead of binding your program to a port and trying to listen to incoming requests?
I fixed the problem. It turns out that somehow none of my threads were being destroyed. When restarting the computer the jvm would "hibernate" and then resume all the threads when the system came back up. I was able to go into the IVr control panel and look at close to 600 threads being open, some of which were from classes that I haven't used in weeks.
Anyway, in our platform class I added a method called shutdownThreads() and added this code to the above servlet