{"id":144,"date":"2015-01-13T17:53:35","date_gmt":"2015-01-13T17:53:35","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=144"},"modified":"2015-01-13T17:57:08","modified_gmt":"2015-01-13T17:57:08","slug":"the-singleton-solution","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/the-singleton-solution\/","title":{"rendered":"The singleton solution"},"content":{"rendered":"<p>One of the questions that often gets asked in our support forums is something along the lines of <em>&#8220;How do I create a singleton class where to store data that is global among all Extensions?&#8221;<\/em><\/p>\n<p><a href=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2015\/01\/singleton.png\"><img loading=\"lazy\" class=\" size-full wp-image-145 aligncenter\" src=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2015\/01\/singleton.png\" alt=\"singleton\" width=\"256\" height=\"135\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>If you&#8217;re not entirely familiar with how Extension loading works you can <a title=\"Extension Class Loading\" href=\"http:\/\/docs2x.smartfoxserver.com\/AdvancedTopics\/advanced-extensions#classLoading\" target=\"_blank\">consult this article<\/a> 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.<\/p>\n<p>There is however a simple deployment trick that can be employed.\u00a0Using the <strong>extensions\/__lib__\/\u00a0<\/strong>folder we can deploy a jar file with one or multiple classes that will be accessible from any Extension, even throughout multiple reloads.<\/p>\n<p>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 &#8211;&gt;\u00a0<strong>extensions\/__lib__\/<\/strong><\/p>\n<p>From a coding point of view nothing changes, which means that you don&#8217;t need to create a different project for your Singleton(s). You can keep them with your main Extension project\u00a0and 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the questions that often gets asked in our support forums is something along the lines of &#8220;How do I create a singleton class where to store data that is global among all Extensions?&#8221; This is a relatively complex subject in SmartFoxServer because each Extension is loaded in a separate Class Loader, thus making [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[31,7],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/144"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":2,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/144\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}