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 类/库?
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01