one very weird scenario in extension

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
iphoneonly118
Posts: 7
Joined: 30 Nov 2009, 15:08

one very weird scenario in extension

Post by iphoneonly118 »

Hi,
I write some code like this, one class named as Test, which has one function like below:

Code: Select all

private static Test instance = null;
         public static Test getInstance() {
                if (instance == null){
	        System.out.println("create new Test instance");
		instance = new Test();
	}else{
                        System.out.println("have created");
                }
	return instance;
         }
Since both function and variable are static, this instance should only be created by once, but in my testing, it will be created several times, and I don't know why..
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Because each extension is loaded in a separate Class Loader.
Singletons won't work because every extension will have its own version.
In order to be able to work with singletons you will need to have your singleton class be loaded by the System Class Loader.

This can be done by adding your class to the server classpath.
In other words:
Classes that need to be seen globally should go in the main server classpath
Extension classes should not be loaded by the system Class Loader

More on this here:
http://www.smartfoxserver.com/docs/docP ... ons.htm#9a
Lapo
--
gotoAndPlay()
...addicted to flash games
iphoneonly118
Posts: 7
Joined: 30 Nov 2009, 15:08

Post by iphoneonly118 »

thanks!
iphoneonly118
Posts: 7
Joined: 30 Nov 2009, 15:08

Post by iphoneonly118 »

hi, a little bit confusion here. according to the document,
Make sure that the javaExtensions/ folder is not comprised in your classpath. By default SmartFoxServer includes this folder in the classpath so that all extension code is loaded at boot-time by the JVM system class loader. While this can be useful for sharing classes across multiple extensions (e.g. Singletons etc...) it will prevent dynamic reloading of the classes. If you remove the javaExtensions/ folder from the classpath, SmartFoxServer will use a separate class loader for each Extension and allowing dynamic reloading.
I remember I did not change the classpath after I installed SFS, that is to say, by default all my extension code and singleton code(my singleton code also put under javaExtensions/ folder) are loaded at boot-time by JVM system class loader, then why my singleton doesn't work...
I will go back double check this settings under my machine.
iphoneonly118
Posts: 7
Joined: 30 Nov 2009, 15:08

Post by iphoneonly118 »

I have checked, it works now:) I don't know why the default state of my SFS, the javaExtension is not under system class loader so it has different class loader version according to your comment.

One further question, I don't care about dynamic loading extension, I can accept loading all classes at boot time, but except the flexibility I lose, are there any more losts I may take?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

No, the only thing you "loose" is the dynamic reloading. That's all
Ciao
Lapo
--
gotoAndPlay()
...addicted to flash games
iphoneonly118
Posts: 7
Joined: 30 Nov 2009, 15:08

Post by iphoneonly118 »

Sorry for my bothering questions:) I got new one.
I am thinking about the memory waste, as my last post, I guess the other disadvantage is wasting some memory, right? One class that can be loaded later now is loaded earlier. But this should not affect the class instance, for example, if I put all my extensions under system class path, they are all loaded at boot-time, during server running, assuming there are 10 rooms which attatched one same extension, this will NOT cost me 10 times of memory wasting, am I right?
If the only memory wasting is just at loading stage, then I can take it.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Classes are loaded only when they are needed, they are not loaded at boot. Simply, the Class Loader learns from the classpath which classes it can load. Memory waste is not much of a problem here
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply