简体   繁体   中英

jetty get webapp list

I need to get some server properties from server to my servlet in init method, before any request execute(in init method). Actually I neet get list on all working on this server connectors, all working webapps and, most important, - port numbers or connectors. Desired jetty version - up to 8 version including.

So I need something like org.eclipse.jetty.server.Server , but not for embdeded, but from existing server, on wich my servlet is running. This information shuld be on jetty as far as webapp deployer operating with this info. But I can't find where.

Enabling JMX in Jetty's start.ini would allow you to use code similar to the following:

final MBeanServer mBeanServerConnection = ManagementFactory.getPlatformMBeanServer();
final String[] portAttribute = new String[] {"port"};

// Jetty 9 MBeans
final ObjectName webappcontext9 = new ObjectName("org.eclipse.jetty.webapp:context=*,type=webappcontext,id=*");
final Set<ObjectName> webappcontexts9 = mBeanServerConnection.queryNames(webappcontext9, null);
for (final ObjectName objectName : webappcontexts9) {
    System.out.println(objectName.getKeyProperty("context"));
}

final ObjectName serverconnector9 = new ObjectName("org.eclipse.jetty.server:context=*,type=serverconnector,id=*");
final Set<ObjectName> serverconnectors9 = mBeanServerConnection.queryNames(serverconnector9, null);
for (final ObjectName objectName : serverconnectors9) {
    System.out.println("listening port for " + objectName.getCanonicalName() + " is " + mBeanServerConnection.getAttributes(objectName, portAttribute).asList().get(0).getValue());
}

// Jetty 8 and 7 MBeans
final ObjectName webappcontext8 = new ObjectName("org.eclipse.jetty.webapp:type=webappcontext,id=*,name=*");
final Set<ObjectName> webappcontexts8 = mBeanServerConnection.queryNames(webappcontext8, null);
for (final ObjectName objectName : webappcontexts8) {
    System.out.println(objectName.getKeyProperty("name"));
}

final ObjectName serverconnector8 = new ObjectName("org.eclipse.jetty.server.nio:type=selectchannelconnector,id=*");
final Set<ObjectName> serverconnectors8 = mBeanServerConnection.queryNames(serverconnector8, null);
for (final ObjectName objectName : serverconnectors8) {
    System.out.println("listening port for " + objectName.getCanonicalName() + " is " + mBeanServerConnection.getAttributes(objectName, portAttribute).asList().get(0).getValue());
}

Of course you'll need to configure Jetty to load the web application that contains this code last or it won't get the complete list of other web applications loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM