Preloading java classes/libraries at jar startup?
Asked Answered
P

3

12

A time-out occurs during the first RPC call to a server yet subsequest requests succeed. The server times-out on the response because upon first call it loads the libraries needed to handle the request. Due to this delay, some clients time out. Although it is possible to increase the time-out delay in the client, I'd like to minimize the impact that class loading has on the application's responsiveness.

How would you preload Java class files so that when the application's .jar file is first run class loading does not introduce a delay on the first call?

Ppm answered 24/3, 2009 at 14:51 Comment(1)
You might want to take a look at some existing java class preloader.Cathode
C
5

You could run a load before the server becomes live. You haven't specified how you're loading the server, the classes, and what the environment is, but you can take advantage of the fact that a class static initializer will run when the class is loaded. So, if you're running from a "main" method, your class could look something like this

public class Foo {

   static {
     //this will be run when the class is loaded
     try { Class.forName("fully.qualified.class.name.that.i.want.to.Load"); }
     catch ...
   }

   public static void main (string args[])
   {
    //run my server...
   }
}
Cock answered 24/3, 2009 at 15:42 Comment(1)
This solution is kind of fragile. A better solution is to do a dummy action before hand that will load all the needed classes. Something like sendRequest("/dev/null") would be good. But the basic idea is okay.Thirteenth
D
3

One thing you might want to try is writing a simple client inside of the Java server itself. This client does nothing but call some method in the server when it starts up, forcing the classes to be loaded. After this little client gets a result (or callback), then it puts the server into an "accessible by the outside world" state.

Dextrocular answered 24/3, 2009 at 15:0 Comment(0)
G
2

I suggest faking a connection just before opening up the server. That will ensure that (most) relevant lazy initialisation will have been performed.

Glennieglennis answered 24/3, 2009 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.