2

We have connectors in server.xml in tomcat like this:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" 
           redirectPort="8443" maxConnections="500" maxThreads="150"/>

As far from my concern, I guess these connectors are loaded as objects when the tomcat is started.

So can we get the attributes like port, maxConnections, connectionTimeout in my java web application which is running in that tomcat as Objects using any library ?

It would be nice if I get some spark here.

svarog
  • 9,477
  • 4
  • 61
  • 77
Arun Kumar G R
  • 178
  • 2
  • 13

1 Answers1

7

You can get the server and its configurations via MBeanServer.

Interface MBeanServer: This is the interface for MBean manipulation on the agent side. It contains the methods necessary for the creation, registration, and deletion of MBeans as well as the access methods for registered MBeans. This is the core component of the JMX infrastructure.

You can use the following code:

MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
int port = server.getPort();
Chinmay jain
  • 979
  • 1
  • 9
  • 20
  • Good to hear , But can you give more information about MBeanServer ? like to which package does it belong ? and explain your snippet too ? – Arun Kumar G R Aug 10 '17 at 06:28
  • I've mentioned it in the description of MBean Server. It belongs to the `javax.management` package. – Chinmay jain Aug 10 '17 at 06:47
  • yes I got it, It includes two packages , source : https://stackoverflow.com/questions/6833947/org-apache-catalina-serverfactory-getserver-equivalent-in-tomcat-7 But the thing is How do I get other attributes , I mainly need maxConnections . – Arun Kumar G R Aug 10 '17 at 06:51
  • 2
    I got a perfect sample from here https://github.com/cqyijifu/watcher/blob/master/watcher-http/src/main/java/com/yiji/framework/watcher/http/metrics/WebContainerMetrics.java with help of your comment. Thanks ! I will mark this as answer for now. – Arun Kumar G R Aug 10 '17 at 08:50
  • Glad you find it. Cheers! – Chinmay jain Aug 10 '17 at 08:54