Tomcat doesn't support this.
In the other thread, I suggested writing an MBean to
load/unload the library.
Ok. I will try to find that thread here in the forums.
If I load the library in the MBean, can all my servlets access it then ?? Or do they have to go through the Bean ?
BTW what is a MBean ? Where can i find more info about how to write a mbean ??
An MBean is a managed bean. They are used in the
core JBoss architecure.
If you look at jboss.jcml, these are all mbeans.
You can use the mbean to load a class that wraps the
native implementation. The mbean is loaded at server
start, i.e. the code only happens once.
The servlets can then reference that class, probably
through some sort of static factory method?
The trouble with Tomcat is that when you redeploy,
you get a new classloader, so the class is reloaded.
But the old class is probably still on the heap waiting
to be garbage collected.
I am not an expert on JNI, however when I investigated I got the strong impression that java goes to great lengths to prevent you from loading a native library twice in the same vm under any circumstances. I would certainly like to know if I am wrong, preferably by means of an example;-)
I had a similar problem with a servlet that I was developing.
I split the JNI stuff off into another servlet, which needed no further development. The servlet which was being actively developed then consumed the jni based one.
The second servlet could then be bounced independently of the JNI code.
I did look around for a way to unload native libs so I could load in init() and unload in destroy() - but could not find one....
Perhaps I missed something ?
Try putting your classes that use JNI into lib/ext (2.x) or lib/ (3.x). This will allow your servlets & other components to see the classes, but not have problems realoading.
If you want to reload the classes which load JNI libraries then you are hosed. If this is the case consider rearchitecting to put JNI into seperate class library which can be detached from the app, thus allowing it to live in lib/ext or lib/.
Another question about MBeans. Can all users add MBeans to the JBoss ? Since we're only a user among several other user at our web host, the host admins wont let any user do any special things that isnt allowed outside autoconfiguring.
Im new to MBeans and Beans (as well), is there any good tutorial on how to write a small bean that wraps the JNI code, and also how to call it ??
Does the MBean have to exist when the server is started, or can it be added later (only once) ?
Try the JMX Book that Juha wrote, it is very informative & helpful.