In my extension, I’m running a background thread which runs forever. The job of this thread is to sync user profiles (such a photo, phone, few more user-defined fields) with profiles on some external services (such as Slack or so), in addition to few other things such as keeping the rights as desired (e.g, an admin mistakenly made the wiki public from UI without realizing it, so this thread reverts those changes). This thread mostly sleeps, wakes up on a regular period, does it work and then goes back to sleep, in a loop.
In this thread, I need an instance of
XWikiContext so that it could do its work. Both these calls return
null for me:
Utils.getContext(); // returns null Utils.getComponent(XWikiContext.class); // returns null
I vaguely understand that these functions will return non null value only within a context of calls such as
logout, and other (http) requests. Am I right?
My queries are:
- How do I get an instance of
XWikiContextin the above background-thread context? Alternatively, how do I safely carry out the above tasks in the background?
XWikiContextbe used by multiple threads?