[xwiki-devs] Running groovy code in the background
Hello developers, I've been experimenting a little bit into running things in the background on our xwiki 3.5: as part of a velocity delivered page, I am calling groovy code. That code launches a thread in the background which runs for 1000 seconds checking if it can: - use the context's toString - call context.getContext() - use the xwiki object that was passed around My current conclusion is that context.getContext() fails (with an NPE trying to check programming rights) but others work. Is this the intended behavior? Is there a way for me to get another context to run in the background? thanks in advance Paul
Hi Paul, On Aug 24, 2012, at 4:33 PM, Paul Libbrecht <[email protected]> wrote:
Hello developers,
I've been experimenting a little bit into running things in the background on our xwiki 3.5: as part of a velocity delivered page, I am calling groovy code. That code launches a thread in the background which runs for 1000 seconds checking if it can: - use the context's toString - call context.getContext() - use the xwiki object that was passed around
My current conclusion is that context.getContext() fails (with an NPE trying to check programming rights) but others work.
Is this the intended behavior? Is there a way for me to get another context to run in the background?
You need to set up the correct environment in your thread as otherwise all bets are off :) We do that in lots of places; just to name a few: * The main Servlet thread * Stats * Lucene Updater and Rebuilder * Linkchecker * IRCBot thread In the future I'd like to have common initialization code that can be easily reused. We have a bit of this already with AbstractXWikiRunnable which are extended by Lucene Updater and Rebuilder threads for example. -Vincent
Vincent, I am now close to a working version of a background task. I am launching a thread that has as Runnable an AbstractWikiRunnable. This process can run and read objects but seems not miss some. The same code, in using xwiki and context of a page obtains an empty objects list in the background task and one quite full in the page. Would this be related to the way I build my AbstractWikiRunnable? E.g. should I use LucenePlugin's runnable super("xcontext", xcontext) ? Or would there be another reason that makes the call xwiki.getDocument("space.name") fail? For the information, i'm doing:
XWikiContext xcontext = (XWikiContext) Utils.getComponent(Execution.class).getContext() .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); Context context = new Context(xcontext); XWiki xwiki = new com.xpn.xwiki.api.XWiki(context.getXWiki(), xcontext);
thanks in advance Paul
My current conclusion is that context.getContext() fails (with an NPE trying to check programming rights) but others work.
Is this the intended behavior? Is there a way for me to get another context to run in the background?
You need to set up the correct environment in your thread as otherwise all bets are off :)
We do that in lots of places; just to name a few: * The main Servlet thread * Stats * Lucene Updater and Rebuilder * Linkchecker * IRCBot thread
In the future I'd like to have common initialization code that can be easily reused. We have a bit of this already with AbstractXWikiRunnable which are extended by Lucene Updater and Rebuilder threads for example.
The console does say:
2012-09-14 20:23:28,278 [archiver-351868487901461] WARN c.x.x.a.XWiki - Failed to access document name = [Orders], type = [DOCUMENT], parent = [name = [GCheckout], type = [SPACE], parent = [name = [xwiki], type = [WIKI], parent = [null]]]: null
Is that a rights problem? How do you give the rights to an AbstractWikiRunnable? thanks in advance Paul
On 09/14/2012 03:34 PM, Paul Libbrecht wrote:
The console does say:
2012-09-14 20:23:28,278 [archiver-351868487901461] WARN c.x.x.a.XWiki - Failed to access document name = [Orders], type = [DOCUMENT], parent = [name = [GCheckout], type = [SPACE], parent = [name = [xwiki], type = [WIKI], parent = [null]]]: null
Is that a rights problem? How do you give the rights to an AbstractWikiRunnable?
thanks in advance
There's no context user, and using the .api. package always checks rights. So, you have two options: - set a user in the XWikiContext - use the internal objects instead of the API, which bypasses rights -- Sergiu Dumitriu http://purl.org/net/sergiu/
thanks Sergiu,
There's no context user, and using the .api. package always checks rights.
I was fearing exactly that.
So, you have two options: - set a user in the XWikiContext
How?
- use the internal objects instead of the API, which bypasses rights
I'm almost there. I think I'll finally become good at that! paul
Vincent, can you detail the best practice to create an XWikiContext? I've done:
XWikiContext xcontext = (XWikiContext) Utils.getComponent(Execution.class).getContext() .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
But then I get a context whose URLFactory is null. Is there a better way? thanks in advance Paul Le 24 août 2012 à 16:45, Vincent Massol a écrit :
Is there a way for me to get another context to run in the background?
You need to set up the correct environment in your thread as otherwise all bets are off :)
We do that in lots of places; just to name a few: * The main Servlet thread * Stats * Lucene Updater and Rebuilder * Linkchecker * IRCBot thread
In the future I'd like to have common initialization code that can be easily reused. We have a bit of this already with AbstractXWikiRunnable which are extended by Lucene Updater and Rebuilder threads for example.
participants (3)
-
Paul Libbrecht -
Sergiu Dumitriu -
Vincent Massol