Preload Java classes / libraries at jar startup?

I wrote a thrift server in Java to take advantage of specific Java packages / libraries, but I'm not a java programmer

The problem is; I saw the server timeout for the first RPC call The subrequest is executed without any problems, and it only affects clients written in some (but essential) languages

My current idea is that the server times out when responding because it must load all the libraries required for the request on the first call Some thrift client implementations must handle timeouts better than others and may keep requests longer

When I first started Jar file, is there a way to @ r_ 419_ 2447 @ the library I'm using so that the first request won't be delayed?

Solution: I solve the problem (and some other problems) by increasing the timeout of thrift client However, I have implemented static / class Forname answers to help solve the problem thank you!

Solution

You can run the load before the server takes effect You have not specified how to load the server, class, and environment, but you can take advantage of the fact that class static initializers will run when class loads So if you are running from the "main" method, your class may look 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...
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>