Page 1 of 1

ClassLoader issues with room extensions

Posted: 08 Oct 2011, 17:14
by harlanji
Hello,

I am developing my first extension, which I have designed to include a Zone extension and various extensions for the Room level that implement different logic for different types of Rooms.

Essentially I want my Spring context with DB connections/ws clients/remote credentials and the like to live on my Zone level extension, and then have a different Room extension for each type of game, that can navigate up to the Zone level to get at the single application context. SFS2X api provides for this without doing anything tricky:

Code: Select all

// from within room-level request handler
MyZoneExt zoneExt = (MyZoneExt)getParentExtension().getParentRoom().getZone().getExtension();
zoneExt.getSomeResource();
This seemed like a clean way to separate my logic and make an extensible server that can have additional types of games added via new room-level extensions. But there's a funny catch when I run the above code:
Exception: java.lang.ClassCastException
Message: net.example.mygame.sfs.MyZoneExt cannot be cast to net.example.mygame.sfs.MyZoneExt
(no, that's not a typo!). The problem is that the two versions of the same class were loaded in different ClassLoader instances... my guess is that when we pass the extension directory into CreateExtensionSettings, it creates a new full extension class loader.

The behavior that makes sense to me is to use the same ClassLoader that the zone level extension used, because after all, how else can one call createRoom without having written a zone-level extension?

I am proposing a way to specify that the same ClassLoader should be used; or changing default behavior if using separate ClassLoaders for the same extension almost never makes sense--I think the only time it would is with singletons.



Best regards,

Harlan

Posted: 10 Oct 2011, 07:16
by Lapo
my guess is that when we pass the extension directory into CreateExtensionSettings, it creates a new full extension class loader.
Yes that's what happens
We explain all the details here:
http://docs2x.smartfoxserver.com/Advanc ... assLoading

Posted: 13 Oct 2011, 02:27
by harlanji
Lapo wrote: Yes that's what happens
We explain all the details here:
http://docs2x.smartfoxserver.com/Advanc ... assLoading
Thanks, I must've glossed over that part the first time. I can see a solution now, but I am not completely convinced that having one ClassLoader per instance of a Room with an extension is a scalable approach.

Can you tell me about the reasons behind that decision (as MMO game development is new to me)? Have you evaluated the memory footprint of having so many ClassLoaders?

Thanks and best regards,

Harlan