SFS scheduled task

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

Moderators: Lapo, Bax

Mrm83
Posts: 155
Joined: 17 Dec 2017, 04:02

SFS scheduled task

Postby Mrm83 » 22 Jan 2019, 15:05

I read in one of the articles that to break the task, just throw an exception.

I tried throwing an exception within run() in the case of an error but the scheduled task still runs.

How can I kill the scheduled task from within the task itself?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFS scheduled task

Postby Lapo » 22 Jan 2019, 16:43

Hi,
can you show me the code of your task?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
Mrm83
Posts: 155
Joined: 17 Dec 2017, 04:02

Re: SFS scheduled task

Postby Mrm83 » 22 Jan 2019, 19:06

It gets inside the exception condition, but it doesn't kill it.
This is called from another class
MMOGame.getInstance().SFS().getTaskScheduler().scheduleAtFixedRate (
new PlayerMonitorTask(GetNetworkId (), GetId()), 0, PLAYER_MONITOR_TASK_INTERVAL, TimeUnit.MILLISECONDS);

Code: Select all

            
package pixelmmo.model.task;

import com.smartfoxserver.v2.entities.User;

import pixelmmo.logging.MMOLogging;
import pixelmmo.model.MMOPlayer;
import pixelmmo.model.MMOWorld;
import pixelmmo.server.MMOGame;

public class PlayerMonitorTask implements Runnable {
   private int _id;
   private int _pcid;
   
   public PlayerMonitorTask (int id, int pcid) {
      _id = id;
      _pcid = pcid;
   }
   
   @Override
   public void run () {
      User user = MMOGame.getInstance().Ext().getParentRoom().getUserById(_id);
      if (user == null || !user.isConnected()) {
         MMOLogging.LogError("user not found " + _id + " " + _pcid);
         MMOPlayer pc = (MMOPlayer) MMOWorld.getInstance().FindObject(_pcid);
         if (pc == null) {
            MMOLogging.LogError("PC is not found too id: " + _id + " pid:" + _pcid);
            try {
               throw new Exception ();
            } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
         } else {
            pc.StopMonitors ();
            //pc.Logout();
         }
         return;
      } else {
         //MMOGame.getInstance().Log("user monitor");
      }
      ((MMOPlayer)user.getProperty("MMOPlayer")).Update();
   }

}
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFS scheduled task

Postby Lapo » 23 Jan 2019, 08:06

The problem is that you're throwing an exception and catching it at the same time :)
This is not how it works, you need to throw an (unchecked) exception so that the execution leaves the run() method.

In other word just use:

Code: Select all

throw new RuntimeException();


Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Mrm83
Posts: 155
Joined: 17 Dec 2017, 04:02

Re: SFS scheduled task

Postby Mrm83 » 23 Jan 2019, 13:35

RuntimeException fixed it!

I couldn't throw Exception without try/catch without the ide erroring out.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 47 guests