[Bug 52028] Embeded Tomcat using a Connector with a random port (port 0)
https://bz.apache.org/bugzilla/show_bug.cgi?id=52028 --- Comment #3 from Konstantin Kolinko--- Backported to Tomcat 6 (r1723545, r1723551) and will be in 6.0.45 onwards. -- You are receiving this mail because: You are the assignee for the bug. - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
DO NOT REPLY [Bug 52028] Embeded Tomcat using a Connector with a random port (port 0)
https://issues.apache.org/bugzilla/show_bug.cgi?id=52028 Mark Thomas ma...@apache.org changed: What|Removed |Added Status|NEW |RESOLVED Resolution||FIXED --- Comment #2 from Mark Thomas ma...@apache.org 2011-11-28 23:20:32 UTC --- This has been fixed in trunk and back-ported to 7.0.x. It will be included in 7.0.24 onwards. -- Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email --- You are receiving this mail because: --- You are the assignee for the bug. - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
DO NOT REPLY [Bug 52028] Embeded Tomcat using a Connector with a random port (port 0)
https://issues.apache.org/bugzilla/show_bug.cgi?id=52028 --- Comment #1 from Mark Thomas ma...@apache.org 2011-10-28 17:52:55 UTC --- Created attachment 27861 -- https://issues.apache.org/bugzilla/attachment.cgi?id=27861 v1 patch for discussion I have started to look at this and it looks doable. port=0 works for all connectors for 64-bit Windows. The problem is with MBean names, JMX MBean registration and logging info. The port number is not available at the point where the JMX registration occurs. If bindOnInit=false, the port number may change multiple times during the life of the connector. With multiple connectors using port=0 there will be a naming clash as the connectors need to be registered before the the ports are known. I see two options. 1. Use auto-1, auto-2 instead of the port number in the JMX names (with a static, thread-safe counter). 2. Re-register the connector and related components when the port number changes. I'm leaning towards one as it is a heck of a lot simpler. Thoughts? -- Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email --- You are receiving this mail because: --- You are the assignee for the bug. - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
2011/10/10 Konstantin Kolinko knst.koli...@gmail.com: 2011/10/9 Olivier Lamy ol...@apache.org: tomcat.getConnector().setPort( 0 ); 1) Look at how TomcatBaseTest assigns subsequent port numbers, starting with 8001. http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?view=markup#l84 2) While the proposed feature may have some usage, I think that I would want more control in what range the opened port number will be. Just setting a 0 would not provide such control. 3) There are several Connector/Endpoint implementations in Tomcat. While java.net.ServerSocket does support port number of 0, I am not sure that APR-based implementation does allow it. Sure but the use case is just to start a http/https (apr can be omitted) connector on any random free port, do some unit test and stop it. IMHO it's a valid use case (and with it folks will be able to use tomcat rather than an other servlet container which has this feature available :-) ). See the code snippet I have pointed, the code to write for using tomcat is really smaller/smarter (except all hacking I have to write due to the restriction on port). 2011/10/9 Mark Thomas ma...@apache.org: On 09/10/2011 15:08, Olivier Lamy wrote: Hello, I'd like to be able to use a random port when using embedded Tomcat in unit tests to test servlets. Currently it's locked by a test in Connector#startInternal. Is it intentional ? svn blame would have answered that for you. To be specific, that was http://svn.apache.org/viewvc?view=revisionrevision=1147949 Best regards, Konstantin Kolinko - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org -- Olivier Lamy Talend : http://talend.com http://twitter.com/olamy | http://linkedin.com/in/olamy - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
3) There are several Connector/Endpoint implementations in Tomcat. While java.net.ServerSocket does support port number of 0, I am not sure that APR-based implementation does allow it. Sure but the use case is just to start a http/https (apr can be omitted) connector on any random free port, do some unit test and stop it. IMHO it's a valid use case (and with it folks will be able to use tomcat rather than an other servlet container which has this feature available :-) ). See the code snippet I have pointed, the code to write for using tomcat is really smaller/smarter (except all hacking I have to write due to the restriction on port). Guys. It make sense to have a way to get an unused random port for embedded mode in testing cases. On a CI system, ie Jenkins, you could have many concurrents tests done at the same time, the only solution to get a free port is to discover it at startup time isn't it ? - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
2011/10/14 Henri Gomez henri.go...@gmail.com: 3) There are several Connector/Endpoint implementations in Tomcat. While java.net.ServerSocket does support port number of 0, I am not sure that APR-based implementation does allow it. Sure but the use case is just to start a http/https (apr can be omitted) connector on any random free port, do some unit test and stop it. IMHO it's a valid use case (and with it folks will be able to use tomcat rather than an other servlet container which has this feature available :-) ). See the code snippet I have pointed, the code to write for using tomcat is really smaller/smarter (except all hacking I have to write due to the restriction on port). Guys. It make sense to have a way to get an unused random port for embedded mode in testing cases. On a CI system, ie Jenkins, you could have many concurrents tests done at the same time, the only solution to get a free port is to discover it at startup time isn't it ? Well, after some research, using some random port for bind(0) seems to be a kernel feature. I cannot confirm that apr_socket_bind can accept 0 as port number [1], but seems that allowing 0 will use less resources than trying to programmatically select a random port and retry on failure. [1] http://apr.apache.org/docs/apr/1.4/group__apr__network__io.html So maybe let's go with this and document that support for 0 depends on platform and connector implementation. I think that the value of -1 could be officially used to disable the connector (prevent it from binding and starting). I think -1 can be used as the initial value for Connector#port as well, and that this value can be used to re-implement the check in http://svn.apache.org/viewvc?view=revisionrevision=1147949 Best regards, Konstantin Kolinko - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
On 14/10/2011 09:27, Olivier Lamy wrote: 2011/10/10 Konstantin Kolinko knst.koli...@gmail.com: 2011/10/9 Olivier Lamy ol...@apache.org: tomcat.getConnector().setPort( 0 ); 1) Look at how TomcatBaseTest assigns subsequent port numbers, starting with 8001. http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?view=markup#l84 2) While the proposed feature may have some usage, I think that I would want more control in what range the opened port number will be. Just setting a 0 would not provide such control. 3) There are several Connector/Endpoint implementations in Tomcat. While java.net.ServerSocket does support port number of 0, I am not sure that APR-based implementation does allow it. Sure but the use case is just to start a http/https (apr can be omitted) connector on any random free port, do some unit test and stop it. Consistency of behaviour and configuration across the connectors is important. Where there are differences, we should be looking to converge, not diverge. IMHO it's a valid use case (and with it folks will be able to use tomcat rather than an other servlet container which has this feature available :-) ). See the code snippet I have pointed, the code to write for using tomcat is really smaller/smarter (except all hacking I have to write due to the restriction on port). An additional issue is that not specifying a port in server.xml will result in 0 being used which would then result in a random port being bound. I don't like that at all. I much prefer the current behaviour of throwing an error. If binding to a random port is to be supported I think it needs to be an explicit choice - e.g. by setting a special value such as auto - and ideally needs to be supported by all connectors. In summary, I think the minimum I would find acceptable is: - use a special value - support for BIO and NIO Ideally, APR needs to be supported to. Mark - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
DO NOT REPLY [Bug 52028] New: Embeded Tomcat using a Connector with a random port (port 0)
https://issues.apache.org/bugzilla/show_bug.cgi?id=52028 Bug #: 52028 Summary: Embeded Tomcat using a Connector with a random port (port 0) Product: Tomcat 7 Version: trunk Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: Connectors AssignedTo: dev@tomcat.apache.org ReportedBy: ol...@apache.org Classification: Unclassified New feature based on discussion: http://markmail.org/message/3nczqiqm6smez255 -- Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email --- You are receiving this mail because: --- You are the assignee for the bug. - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
2011/10/14 Mark Thomas ma...@apache.org: On 14/10/2011 09:27, Olivier Lamy wrote: 2011/10/10 Konstantin Kolinko knst.koli...@gmail.com: 2011/10/9 Olivier Lamy ol...@apache.org: tomcat.getConnector().setPort( 0 ); 1) Look at how TomcatBaseTest assigns subsequent port numbers, starting with 8001. http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?view=markup#l84 2) While the proposed feature may have some usage, I think that I would want more control in what range the opened port number will be. Just setting a 0 would not provide such control. 3) There are several Connector/Endpoint implementations in Tomcat. While java.net.ServerSocket does support port number of 0, I am not sure that APR-based implementation does allow it. Sure but the use case is just to start a http/https (apr can be omitted) connector on any random free port, do some unit test and stop it. Consistency of behaviour and configuration across the connectors is important. Where there are differences, we should be looking to converge, not diverge. Agree. Makes sense. IMHO it's a valid use case (and with it folks will be able to use tomcat rather than an other servlet container which has this feature available :-) ). See the code snippet I have pointed, the code to write for using tomcat is really smaller/smarter (except all hacking I have to write due to the restriction on port). An additional issue is that not specifying a port in server.xml will result in 0 being used which would then result in a random port being bound. I don't like that at all. I much prefer the current behaviour of throwing an error. If binding to a random port is to be supported I think it needs to be an explicit choice - e.g. by setting a special value such as auto - and ideally needs to be supported by all connectors. This new auto attribute sounds a good solution to me. Do we agree on the fact that after been started, the protocol handler will update the connector.port field to make the real port used available for use ? I have opened https://issues.apache.org/bugzilla/show_bug.cgi?id=52028 and will try to propose a patch next week. Thanks -- Olivier In summary, I think the minimum I would find acceptable is: - use a special value - support for BIO and NIO Ideally, APR needs to be supported to. Mark - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
2011/10/14 Mark Thomas ma...@apache.org: If binding to a random port is to be supported I think it needs to be an explicit choice - e.g. by setting a special value such as auto - and ideally needs to be supported by all connectors. 2011/10/14 Olivier Lamy ol...@apache.org: Do we agree on the fact that after been started, the protocol handler will update the connector.port field to make the real port used available for use ? If it were port=0 then you would better define a separate getLocalPort() property. The auto value... how can that be? Connector.setPort() accepts integer, and it can also be accessed through JMX. There are several layers, and if you say update its port number, which of them will update its member variable and which not, and what happens if connector is restarted e.g. through JMX? Also I would like to introduce support for -1 value for port number, as I mentioned above, but that would be a separate feature. I have opened https://issues.apache.org/bugzilla/show_bug.cgi?id=52028 and will try to propose a patch next week. Best regards, Konstantin Kolinko - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Embeded Tomcat using a Connector with a random port (port 0)
Hello, I'd like to be able to use a random port when using embedded Tomcat in unit tests to test servlets. Currently it's locked by a test in Connector#startInternal. Is it intentional ? I'd like to be able to do simply something like : Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(System.getProperty(java.io.tmpdir)); tomcat.getConnector().setPort( 0 ); Context context = tomcat.addContext(, System.getProperty(java.io.tmpdir)); Tomcat.addServlet(context, cxf, new MyServlet()); context.addServletMapping(/ + getRestServicesPath() + /*, cxf); tomcat.start(); int port = tomcat.getConnector().getPort( ); here my tests with the port tomcat.stop() At least I'd like to have: tomcat.getConnector().getLocalPort( ) which returns the port on which the socket is listening (most of the time will be same as getPort() except when using port 0 ) Currently I do something which I find a little hackhish :-) : Http11Protocol http11Protocol = ( (Http11Protocol) tomcat.getConnector().getProtocolHandler() ); // ReflectionUtils is a Spring framework class to help Field fieldEndpoint = ReflectionUtils.findField(Http11Protocol.class, endpoint); fieldEndpoint.setAccessible(true); JIoEndpoint jIoEndpoint = (JIoEndpoint) fieldEndpoint.get(http11Protocol); Field serverSocketField = ReflectionUtils.findField(JIoEndpoint.class, serverSocket); serverSocketField.setAccessible(true); ServerSocket serverSocket = (ServerSocket) serverSocketField.get(jIoEndpoint); this.port = serverSocket.getLocalPort(); Makes sense for to add getLocalPort on Connector class and remove the test if (getPort() 1) ? If yes I can start on providing a patch for review. Thanks -- Olivier Lamy Talend : http://talend.com http://twitter.com/olamy | http://linkedin.com/in/olamy - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
On 09/10/2011 15:08, Olivier Lamy wrote: Hello, I'd like to be able to use a random port when using embedded Tomcat in unit tests to test servlets. Currently it's locked by a test in Connector#startInternal. Is it intentional ? svn blame would have answered that for you. I'd like to be able to do simply something like : Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(System.getProperty(java.io.tmpdir)); tomcat.getConnector().setPort( 0 ); If you want a random port, why not just set a random port at this point? Makes sense for to add getLocalPort on Connector class and remove the test if (getPort() 1) ? No. If yes I can start on providing a patch for review. I'd be against adding that patch to the code base. Mark - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
2011/10/9 Mark Thomas ma...@apache.org: On 09/10/2011 15:08, Olivier Lamy wrote: Hello, I'd like to be able to use a random port when using embedded Tomcat in unit tests to test servlets. Currently it's locked by a test in Connector#startInternal. Is it intentional ? svn blame would have answered that for you. I'd like to be able to do simply something like : Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(System.getProperty(java.io.tmpdir)); tomcat.getConnector().setPort( 0 ); If you want a random port, why not just set a random port at this point? Yup but I'm not sure the generated random number will be a free port ! At least ServerSocket ( 0 ) will ensure to use a free port. My goal is to be able to write unit tests using a non harcoded port which I'm sure is free on machines where the build run. Makes sense for to add getLocalPort on Connector class and remove the test if (getPort() 1) ? No. At least having this validation in a separate method to ease override or having a sysprops which make using port 0 possible. Here I have to override startInternal(). see this unit test here [1] method startServer . I just found that complicated/hackhish just to be able to start an embedded servlet container on any free port to run unit tests ... If yes I can start on providing a patch for review. I'd be against adding that patch to the code base. Mark - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org Olivier [1] http://svn.codehaus.org/redback/redback/trunk/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/codehaus/redback/rest/services/AbstractRestServicesTest.java - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org
Re: Embeded Tomcat using a Connector with a random port (port 0)
2011/10/9 Olivier Lamy ol...@apache.org: tomcat.getConnector().setPort( 0 ); 1) Look at how TomcatBaseTest assigns subsequent port numbers, starting with 8001. http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?view=markup#l84 2) While the proposed feature may have some usage, I think that I would want more control in what range the opened port number will be. Just setting a 0 would not provide such control. 3) There are several Connector/Endpoint implementations in Tomcat. While java.net.ServerSocket does support port number of 0, I am not sure that APR-based implementation does allow it. 2011/10/9 Mark Thomas ma...@apache.org: On 09/10/2011 15:08, Olivier Lamy wrote: Hello, I'd like to be able to use a random port when using embedded Tomcat in unit tests to test servlets. Currently it's locked by a test in Connector#startInternal. Is it intentional ? svn blame would have answered that for you. To be specific, that was http://svn.apache.org/viewvc?view=revisionrevision=1147949 Best regards, Konstantin Kolinko - To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org