• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet access to MBean in JBoss  RSS feed

 
Jenny Sun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jenny Sun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing your code and the exception stack trace, I cannot offer any help.
 
Jenny Sun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!