Arun Kumar G R
Arun Kumar G R

Reputation: 198

How to get tomcat connector attributes in a java web app programatically

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.

Upvotes: 2

Views: 2644

Answers (1)

Chinmay jain
Chinmay jain

Reputation: 999

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();

Upvotes: 7

Related Questions