Saving rooms on server shutdown

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

Moderators: Lapo, Bax

efsnet
Posts: 55
Joined: 14 Nov 2013, 20:48

Saving rooms on server shutdown

Postby efsnet » 14 Jan 2014, 22:44

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
efsnet
Posts: 55
Joined: 14 Nov 2013, 20:48

Re: Saving rooms on server shutdown

Postby efsnet » 14 Jan 2014, 23:43

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

Re: Saving rooms on server shutdown

Postby Lapo » 15 Jan 2014, 10:46

Yes, the ShutdownHook is the proper way to handle the server shut down.
Lapo
--
gotoAndPlay()
...addicted to flash games
hoangdoanh
Posts: 251
Joined: 21 Jun 2013, 04:54
Location: Vietnam

Re: Saving rooms on server shutdown

Postby hoangdoanh » 06 Mar 2014, 10:23

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

Re: Saving rooms on server shutdown

Postby Lapo » 06 Mar 2014, 13:18

Yes, it works as described in the previous posts, by using the JVM "shutdown hook" mechanism.
Lapo

--

gotoAndPlay()

...addicted to flash games
hoangdoanh
Posts: 251
Joined: 21 Jun 2013, 04:54
Location: Vietnam

Re: Saving rooms on server shutdown

Postby hoangdoanh » 06 Mar 2014, 16:25

Thanks Lapo .

I tested and it worked fine.

Doanh

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 3 guests