Page 1 of 1

Weird issue with extension background tasks

Posted: 15 Sep 2020, 19:32
by noping
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

Re: Weird issue with extension background tasks

Posted: 16 Sep 2020, 14:07
by Lapo
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

Re: Weird issue with extension background tasks

Posted: 21 Sep 2020, 00:22
by noping
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?

Re: Weird issue with extension background tasks

Posted: 21 Sep 2020, 07:54
by Lapo
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

Re: Weird issue with extension background tasks

Posted: 22 Sep 2020, 14:48
by noping
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,