Not through the request object, after the system starts, get the tomcat port

private static String getPortByMBean() {
		String portStr = null;;

		MBeanServer mBeanServer = null;
		ArrayList<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
		if (mBeanServers.size() > 0) {
			mBeanServer = mBeanServers.get(0);
		}
		if (mBeanServer == null) {
			throw new IllegalStateException("The associated MBeanServer in the JVM was not found.");
		}
		Set<ObjectName> objectNames = null;
		try {
			objectNames = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
		} catch (MalformedObjectNameException e) {
			e.printStackTrace ();
		} catch (NullPointerException e) {
			e.printStackTrace ();
		}
		if (objectNames == null || objectNames.size() <= 0) {
			throw new IllegalStateException("No associated MBeanServer found in JVM: " + mBeanServer.getDefaultDomain() + object name in ".");
		}
		try {
			for (ObjectName objectName : objectNames) {
				String protocol = (String) mBeanServer.getAttribute(objectName, "protocol");
				if (protocol.equals("HTTP/1.1")) {
					portStr = String.valueOf(mBeanServer.getAttribute(objectName, "port"));
					break;
				}

			}
		} catch (AttributeNotFoundException e) {
			e.printStackTrace ();
		} catch (InstanceNotFoundException e) {
			e.printStackTrace ();
		} catch (MBeanException e) {
			e.printStackTrace ();
		} catch (ReflectionException e) {
			e.printStackTrace ();
		}
		System.out.println("Current tomcat container port >>>"+portStr);
		return portStr;
	}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326966888&siteId=291194637