在 jar 启动时预加载 java 类/库?
在对服务器的第一次 RPC 调用期间发生超时,但 subsequest 请求成功.服务器响应超时,因为在第一次调用时它会加载处理请求所需的库.由于此延迟,一些客户端超时.虽然可能会增加客户端的超时延迟,但我想尽量减少类加载对应用程序响应能力的影响.
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.
您将如何预加载 Java 类文件,以便在首次运行应用程序的 .jar
文件时,类加载不会在第一次调用时引入延迟?
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?
推荐答案
您可以在服务器上线之前运行负载.您尚未指定如何加载服务器、类以及环境是什么,但您可以利用类静态初始化程序将在加载类时运行这一事实.所以,如果你从main"方法运行,你的类可能看起来像这样
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...
}
}
相关文章