File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JBoss and the fly likes Servlet access to MBean in JBoss Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Products » JBoss
Reply Bookmark "Servlet access to MBean in JBoss" Watch "Servlet access to MBean in JBoss" New topic
Author

Servlet access to MBean in JBoss

Jenny Sun
Greenhorn

Joined: Sep 12, 2009
Posts: 11
I am wondering how the servlet running in the servlet container can access to the Hibernate sesion factory which was started by JBoss service MBean.

Some JBoss doc mentions the mapping between the servlet and stateless session bean can be done in web.xml/jboss-web.xml, but nowhere I can find the info . for service MBean. Is it doable?

Any advice will be highly appreciated.

-Jenny
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5536

Does this help (see bottom of page):
http://www.jboss.org/file-access/default/members/jbossas/freezone/docs/Server_Configuration_Guide/4/html/Writing_JBoss_MBean_Services-A_Standard_MBean_Example.html


JBoss In Action
Jenny Sun
Greenhorn

Joined: Sep 12, 2009
Posts: 11
Thanks much for your quick reply!

Hmm... jboss-service.xml descriptor is for application container how to start the specific MBean service. There is no information that I can find for how a servlet can access to this MBean.

-Jenny
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5536

In the servlet, first look up the MBeanServer (http://www.jboss.org/community/wiki/FindMBeanServer) then use its methods to lookup and access the MBean.
Jenny Sun
Greenhorn

Joined: Sep 12, 2009
Posts: 11
It is very helpful!! Thanks bunch!

The MBean method can be invoked through MBeanServer: MBeanServer.invoke(...) returns an Object which is returned by the operation. It was working if it is of type Object, but when to down cast the return type to what it should be, a class cast exception was thrown. I can't think of a reason why that happened.

-Jenny
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5536

Without seeing your code and the exception stack trace, I cannot offer any help.
Jenny Sun
Greenhorn

Joined: Sep 12, 2009
Posts: 11
Right, I did not include details.

Start JBoss, service MBean started, via jmx-console, I checked getter method named "SessionFac" return an object type of SessionFactory, which is desired. Below is an excerpt from jmx-console:
"List of MBean attributes:
Name|Type|Access|Value|Description
SessionFac|org.hibernate.SessionFactory|R|org.hibernate.impl.SessionFactoryImpl@b03101|MBean Attribute"

In servlet code, try to access MBean through MBeanServer (server); it is fine if no type cast:
Object obj = server.invoke(new ObjectName("<MBean name>"), "getSessionFac", null, null);

but an exception thrown "java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl", if cast to SessionFactory:
SessionFactory fac = (SessionFactory) server.invoke(new ObjectName("<MBean name>"), "getSessionFac", null, null);

I don't understand why. a wiki page mentions such weird behavior may happen especially in servlets, but I have yet to read all the details: http://www.jspwiki.org/wiki/A2AClassCastException
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5536

This type of error often occurs when you package a hibernate JAR file with your app. What JARs are in your app?
Jenny Sun
Greenhorn

Joined: Sep 12, 2009
Posts: 11
Thanks Peter!

The hibernate jar used is hibernate-3.2.6.ga.jar.

Why does it happen? Is there any doc explaining it?

Regards,
Jenny
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5536

It is a classloader issue. You can read about classloading in the JBoss AS docs and on the JBoss wiki. Also, JBoss in Action has a high-level description of classloading that is sufficient for you to understand the problem you ran into.
Jenny Sun
Greenhorn

Joined: Sep 12, 2009
Posts: 11
Thanks Peter!! It is helpful.

Now I understand the issue better - the class loader used by MBeanServer is org.jboss.mx.loading.UnifiedClassLoader; while from inside the servlet container, the one used is WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader

Is there any possible work around? OR it is just a bad approach trying to access SessionFactory from Servlet??
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Servlet access to MBean in JBoss
 
Similar Threads
Hessian in Jboss
jboss hibernate configuration
How to reference a JMX MBean ?
NullPointerException
jmx