One of the questions that often gets asked in our support forums is something along the lines of “How do I create a singleton class where to store data that is global among all Extensions?”
This is a relatively complex subject in SmartFoxServer because each Extension is loaded in a separate Class Loader, thus making it more difficult to create a singleton that can be shared.
If you’re not entirely familiar with how Extension loading works you can consult this article for a quick overview. Part of these rules are essentially inherited from the Java Virtual Machine (JVM) architecture and, even if they might seem a bit convoluted, they help with reloading new code at runtime without stopping or restarting the server.
There is however a simple deployment trick that can be employed. Using the extensions/__lib__/ folder we can deploy a jar file with one or multiple classes that will be accessible from any Extension, even throughout multiple reloads.
In other words this will not change the way in which you deploy your Extension in SmartFoxServer 2X, however you will need to export the Singleton class(es) to a separate jar file that will be deployed to –> extensions/__lib__/
From a coding point of view nothing changes, which means that you don’t need to create a different project for your Singleton(s). You can keep them with your main Extension project and just deploy them differently, as described. Most Java IDEs will allow you to export a number of selected classes to a specific jar file, which can then be deployed as we have discussed.