ShutdownHookThread

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

Moderators: Lapo, Bax

Ber4ber43
Posts: 79
Joined: 30 Apr 2019, 14:35

ShutdownHookThread

Postby Ber4ber43 » 09 May 2019, 14:52

Hi, I have a series of data loaded in the server's RAM, I'm using "ShutdownHookThread" to save the data in the database when the server is closed with ctrl + c. I don't understand why I get NULL.

Code: Select all

package zone1;
 
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

import zone2.Info;

public class ServerInit extends BaseServerEventHandler {
 
   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException {
      Info.Init(getParentExtension().getParentZone().getDBManager());
      
      Thread thread = new ShutdownHookThread();
       Runtime.getRuntime().addShutdownHook(thread);
   }
   
}


Code: Select all

import zone1.ShutdownSave;

//ctrl + c
public class ShutdownHookThread extends Thread
{
    public void run()
    {       
       ShutdownSave save = new ShutdownSave();
       save.init();
    }
}


Code: Select all

package zone1;

import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class ShutdownSave extends SFSExtension
{
   public void init()
   {
      System.out.println("Saving dates...");
      IDBManager dbmanager = getParentZone().getDBManager();
       
      try {         
         dbmanager.executeInsert(
                 "INSERT into test(id) values (?)",
                 new Object[] { 1 }
           );
         
      }
       catch (Exception e) {
          trace("error: " + e);
          e.printStackTrace();
      }
   }
}


Code: Select all

09 mag 2019 | 16:54:52,916 | INFO  | Thread-1 | entities.managers.SFSZoneManager |     | BuddyList saveAll...
09 mag 2019 | 16:54:52,917 | WARN  | SFS2X ShutdownHook | v2.core.SFSShutdownHook |     | SFS2X is shutting down. The process may take a few seconds...
09 mag 2019 | 16:54:52,923 | INFO  | Thread-2 | entities.managers.SFSBannedUserManager |     | BanUser data saved.
09 mag 2019 | 16:54:52,924 | INFO  | Thread-5 | Extensions |     | {null}: error: java.lang.NullPointerException
09 mag 2019 | 16:54:52,924 | INFO  | Thread-6 | Extensions |     | {null}: error: java.lang.NullPointerException
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: ShutdownHookThread

Postby Lapo » 09 May 2019, 16:04

Hello,
If you use System.out.println to print errors you won't find them in the logs, as they get output to the console.
So you either keep the server running in the console to check the details of those exceptions or you use logging API instead.
See here:
https://smartfoxserver.com/blog/better- ... -in-sfs2x/

Side note: I don't understand why the ShutdownSave class is extending SFSExtension. It seem incorrec, as you need to inherit from SFSExtension only if the class is loaded as Zone/Room Extension, which is not.

Let us know the details of the exceptions.
Lapo
--
gotoAndPlay()
...addicted to flash games
Ber4ber43
Posts: 79
Joined: 30 Apr 2019, 14:35

Re: ShutdownHookThread

Postby Ber4ber43 » 09 May 2019, 17:04

I improved the code

Code: Select all

package zone1;

import java.util.logging.Logger;

import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.extensions.SFSExtension;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

//ctrl + c
public class ShutdownDemo
{
   public ShutdownDemo ()
   {
      Thread t = new ShutdownHookThread();      
      Runtime.getRuntime().addShutdownHook(t);
      
      while (true)
      {
         try
         {
            Thread.sleep (300);
         }
         catch (InterruptedException e) {
            Logger.getLogger(e.toString());
            e.getCause();
             e.getStackTrace();
              e.printStackTrace();
         }
      }
   }
      
   public static void main(String args[])
   {
      new ShutdownDemo();
   }         
}
         
class ShutdownHookThread extends Thread
{      
   public void run()
   {
      Write write = new Write();
      write.init();
      
      try {
         Thread.sleep (300000);
      } catch (InterruptedException e) {         
         Logger.getLogger(e.toString());
         e.getCause();
          e.getStackTrace();
           e.printStackTrace();
      }
   }
}

 class Write extends SFSExtension
{   
   @Override
   public void init()
   {      
      IDBManager dbmanager = null;
      try
       {       
         trace("Saving dates...");
         dbmanager = getParentZone().getDBManager();
          dbmanager.executeInsert(
                  "INSERT into test(id) values (?)",
                  new Object[] { 1 }
            );
          
       }
        catch (Exception e)
       {
           Logger.getLogger(e.toString());
          e.getCause();
          e.getStackTrace();
          e.printStackTrace();
       }      
   }
}


getLogger.warn("My custom message", ex); I can not seem to use it
I copied this error code from the console.

Code: Select all

19:04:05,834 INFO  [main] v3.SessionFilter     - BlueBox-2X Service (3.3.0) READY.
19:04:07,831 INFO  [Thread-1] managers.SFSZoneManager     - BuddyList saveAll...
19:04:07,832 WARN  [SFS2X ShutdownHook] core.SFSShutdownHook     - SFS2X is shutting down. The process may take a few seconds...
19:04:07,832 INFO  [Thread-5] Extensions     - {null}: Saving dates...
java.lang.NullPointerException
        at zone1.Write.init(ShutdownDemo.java:65)
        at zone1.ShutdownHookThread.run(ShutdownDemo.java:43)
19:04:07,838 INFO  [Thread-2] managers.SFSBannedUserManager     - BanUser data saved.
19:04:08,716 WARN  [SFSWorker:Ext:4] managers.SFSExtensionManager     - java.lang.NullPointerException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.NullPointerException
Message: *** Null ***
Description: Error during event handling: java.lang.NullPointerException, Listener: { Ext: Admin, Type: JAVA, Lev: ZONE, { Zone: --=={{{ AdminZone }}}==-- }, {} }
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.admin.handlers.events.TraceMessageEvtHandler.handleServerEvent(TraceMessageEvtHandler.java:45)
com.smartfoxserver.v2.extensions.SFSExtension.handleServerEvent(SFSExtension.java:259)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:768)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchZoneLevelEvent(SFSExtensionManager.java:689)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.handleServerEvent(SFSExtensionManager.java:1023)
com.smartfoxserver.v2.core.SFSEventManager$SFSEventRunner.run(SFSEventManager.java:66)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Ber4ber43
Posts: 79
Joined: 30 Apr 2019, 14:35

Re: ShutdownHookThread

Postby Ber4ber43 » 09 May 2019, 17:39

Side note: I don't understand why the ShutdownSave class is extending SFSExtension. It seem incorrec, as you need to inherit from SFSExtension only if the class is loaded as Zone/Room Extension, which is not.


You're absolutely right, but how can I write to the database without SFSExtension?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: ShutdownHookThread

Postby Lapo » 10 May 2019, 06:28

You can pass a reference to the database instance in your shutdown thread, via its constructor.
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Ber4ber43
Posts: 79
Joined: 30 Apr 2019, 14:35

Re: ShutdownHookThread

Postby Ber4ber43 » 10 May 2019, 09:57

Work it!!

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Thomasea and 67 guests