Need Help with the TaskScheduler

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Gardosen
Posts: 3
Joined: 11 Nov 2014, 10:03

Need Help with the TaskScheduler

Postby Gardosen » 23 Mar 2015, 13:37

Hello Everyone,

since 6 Months i am working with Smartfox for a project of mine.
Till now i was able to solve every problem with checking the Forum or Documentation.
But today i gave up after a sleepless night, and i hope you can help me now.

I want to use the TaskScheduler for starting a RoundTimer. In my Game each Player of a match has 60Second to make his turn. For this i am using the following code

Code: Select all

    ScheduledFuture<?> playerTurnTimer;
    PlayerTurnTimeoutHandler playerTurnTimeoutHandler;

    /**
     * Private timer class for the player turn
     */
    private class PlayerTurnTimeoutHandler implements  Runnable {
        public void run() {
            try {
                AutomizeTurnForPlayer();
            } catch (Exception e){
                game.trace(ExtensionLogLevel.ERROR, "MatchRound:PlayerTurnTimeoutHandler:run");
                game.trace(ExtensionLogLevel.ERROR, e.toString());
            } finally {
                playerTurnTimer.cancel(true);
            }
        }

    public void init(EnumsUtil.GAME_MODE _gameMode, List<EnumsUtil.GAME_RULE> _rules) {
        //prepare the timer for the turns
        playerTurnTimeoutHandler = new PlayerTurnTimeoutHandler();
        .......
    }

    /**
     * Method to activate the turn timer for the user
     */
    public void activateTurnTimer() {
        SmartFoxServer sfs = SmartFoxServer.getInstance();
        //activate the timer for the user
        game.trace("Activate the timer for user:"+activeMatchPlayer.getUser().getName()+" about "+(activeMatchPlayer.getTurnTime()+calculatedDelay)+" Milliseconds");
        playerTurnTimer = sfs.getTaskScheduler().schedule(playerTurnTimeoutHandler, activeMatchPlayer.getTurnTime()+calculatedDelay, TimeUnit.MILLISECONDS);
    }


i implemented this code into a Class which is working together with a SFSExtension which is used as an extension for a room.
my problem now is that the timer is working randomly one or two times, and then he says he is starting it, but it never finishs.
in generall he was never doing it more then two times. and i dont understand why.
i checked several forum threads about the TaskScheduler and also checked out the Documentation and Example on the Tutorial Page.

what am i doing wrong? :(

Kind regards
Gardosen
User avatar
Lapo
Site Admin
Posts: 20392
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Need Help with the TaskScheduler

Postby Lapo » 23 Mar 2015, 15:48

Isn't because of the error catching?
If an exception occurs your code will shut down the timer. I would expect the behavior to be due to that.

Try removing the finally block and let the catch block log the error without shutting down the task.

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Gardosen
Posts: 3
Joined: 11 Nov 2014, 10:03

Re: Need Help with the TaskScheduler

Postby Gardosen » 23 Mar 2015, 17:44

Hello,

thank you for the tip with the final removal.

i didnt expected that traces inside of all the methods, doesnt show up if in the whole class an error occurs (to be honest, i dont like this behaviour o.o)
i was able to spot the bug and fixed it.

was not related to the Scheduler, everything is fine there :)
it was related to a list i was incorrectly addressing and where an indexArrayOutOfBounds was thrown.

Thank you :)
Kind regards
Gardosen
User avatar
Lapo
Site Admin
Posts: 20392
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Need Help with the TaskScheduler

Postby Lapo » 24 Mar 2015, 10:13

Gardosen wrote:Hello,

thank you for the tip with the final removal.

i didnt expected that traces inside of all the methods, doesnt show up if in the whole class an error occurs (to be honest, i dont like this behaviour o.o)
i was able to spot the bug and fixed it.

The error should definitely show up in the logs if the catch block is executed.

The only problem with the ScheduledThreadPoolExecutor from the JDK is that it swallows exceptions if you don't catch them in the run() method of your Runnable task implementation.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 11 guests