Preloading java classes/libraries at jar startup?(在 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...
}
}
这篇关于在 jar 启动时预加载 java 类/库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 jar 启动时预加载 java 类/库?


基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01