port.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,800 行 · 第 1/3 页
JAVA
1,800 行
/** * Configures the throttle. */ public void setThrottleConcurrentMax(int max) { Throttle throttle = createThrottle(); throttle.setMaxConcurrentRequests(max); } /** * Configures the throttle. */ public long getThrottleConcurrentMax() { if (_throttle != null) return _throttle.getMaxConcurrentRequests(); else return -1; } /** * Sets the write timeout for the accepted sockets. * * @deprecated */ public void setWriteTimeout(Period period) { } private Throttle createThrottle() { if (_throttle == null) { _throttle = Throttle.createPro(); if (_throttle == null) throw new ConfigException(L.l("throttle configuration requires Resin Professional")); } return _throttle; } // // compat config // /** * Sets the minimum spare listen. */ public void setMinSpareListen(int minSpare) throws ConfigException { setAcceptThreadMin(minSpare); } /** * Sets the maximum spare listen. */ public void setMaxSpareListen(int maxSpare) throws ConfigException { setAcceptThreadMax(maxSpare); } // // statistics // /** * Returns the number of connections */ public int getConnectionCount() { return _connectionCount; } /** * Returns the number of comet connections. */ public int getCometIdleCount() { return _suspendList.size(); } /** * Returns the number of duplex connections. */ public int getDuplexCount() { return 0; } void addLifetimeRequestCount() { _lifetimeRequestCount++; } public long getLifetimeRequestCount() { return _lifetimeRequestCount; } void addLifetimeKeepaliveCount() { _lifetimeKeepaliveCount++; } public long getLifetimeKeepaliveCount() { return _lifetimeKeepaliveCount; } void addLifetimeClientDisconnectCount() { _lifetimeClientDisconnectCount++; } public long getLifetimeClientDisconnectCount() { return _lifetimeClientDisconnectCount; } void addLifetimeRequestTime(long time) { _lifetimeRequestTime += time; } public long getLifetimeRequestTime() { return _lifetimeRequestTime; } void addLifetimeReadBytes(long time) { _lifetimeReadBytes += time; } public long getLifetimeReadBytes() { return _lifetimeReadBytes; } void addLifetimeWriteBytes(long time) { _lifetimeWriteBytes += time; } public long getLifetimeWriteBytes() { return _lifetimeWriteBytes; } /** * Sets the keepalive max. */ public void setKeepaliveMax(int max) { _keepaliveMax = max; } /** * Gets the keepalive max. */ public int getKeepaliveMax() { return _keepaliveMax; } /** * Sets the keepalive max. */ public void setKeepaliveConnectionTimeMax(Period period) { _keepaliveTimeMax = period.getPeriod(); } /** * Gets the keepalive max. */ public long getKeepaliveConnectionTimeMax() { return _keepaliveTimeMax; } /** * Gets the suspend max. */ public long getSuspendTimeMax() { return _suspendTimeMax; } public void setSuspendTimeMax(Period period) { _suspendTimeMax = period.getPeriod(); } public void setKeepaliveTimeout(Period period) { _keepaliveTimeout = period.getPeriod(); } public long getKeepaliveTimeout() { return _keepaliveTimeout; } public long getKeepaliveSelectThreadTimeout() { return _keepaliveSelectThreadTimeout; } public int getKeepaliveSelectMax() { if (getSelectManager() != null) return getSelectManager().getSelectMax(); else return -1; } // // statistics // /** * Returns the thread count. */ public int getThreadCount() { return _threadCount; } /** * Returns the active thread count. */ public int getActiveThreadCount() { return _threadCount - _idleThreadCount; } /** * Returns the count of idle threads. */ public int getIdleThreadCount() { return _idleThreadCount; } /** * Returns the number of keepalive connections */ public int getKeepaliveCount() { synchronized (_keepaliveCountLock) { return _keepaliveCount; } } public Lifecycle getLifecycleState() { return _lifecycle; } /** * Returns true if the port is active. */ public boolean isActive() { return _lifecycle.isActive(); } /** * Returns the active connections. */ public int getActiveConnectionCount() { return _threadCount - _idleThreadCount; } /** * Returns the keepalive connections. */ public int getKeepaliveConnectionCount() { return getKeepaliveCount(); } /** * Returns the number of keepalive connections */ public int getKeepaliveThreadCount() { synchronized (_keepaliveCountLock) { return _keepaliveThreadCount; } } /** * Returns the number of connections in the select. */ public int getSelectConnectionCount() { if (_selectManager != null) return _selectManager.getSelectCount(); else return -1; } /** * Returns the accept pool. */ public int getFreeKeepalive() { int freeKeepalive = _keepaliveMax - _keepaliveCount; int freeConnections = _connectionMax - _connectionCount - _minSpareConnection; int freeSelect = _server.getFreeSelectKeepalive(); if (freeKeepalive < freeConnections) return freeSelect < freeKeepalive ? freeSelect : freeKeepalive; else return freeSelect < freeConnections ? freeSelect : freeConnections; } /** * Returns true if the port matches the server id. */ public boolean matchesServerId(String serverId) { return getServerId().equals("*") || getServerId().equals(serverId); } /** * Initializes the port. */ @PostConstruct public void init() throws ConfigException { if (! _lifecycle.toInit()) return; if (_server instanceof EnvironmentBean) Environment.addEnvironmentListener(this, ((EnvironmentBean) _server).getClassLoader()); StringBuilder url = new StringBuilder(); if (_protocol != null) url.append(_protocol.getProtocolName()); else url.append("unknown"); url.append("://"); if (getAddress() != null) url.append(getAddress()); else url.append("*"); url.append(":"); url.append(getPort()); if (_serverId != null && ! "".equals(_serverId)) { url.append("("); url.append(_serverId); url.append(")"); } _url = url.toString(); } /** * Starts the port listening. */ public void bind() throws Exception { synchronized (this) { if (_isBind) return; _isBind = true; } if (_protocol == null) throw new IllegalStateException(L.l("'{0}' must have a configured protocol before starting.", this)); if (_port == 0) return; if (_throttle == null) _throttle = new Throttle(); if (_serverSocket != null) { if (_port == 0) { } else if (_address != null) log.info("listening to " + _address + ":" + _port); else log.info("listening to " + _port); } else if (_sslFactory != null && _socketAddress != null) { _serverSocket = _sslFactory.create(_socketAddress, _port); log.info(_protocol.getProtocolName() + "s listening to " + _socketAddress.getHostName() + ":" + _port); } else if (_sslFactory != null) { if (_address == null) { _serverSocket = _sslFactory.create(null, _port); log.info(_protocol.getProtocolName() + "s listening to *:" + _port); } else { InetAddress addr = InetAddress.getByName(_address); _serverSocket = _sslFactory.create(addr, _port); log.info(_protocol.getProtocolName() + "s listening to " + _address + ":" + _port); } } else if (_socketAddress != null) { _serverSocket = QJniServerSocket.create(_socketAddress, _port, _acceptListenBacklog); log.info(_protocol.getProtocolName() + " listening to " + _socketAddress.getHostName() + ":" + _port); } else { _serverSocket = QJniServerSocket.create(_port, _acceptListenBacklog); log.info(_protocol.getProtocolName() + " listening to *:" + _port); } assert(_serverSocket != null); postBind(); } /** * Starts the port listening. */ public void bind(QServerSocket ss) throws Exception { if (ss == null) throw new NullPointerException(); _isBind = true; if (_protocol == null) throw new IllegalStateException(L.l("'{0}' must have a configured protocol before starting.", this)); if (_throttle == null) _throttle = new Throttle(); _serverSocket = ss; String scheme = _protocol.getProtocolName(); if (_address != null) log.info(scheme + " listening to " + _address + ":" + _port); else log.info(scheme + " listening to *:" + _port); if (_sslFactory != null) _serverSocket = _sslFactory.bind(_serverSocket); } public void postBind() { synchronized (this) { if (_isPostBind) return; _isPostBind = true; } if (_tcpNoDelay) _serverSocket.setTcpNoDelay(_tcpNoDelay); _serverSocket.setConnectionSocketTimeout((int) getSocketTimeout()); if (_serverSocket.isJNI() && _server.isSelectManagerEnabled()) { _selectManager = _server.getSelectManager(); if (_selectManager == null) { throw new IllegalStateException(L.l("Cannot load select manager")); } } if (_keepaliveMax < 0) _keepaliveMax = _server.getKeepaliveMax(); if (_keepaliveMax < 0 && _selectManager != null) _keepaliveMax = _selectManager.getSelectMax(); if (_keepaliveMax < 0) _keepaliveMax = 256; _admin.register(); } /** * binds for the watchdog. */ public QServerSocket bindForWatchdog() throws java.io.IOException { QServerSocket ss; if (_port >= 1024) return null; else if (_sslFactory instanceof JsseSSLFactory) { log.warning(this + " cannot bind jsse in watchdog"); return null; } if (_socketAddress != null) { ss = QJniServerSocket.createJNI(_socketAddress, _port); if (ss == null) return null; log.fine(this + " watchdog binding to " + _socketAddress.getHostName() + ":" + _port); } else { ss = QJniServerSocket.createJNI(null, _port); if (ss == null) return null; log.fine(this + " watchdog binding to *:" + _port); } if (! ss.isJNI()) { ss.close(); return ss; } if (_tcpNoDelay) ss.setTcpNoDelay(_tcpNoDelay); ss.setConnectionSocketTimeout((int) getSocketTimeout()); return ss; } /** * Starts the port listening. */ public void start() throws Exception { if (_port == 0) return; if (! _lifecycle.toStarting()) return; boolean isValid = false; try { bind(); postBind(); enable(); String name = "resin-port-" + _serverSocket.getLocalPort(); Thread thread = new Thread(this, name); thread.setDaemon(true); thread.start(); _suspendAlarm = new Alarm(new SuspendReaper()); _suspendAlarm.queue(60000); isValid = true; } finally { if (! isValid) close(); } } /** * Starts the port listening for new connections. */ void enable() { if (_lifecycle.toActive()) { _serverSocket.listen(_acceptListenBacklog); } } /** * Stops the port from listening for new connections. */ void disable() { if (_lifecycle.toStop()) { _serverSocket.listen(0); if (_port == 0) { } else if (_address != null) log.info(_protocol.getProtocolName() + " disabled " + _address + ":" + _port); else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?