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 类/库?
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01