欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获得tomcat 的并发连接数

程序员文章站 2024-02-27 16:49:39
...

获得系统当前时刻的连接数。

public  class MBeans {
 
	private final MBeanServer mbeanServer;
 
	MBeans() {
		this(getPlatformMBeanServer());
	}
 
	private MBeans(MBeanServer mbeanServer) {
		super();
		this.mbeanServer = mbeanServer;
	}
	static MBeanServer getPlatformMBeanServer() {
		return ManagementFactory.getPlatformMBeanServer();
	}
	Set<ObjectName> getTomcatThreadPools() throws MalformedObjectNameException {
		return mbeanServer.queryNames(new ObjectName("*:type=ThreadPool,*"), null);
	}
 
	Set<ObjectName> getTomcatGlobalRequestProcessors() throws MalformedObjectNameException {
		return mbeanServer.queryNames(new ObjectName("*:type=GlobalRequestProcessor,*"), null);
	}
 
	Object getAttribute(ObjectName name, String attribute) throws JMException {
		return mbeanServer.getAttribute(name, attribute);
	}
}
public class TomcatInformations implements Serializable {
 
	private static final boolean TOMCAT_USED = System.getProperty("catalina.home") != null;
 
	@SuppressWarnings("all")
	private static final List<ObjectName> THREAD_POOLS = new ArrayList<ObjectName>();
	@SuppressWarnings("all")
	private static final List<ObjectName> GLOBAL_REQUEST_PROCESSORS = new ArrayList<ObjectName>();
 
	private final String name;
	private final int currentThreadCount;
	private static int currentThreadsBusy=0;
	private static int threadsBusy=0;
	
 
	private TomcatInformations(MBeans mBeans, ObjectName threadPool) throws JMException {
		super();
		name = threadPool.getKeyProperty("name");
		currentThreadCount = (Integer) mBeans.getAttribute(threadPool, "currentThreadCount");
		currentThreadsBusy = (Integer) mBeans.getAttribute(threadPool, "currentThreadsBusy");
	//	System.out.println("currentThreadCount线程总数:"+currentThreadCount);
		//System.out.println("currentThreadsBusy繁忙线程:"+currentThreadsBusy);
	}
 
	public static List<TomcatInformations> buildTomcatInformationsList() {
		if (!TOMCAT_USED) {
			return Collections.emptyList();
		}
		try {
			synchronized (THREAD_POOLS) {
				if (THREAD_POOLS.isEmpty() || GLOBAL_REQUEST_PROCESSORS.isEmpty()) {
					initMBeans();
				}
			}
			final MBeans mBeans = new MBeans();
			final List<TomcatInformations> tomcatInformationsList = new ArrayList<TomcatInformations>(
					THREAD_POOLS.size());
			for (final ObjectName threadPool : THREAD_POOLS) {
		    //并发线程
			//在通过浏览器访问Tomcat服务器的Web应用时,HTTP负责建立连接
			//	System.out.println("threadPool:"+threadPool);
		       String tpool= String.valueOf(threadPool);
		       boolean bool = tpool.contains("http");
		       if(bool) {
		    	   threadsBusy = (Integer) mBeans.getAttribute(threadPool, "currentThreadsBusy");
		       }
				tomcatInformationsList.add(new TomcatInformations(mBeans, threadPool));
			}
			return tomcatInformationsList;
		} catch (Exception e) {
			return Collections.emptyList();
		} 
	}
 
	private static void initMBeans() throws MalformedObjectNameException {
		final MBeans mBeans = new MBeans();
		THREAD_POOLS.clear();
		GLOBAL_REQUEST_PROCESSORS.clear();
		THREAD_POOLS.addAll(mBeans.getTomcatThreadPools());
		GLOBAL_REQUEST_PROCESSORS.addAll(mBeans.getTomcatGlobalRequestProcessors());
	}
 
	public static int getCurrentThreadsBusy(){
		buildTomcatInformationsList();
		return threadsBusy;
	}
}

 

相关标签: 并发连接