This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Servlet that reload any class file in server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Servlet that reload any class file in server" Watch "Servlet that reload any class file in server" New topic
Author

Servlet that reload any class file in server

Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
Hi,
We all know that one of the major problems in using application servers is that we need to restart it each time we make a change to a class file, for it to get reloaded. I know that in some application servers we could reload a class if we put a modified version of the class file in some place. But what if the application server is remote? or my application server does not support this feature.
So I want to know if it's possible to write a servlet that will let me upload any class file from the local machine and reload it in it's current JVM. Since the servlet runs in the same JVM as the application server the change should get reflected in the application server.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Just get a better application server!

In modern JVMs, for a class to be unloaded its ClassLoader must be garbage collected; for that to happen, all objects of all classes loaded by the ClassLoader must be unreachable. The way a decent application server manages class reloading is by loading applications each in their own ClassLoader, and when an application is to be unloaded, it simply discards the ClassLoader and creates a new one, which can load new copies of the classes, allowing the old ones to be collected.

I hope you understand why the scenario you describe is not possible -- unless your servlet itself implements an entire application server, and loads and reloads applications by itself!


[Jess in Action][AskingGoodQuestions]
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
Thanks, I got it. There is one more question related to this.
In what situations is the redefining possible?
Is redefining possible if I add a method in the class? what if the method is private?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

What redefining is that, now? A new ClassLoader can load a class that's identical to one loaded by another ClassLoader, so perhaps the answer to your question is just "yes" -- but I'm not exactly sure what you're asking.
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
Thanks. Please tell me if the construction of such a servlet is possible if I include the following constraints.

1. I don't have to redefine all the class but only a set of them
2. The first thing I will do after starting the application server is run this servlet.
3. Let this servlet load all the class files, using a new class loader.
4. Each time, when I need to redefine these classes, I will again upload all of them. Now let the servlet drop the class loader and create a new classloader with these new classes.

would that be feasible?
If it is not possible then how does JPDA work? I've seen that when I connect to a remote oc4j server through remote debugging option in JDeveloper, and compile a class it automatically gets redefined in the server. But the problem with this approach is that it takes too much time for connecting to remote server. I just want the redefining part of it.

PS: please tell me it's possible
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Yes, what you've described is possible, but you've left out how you would get the rest of the server to use those classes (unless we're actually only talking about the server running this one servlet, which is just loading and unloading classes for its own use.) As a general rule, there's no way for one servlet to tell its servlet container "here, use these classes for other web apps".
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
oh no . I'll tell you my exact problem, pls tell me if there is any better solution than what iam thinking. The problem for me is that my application server HAS to run in a remote location. this is because the database server is in the remote location and the business components make a lot of talking to the db. Thus, It's never possible to have the application server locally running. I've tried to run it locally and the application is slow as hell.
So we have a application server (oc4j) , that is running remotely, in the same network as the db , with a web interface to start, stop and put classes in a prepend path. So each time I change something, like putting a log message, I have to put the classes in the prepend path and then bounce the server. Bouncing the server takes anywhere between 10 to 15 mts, since there is a lot of initalization going on. This is really cutting off my productivity. Is there a better way. Am I missing something?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ernest Friedman-Hill:
Just get a better application server!

In modern JVMs, for a class to be unloaded its ClassLoader must be garbage collected; for that to happen, all objects of all classes loaded by the ClassLoader must be unreachable. The way a decent application server manages class reloading is by loading applications each in their own ClassLoader, and when an application is to be unloaded, it simply discards the ClassLoader and creates a new one, which can load new copies of the classes, allowing the old ones to be collected.

I hope you understand why the scenario you describe is not possible -- unless your servlet itself implements an entire application server, and loads and reloads applications by itself!


Hmmm...
In Tomcat, if you want to reload a servlet class, you need to reload the entire application (one classloader per webapp).
If, on the other hand, you change a JSP, Jasper regenerates the servlet code, the servlet gets compiled, and then gets loaded.
This happens without the entire application being reloaded.
In addition, a instance variable in one jsp (say a counter like i++) is not affected by changes to other JSPs.

Kaushik Sathupadi,
If this is really important to you, you may want to poke around in Tomcat's classloader source code to see how they're doing this.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Ben Souther:
you may want to poke around in Tomcat's classloader source code to see how they're doing this.


Well, I'll tell you right now: there's a ClassLoader for each webapp, and then another ClassLoader for each JSP which delegates to the webapp ClassLoader.

Anyhow, although I'm sure there's a better solution, it sounds to me as if you could, with some effort, do something like this. The main servlet would delegate all of its work to other classes that it loaded itself through its own ClassLoader. When it needed to reload the classes, it could simply create a new ClassLoader and do so.
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
you change a JSP, Jasper regenerates the servlet code, the servlet gets compiled, and then gets loaded

.. Not only that, as I mentioned, to the same application server(that runs remotely), i can connect through JPDA with a locally running version of jdeveloper and redefine the classes. But the problem is to make the connection it takes around 20-30 mts. But the redefining part itself is fast. and i can redefine a servlet.. a EJB .. or anything else.

The main servlet would delegate all of its work to other classes that it loaded itself through its own ClassLoader. When it needed to reload the classes, it could simply create a new ClassLoader and do so.

I don't understand this. Would it be possible that once the servlet has redefined the class files, the next access to that classes from anywhere will get the newly loaded class? (Pls. don't ask me to re-structre the entrie application for this. The designers would kill me.)

If that's possible, I am ready to do the home work. Where should I start reading. Ben, like you said, I could go through the Tomcat class loader API, but It's going to take a lot of time. Are there other articles that I could help me get started, especially for redefining files on an application server.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Kaushik Sathupadi:
Pls. don't ask me to re-structre the entrie application for this. The designers would kill me.


I think in that case you're out of luck. Have you looked at just getting a better app server?
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
I think in that case you're out of luck. Have you looked at just getting a better app server?

-- I don't have that option. Iam sorry that I cannot really tell you why for official reasons, but just that even if there was a better app server for our purpose, I cannot make the decision of changing it.

Anyway thaks for your precious help. And anytime, if any Idea strikes you regarding this, please mail me at kaushik.sathupadi@gmail.com.

P.S: Now that I think of it, I could use my time that is required for bouncing the server, to check my mail, chatting etc. Just trying to be optimistic. But pls don't let that drift you, any body reading this post, pls mail me if you have an Idea. There are dozens of developers here bouncing the sever each time they make a change like a tennis ball. And if a solution is found, could save a lot of developer time. Which means a lot of money saved for the company.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Kaushik Sathupadi:

Anyway thaks for your precious help. And anytime, if any Idea strikes you regarding this, please mail me at kaushik.sathupadi@gmail.com.


We'd prefer that you keep the conversation here.
See this to find out why.
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
We'd prefer that you keep the conversation here.

- Sure we can. But I don't see a watch of this thread link anywhere, that would mail me automatically if any updations are made to this thread. Anyway, pls update this thread if any idea strikes you regarding this. (Am I right now?)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet that reload any class file in server
 
Similar Threads
Can Tomcat automatically reload a servlet?
manipulating java code within program
Auto reloading of any edited class/servlet
Servlets and JSP?
Property File Reload problem