在tomcat中运行MBean

时间:2014-03-15 08:06:02

标签: java tomcat servlets jmx mbeans

我在我的Web应用程序中注册了几个MBean(在tomcat7下运行),我可以在jconsole上看到它们。正在调用公开的方法。

我的app中有一些需要数据库连接的实用工具,他们可以从dbcp请求连接。如果从servlet或石英作业调用,这些实用程序可以正常工作。但是当我尝试从MBean的公开方法调用这些实用程序时,它无法从dbcp获得连接。似乎mbeans作为独立程序运行,并且不在servlet上下文中运行。

我的问题是,这是我在这里失踪的东西吗?有可能吗?怎么样?


  • 在jconsole上,mbeans列在tomcat进程下。
  • 我这样得到mbean服务器 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

我如何注册mbean -

AdminService adminService = new AdminService();
ObjectName admin = new ObjectName("AdminS:type=AdminServiceMBean");
mbs.registerMBean(adminService, admin);

我如何从dbcp(ConnectionManager)获得连接

InitialContext initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/adminService");
c = ds.getConnection();

0 个答案:

没有答案