4

I have my web services jar file deployed under webapps\nyx\WEB-INF\services in my tomcat server. Now I am trying to get no of active sessions using below code inside a web service method.

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("Catalina:type=Manager,context=/nyx/services,host=localhost");
Object activeSessions =mBeanServer.getAttribute(objectName,"activeSessions");

But this gives me Instance not found exception.
Can someone help me with the value for the context attribute?

Shelly
  • 681
  • 4
  • 20
  • possible duplicate of https://stackoverflow.com/questions/4069444/getting-a-list-of-active-sessions-in-tomcat-using-java – some IT dood Jan 18 '19 at 10:50
  • I checked that. But the problem here is what is the context I should use when the web services are deployed under webapps\nyx\WEB-INF\services directory – Shelly Jan 18 '19 at 11:02

1 Answers1

0

Try to refer to this SO question, reading your code the first thing that i think is try using JMX (Java Management eXtension)

Something like this:

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
try(JMXConnector jmxc = JMXConnectorFactory.connect(url)) {
  MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  ObjectName mbeanName = new ObjectName("Catalina:type=Manager,context=/,host=localhost");
  Object value = mbsc.getAttribute(mbeanName, "activeSessions");
}

EDIT

If you need to retrieve the number of session locally your code should be fine, try with your code but getting the context at runtime. Override the init method:

@Override
public void init(final ServletConfig config) throws ServletException {
    context = config.getServletContext().getContextPath();
}

Then pass it as ObjectName parameter:

ObjectName objectName = new ObjectName("Catalina:type=Manager,context="+context+",host=localhost");
Roberto Manfreda
  • 1,664
  • 2
  • 13
  • 33
  • Hi, Thank you for your response. I tried this but this gives me "java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is " exception. What should be the ip and the port I should use if my server ip is y31 and port number 3389. – Shelly Jan 18 '19 at 10:58
  • in order to execute in a right way jmx you must pass some addiotional params... Maybe the exception can be thrown because you are omitting `-Dcom.sun.management.jmxremote.authenticate=false` or some other parameter – Roberto Manfreda Jan 18 '19 at 11:23
  • Ohh, thank you. I ll check that. Bdw in config.getServletContext() I couldnt find the method getContextPath(); – Shelly Jan 18 '19 at 11:26
  • Refer tho this https://stackoverflow.com/questions/35837285/different-ways-to-get-servlet-context in order to get the ServletContext depending on your needs – Roberto Manfreda Jan 18 '19 at 11:31
  • Thank you very much for your help. But still I couldnt come up with a solution. – Shelly Jan 18 '19 at 12:25
  • Here I am deploying my web services in a subdirectory under WEB-INF. Will that be the problem – Shelly Jan 18 '19 at 12:46
  • Maybe... Can you try changing your deploy path? – Roberto Manfreda Jan 18 '19 at 14:27