tomcat最根本就是一个Socket Server,于是我找到了org.apache.tomcat.util.net.DefaultServerSocketFactory#createSocket(int port, int backlog),最终就是这个方法执行new java.net.ServerSocket(port, backlog)启动了一个ServerSocket实例。
查看ServerSocket API就发现@param backlog the maximum length of the queue.
明确了,tomcat的acceptCount就是ServerSocket的等待队列。
但设置的acceptCount怎么设置到backlog上呢,我翻了好一会儿代码才注意到org.apache.catalina.connector.Connector中有一个变态的HashMap通过这个HashMap把参数名做了一次转换,再赋值给Http11Protocol使用。这样的变态我想应该是想方便tomcat的使用者吧,毕竟整一个backlog参数谁知道是干什么的,另外这个HashMap也把其它参数名做了转换,代码如下
protected static HashMap replacements = new HashMap(); static { replacements.put("acceptCount", "backlog"); replacements.put("connectionLinger", "soLinger"); replacements.put("connectionTimeout", "soTimeout"); replacements.put("connectionUploadTimeout", "timeout"); replacements.put("clientAuth", "clientauth"); replacements.put("keystoreFile", "keystore"); replacements.put("randomFile", "randomfile"); replacements.put("rootFile", "rootfile"); replacements.put("keystorePass", "keypass"); replacements.put("keystoreType", "keytype"); replacements.put("sslProtocol", "protocol"); replacements.put("sslProtocols", "protocols"); } // ------------------------------------------------------------- Properties /** * Return a configured property. */ public Object getProperty(String name) { String repl = name; if (replacements.get(name) != null) { repl = (String) replacements.get(name); } return IntrospectionUtils.getProperty(protocolHandler, repl); } /** * Set a configured property. */ public boolean setProperty(String name, String value) { String repl = name; if (replacements.get(name) != null) { repl = (String) replacements.get(name); } return IntrospectionUtils.setProperty(protocolHandler, repl, value); }
总结:acceptCount参数其实就是new java.net.ServerSocket(port, backlog)的第二个参数,了解后再设置就不会盲目了。