ClassLoader issues with room extensions

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Post Reply
harlanji
Posts: 8
Joined: 15 Sep 2011, 01:50

ClassLoader issues with room extensions

Post 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
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
harlanji
Posts: 8
Joined: 15 Sep 2011, 01:50

Post 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
Post Reply