Access FacesContext in xAgent (in new Thread)

777 views Asked by At

I am planning to use single entry point for all 5 minutes xAgents, meaning one XPage launchs all 5 minutes "java agents" (classes that should be launched every 5 minutes). I would like to lauch that java code in new different Threads to have true parallel lauch of such agents.

The mentioned "java agents" have strong interdependency with other NSF app classes. Many of them rely on FacesContext and / or other XSP / JSF global variables.

"Java agent" code example:

import javax.faces.context.FacesContext;
import com.ibm.domino.xsp.module.nsf.NSFComponentModule;
import com.ibm.domino.xsp.module.nsf.NotesContext;
import com.ibm.xsp.extlib.util.ExtLibUtil;

public class Agent1 implements Runnable {

private NSFComponentModule module;

public Agent1() {
    this.module = NotesContext.getCurrent().getModule();
    System.out.println("Agent1: test 1.1: " + (ExtLibUtil.getCurrentSessionAsSigner() == null)); // FALSE here
    System.out.println("Agent1: test 1.2: " + (FacesContext.getCurrentInstance() == null)); // FALSE here
}

public void run() {
    NotesContext context = new NotesContext(this.module);
    NotesContext.initThread(context);

    System.out.println("Agent1: test 2.2: " + (ExtLibUtil.getCurrentSessionAsSigner() == null)); // TRUE here
    System.out.println("Agent1: test 2.2: " + (FacesContext.getCurrentInstance() == null)); // TRUE here

    // Threaded xAgent job here...

    NotesContext.termThread();
}
}

The issue: Such methods like: FacesContext.getCurrentInstance(), ExtLibUtil.getCurrentSessionAsSigner() return NULL in new Thread.

The question: Is it possible to init XSP / JSF engine inside new Thread to get access to FacesContext, etc (to get not null in lines "Agent1: test 2.1" and "Agent1: test 2.2")?

Thanks in advance!

1

There are 1 answers

3
Paul Stephen Withers On

I encountered a similar problem when developing with XOTS in OpenNTF Domino API. The best option is to pass whatever objects are needed in the constructor. Here's the relevant blog post on XOTS http://www.intec.co.uk/xots-background-and-multithreaded-tasks-the-openntf-domino-api-way-part-two/ (replace "two" with "one" and "three" for the other parts of the series).

XOTS works very well for parallel processing and allows configuration of the number of threads, by default 10.

When I looked at documentation for threading in XPages, the blog posts I found suggested potential issues not covered in that post, but didn't elaborate. I've not investigated further.