在jar启动时预加载Java类/库? [英] Preloading java classes/libraries at jar startup?
问题描述
我已经用Java编写了Thrift服务器以利用特定的Java包/库,但是我不是Java程序员.
I've written a Thrift server in Java to take advantage of a specific Java package/library, but I'm not a java programmer.
问题是;我看到对服务器的第一个RPC调用超时. Subsequest请求的执行没有任何问题,它仅影响以某些(但必不可少)语言编写的客户端.
The problem is; I'm seeing a time-out for the first RPC call to the server. Subsequest requests are executed without any issues, and its only affecting clients written in certain (but essential) languages.
我当前的想法是服务器响应超时,因为在第一次调用时,服务器必须加载请求所需的所有库.一些Thrift客户端实现必须比其他实现更好地处理超时,这可能会使请求打开的时间更长.
My current thought is that the server times-out on the response because upon first call it has to load all the libraries required for the request. Some Thrift client implementations must be handling the time-out better than others, possibly keeping the request open a little longer.
在Java中,有没有一种方法可以在我第一次启动.jar文件时预加载我正在使用的库,这样就不会延迟第一个请求?
Is there a way in java to preload the libraries I'm using when I first initiate the .jar file so there isn't a delay on the first request?
解决方案::我通过增加节俭客户端的超时来解决此问题(并提出了其他一些问题).但是,我也实现了static/Class.forName答案来帮助解决问题,效果很好.谢谢!
Solution: I got around the problem (and some further ones raised) by increasing the timeout from the thrift client(s). However, I've implemented the static/Class.forName answer also to help things along, which works great. Thanks!
推荐答案
您可以在服务器启动之前运行加载.您尚未指定如何加载服务器,类以及环境是什么,但是您可以利用这样的事实,即在加载类时将运行类静态初始值设定项.因此,如果您使用的是"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类/库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!