Solved

To Unload a shared object in web application what should be path that should be passed in JSP or in business delegate class

Posted on 2014-04-02
11
678 Views
Last Modified: 2014-04-15
I have a web application using native methods. I want to know approach on how to unload the shared object.

I followed This example is good for standalone.

http://www.codethesis.com/blog/unload-java-jni-dll

In my web application I am intending to set the classloader and all classes and methods to null before running System.gc().

what should be the path of Java class in web application if the native method is called in a class that is called by JSP.

Class ca = cl.findClass("com.codethesis.example.A");
0
Comment
Question by:jgdvishnu
  • 5
  • 4
  • 2
11 Comments
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
You won't be able to do that in w web application because the class loading is handled by the servlet container (ie. tomcat, etc). And because the JSP's compiled class is loaded by the servlet container (using it's classloader) the class that uses the native library will also be loaded using that classloader.

I can't think of any other way to do it either. What is the requirement for wanting the native library to be unloaded?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
How do you load the dll in your web application? If you use the default classloader then mccarl is right, you can't do it (unless you gc the web application's classloader, but this would mean that you don't have a web application any more). If you use your own classloader then the approach described on the link should work for a web application as well.
0
 

Author Comment

by:jgdvishnu
Comment Utility
@mccarl  when I restart the application and hit the first request for JSP, time the System has already the library loaded and it gives error

Unable to load library due to: Native Library /usr/sap/libTest.so already loaded in another classloader

@girionis here is the logic...
// Loading through default class loader does not work
       static {
              try {
                     System.load("/usr/sap/libTest.so");            
              } catch (java.lang.UnsatisfiedLinkError exception) {
                     
              } finally {
              }
       }

Open in new window


Using custom class loader executed through different JSP to unload. It gives error that class not found which is being hit by another jsp on same application.


	public  void testLoadCheck() throws Exception {
        CustomClassLoader cl = new CustomClassLoader();
        Class ca = cl.findClass("com.insight.FedEx");
        Object a = ca.newInstance();
        Method p = ca.getMethod("testCall");// need to implement interface for method with parameter.
        p.invoke(a);
        p = null;
        ca = null;
        a = null;
        cl = null;
        System.gc();
    }

Open in new window


I heard this link will help. trying to investigate. Please confirm how this will translate into code...
Diagnosing Web Application OutOfMemoryErrors

I will be grateful.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
when I restart the application
Honestly, if I were you, I would spend my time elsewhere rather than trying to solve this issue. Because even if you were to fix this, it would still be my recommendation that rather than just restarting your application, I would restart the whole Tomcat (I pretty sure that's what you were using) container. It doesn't add much time at all to do a full restart compared to just restarting the webapp, but it ensures that you are always starting from a known good state.

A long time ago, I gave up on Tomcat's "feature" to be able to deploy, undeploy, start, stop, restart individual webapps within the container. It sounds good in theory, but when you actually start using it, issues like what you are seeing, and other things like memory leaks, etc start popping up. In a production environment, for me it is just too big a risk to take. The appliction may appear to be functioning normally after a "webapp restart" but you just never really know if maybe it is just a little bit closer to erroring out with a OutOfMemoryError or others. If you do a full restart of Tomcat, you can be sure that it always comes up in exactly the same state.


Having said all that, if you are still interested in getting this to work "just for the sake of it", then... One question that I had was, are you still having this class that loads the native library and the library itself in Tomcat's shared lib folder? Because if you do, it still should be loaded only once by a shared classloader that ISN'T associated with your webapp's classloader and so you shouldn't be getting the "already loaded" error.
0
 

Author Comment

by:jgdvishnu
Comment Utility
I want to try on tomcat because I need to make product that eventually runs on SAP PI server(on which I have no administrative control). So I do want to make it sturdy enough that it runs on tomcat.

with Shared library it does not recognise the classes in the jar and it gives warning on server start up as shown below-

WARNING: Problem with directory [/home/adm/tomcat/apache-tomcat-6.0.39/bin/$CATALINA_HOME/shared/lib/libTest.jar:$CATALINA_HOME/shared/lib/libTest.so:$CATALINA_HOME/shared/lib/libTest1.so], exists: [false], isDirectory: [false], canRead: [false]

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
Comment Utility
It appears that you possibly didn't read (didn't see) my last comment in another question of yours...

http://www.experts-exchange.com/Programming/Languages/Java/J2EE/Q_28383387.html#a39960795

Did you fix the format of the entry that you added to the catalina.properties file?

Instead of using $CATALINA_HOME you need to use ${catalina.home} and also you need a comma (,) between each path entry not colon (:). Although as I also said in that other comment, you don't add the .so files to this entry, just the .jar file so you should only have one entry anyway, and no need for a comma at all.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
I want to try on tomcat because I need to make product that eventually runs on SAP PI server
And in regards to that, SAP PI probably has a totally different classloading arrangement to Tomcat so the problems that you are facing in Tomcat may not even exist in SAP (and also you might find different problems in SAP that you don't have in Tomcat)

They are different products running different environments and so using one to predict/test how your code will function on the other is not going to amount to much (at least when you are talking about low level details like classloaders)
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 250 total points
Comment Utility
System.load("/usr/sap/libTest.so");

This piece of code will use the default system classloader (which is specific to each JVM implementation).

It gives error that class not found

In your classloader you need to specify the parent classloader that loaded the com.insight.FedEx class. If you don't specify this classloader then your CustomClassLoader should look in the path that you have the class com.insight.FedEx, otherwise you will end up with a class not found exception.

But I still don't understand what the requirement is for wanting the dll unloaded.
0
 

Author Comment

by:jgdvishnu
Comment Utility
@mccarl - I did get the shared library jar to work. Thanks. I am not clear on lifecycle of Customclassloader and how it works.

@girionis the need to unload dll is to restart only one application and not the whole application server. the second time when same aplication is loaded it gives error that the dll(or shared library in UNIX) is already loaded.
0
 

Accepted Solution

by:
jgdvishnu earned 0 total points
Comment Utility
Instead of loading and unloading the library I just put it on shared library location as suggested by mccarl.
This does not error on Tomcat. Though this approach does not work on SAP PI server. I have posted my comments on SAP side and waiting for response.


Is there a shared library (settings) page on SAP PI server to handle native library
0
 

Author Closing Comment

by:jgdvishnu
Comment Utility
Using Shared Library is better solution then loading and unloading of classes through the classloader. Through Classloader I will need to handle and force garbage collection and Garbage Collection is done by system and cannot be forced so may not work in the end.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now