ServerSocket, Tomcat as Service and Port
I have an application(Java) which delivers an embedded Jetty. By default I try to get the Port 8080 for the Jetty开发者_运维问答 Server. If the Port is in use, I try a random one:
try {
socket = new ServerSocket(def);
} catch (IOException e) {
socket = new ServerSocket(0);
}
The problem is that I have a customer who runs a Tomcat on 8080. If tomcat is closed, everything is fine. If Tomcat is running as service, and my app wants to get a port, it gets the default port(8080) although tomcat is listening there. If browsed localhost:8080/test.html, I get a http 404 from the Tomcat.
So somehow it seems tomcat is sleeping as long as there is no real request on the 8080 port.
I'd like to know why and how that's working and how I can alter my java-code to catch that problem.
kind regards
Change either Tomcat or Jetty to use another port. One listener per port.
You already handle the problem...
But you could handle it better with the "taskkill.exe" command (If you found the PID with "tasklist.exe" before).
Your problem is that two applications are competing for system resource which in your case is TCP port 8080. In order to free up the port you would have to work with the system API (widnows or linux or ...) at which point you are leaving the independence of Java behind. The only way I have see this working (although not whit web servers) is by telling the system to find the process that is currently holding on to the port and suspend it for few seconds. Than the other other process will automatically claim the port once there is request made for it. None the less it is messy solution.
精彩评论