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?
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.
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.
Without seeing your code and the exception stack trace, I cannot offer any help.
Jenny Sun
Greenhorn
Joined: Sep 12, 2009
Posts: 11
posted
0
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);
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
posted
0
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.