Page 1 of 1

Saving rooms on server shutdown

Posted: 14 Jan 2014, 22:44
by efsnet
Hello,

In an attempt to save all rooms when the server is shutdown I added the following code to the destroy() method of the extension:

Code: Select all

      try {
         trace("Saving rooms...");
         getParentZone().getRoomPersistenceApi().saveAllRooms();
      } catch (SFSStorageException e) {
         trace("Error saving rooms:" + e);
      }


This results in the static room we have (the room created by the admin console) getting saved, but all other rooms created at runtime are not saved. I'm assuming this is because the dynamically created rooms have already been destroyed.

Is there a place where the code above can be put to facilitate the saving of all the server's rooms at shutdown?

Thanks,
-Eric

Re: Saving rooms on server shutdown

Posted: 14 Jan 2014, 23:43
by efsnet
After digging some more I found that adding a ShutdownHook to the runtime does what I'm looking for. I added the following to the init method of my extension:

Code: Select all

      Thread thread = new ShutdownHookThread();
      Runtime.getRuntime().addShutdownHook(thread);


Then i created ShutdownHookThread as an inner class:

Code: Select all

   class ShutdownHookThread extends Thread {
      public void run() {
         try {
            trace("Saving rooms...");
            getParentZone().getRoomPersistenceApi().saveAllRooms();
         } catch (SFSStorageException e) {
            trace("Error saving rooms:" + e);
         }
      }
   }


When I ctrl+c to shutdown the server, all rooms are saved as intended.

Re: Saving rooms on server shutdown

Posted: 15 Jan 2014, 10:46
by Lapo
Yes, the ShutdownHook is the proper way to handle the server shut down.

Re: Saving rooms on server shutdown

Posted: 06 Mar 2014, 10:23
by hoangdoanh
Hi all

By the way, can I have related question ?

When Smartfox server shutdown, does the server will produce user-disconnect events and will handler for this event be executed fully and safety ?


Thanks,

Doanh

Re: Saving rooms on server shutdown

Posted: 06 Mar 2014, 13:18
by Lapo
Yes, it works as described in the previous posts, by using the JVM "shutdown hook" mechanism.

Re: Saving rooms on server shutdown

Posted: 06 Mar 2014, 16:25
by hoangdoanh
Thanks Lapo .

I tested and it worked fine.

Doanh