Weird issue with extension background tasks

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

Moderators: Lapo, Bax

noping
Posts: 27
Joined: 11 May 2020, 12:38

Weird issue with extension background tasks

Postby noping » 15 Sep 2020, 19:32

Hi,

So I have a working java extension. I had added a background "monitoring" thread to periodically check "state" and perform some actions.
It was working fine for a while and then stopped working. I don't think I changed anything with the code, so could have been an issue with the latest patch?

Code:

Code: Select all

@Instantiation(InstantiationMode.SINGLE_INSTANCE)
public class MyExtension extends BaseClientRequestHandler {

 private class TaskRunner implements Runnable {
        public TaskRunner(String rname) {
               ..
        }

        public void run() {
             trace("THIS CODE IS NEVER CALLED");
        }
 }
    public MyExtension () {
        SmartFoxServer sfs = SmartFoxServer.getInstance();
        taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(new TaskRunner(""), 0, 1, TimeUnit.SECONDS);
    }
   
    ...
 }

Any assistance would be greatly appreciated. Thanks
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Weird issue with extension background tasks

Postby Lapo » 16 Sep 2020, 14:07

Hi,
the reason is that there is no exception trapping in the run() method of your task. In other words if a runtime exception is raised during the execution of the task the Task will be stopped.

Make sure to review the correct way to code a scheduled Task here:
https://smartfoxserver.com/blog/how-to- ... extension/

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
noping
Posts: 27
Joined: 11 May 2020, 12:38

Re: Weird issue with extension background tasks

Postby noping » 21 Sep 2020, 00:22

Hi,

Thanks for the feedback,

I added the try / catch , but still, nothing is happening.

Could something in the room/zone configuration be preventing the background thread from running?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Weird issue with extension background tasks

Postby Lapo » 21 Sep 2020, 07:54

The problem is that you're attempting to start the task in the constructor, while you should use the init() method as per the documentation.

Please do refer to the example provided in the article linked in my previous message.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
noping
Posts: 27
Joined: 11 May 2020, 12:38

Re: Weird issue with extension background tasks

Postby noping » 22 Sep 2020, 14:48

Thank you a lot,

There was null object access in my RUN method. Your input helped me find the issue.

As from where the TaskRunner is initiated from:
I am initiating it from the constructor of my RequestHandler ( as it has no INIT method like the SFSExtension ).

I have set it up this way so that each game RequestHandler (running InstantiationMode.SINGLE_INSTANCE) can have its own TaskRunner thread and is able to access the game data from the associated RequestHandler.

Thank you again,

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 87 guests