glassfish/tomcat Java Server load class once

笑着哭i 提交于 2020-01-06 14:01:45

问题


I am using glassfish v2 but I've had the same problem in tomcat.

Once the server starts, I see:

sudo jmap -histo:live 14127 | grep KVStore
6222:             1             24  xxx.xxx.KVStore

After a while of usage, I get:

sudo jmap -histo:live 14127 | grep KVStore
7240:             1             24  xxx.xxx.KVStore
7360:             1             24  xxx.xxx.KVStore

While I'm guessing this has something to do with how glassfish scales out, this is really problematic because I use the KVStore as an in memory storage class.

It is a singleton class with a static hashtable and getters and setters:

class KVStore
    public static KVStore kvstore;
    public Hashtable hash;

Now the problem, is that with one class. When I call store and get with a servelet I receive the object just fine. But, with two classes or KVStore, the servelet alternates between the two and never returns correctly when the calls are made consecutively.

Now I really need to store the object in memory. How do I force this class to be loaded only once? Or create a Globally accessable hashtable?

One last thing, I am deploying a jruby warbled war file. If there is no actual way this could be happening, let me know and I'll look into that instead.


回答1:


So this is not a complete answer because I still have bugs:

https://stackoverflow.com/questions/5785531/jruby-typeerror-assigning-non-exception-to

But using jmap I have made a single singleton.

How to create a true singleton in java?

specifically:

http://surguy.net/articles/communication-across-classloaders.xml

which is a hack.




回答2:


Ok, fml. Most likely you want this:

config.webxml.jruby.min.runtimes = 1
config.webxml.jruby.max.runtimes = 1

in your warble.rb and call it good.



来源:https://stackoverflow.com/questions/5782133/glassfish-tomcat-java-server-load-class-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!