execAndWait
causes the action to be executed in a new thread.- Since
ActionContext
isThreadLocal
, the new thread will not get the values stored in the parent thread version ofActionContext
. Every thread has a unique version ofActionContext
getText()
will throw a NPE when it tries to execute in the new thread because it depends onActionContext
To fix this, you need to copy the parent threads ActionContext
into the execAndWait
thread. You can do this by extending the BackgroundProcess
class, implementing the beforeInvocation()
and afterInvocation()
methods, and extending ExecuteAndWaitInterceptor
, implementing the getNewBackgroundProcess()
method.
EXAMPLE
public class YourExecAndWaitInterceptor extends ExecuteAndWaitInterceptor {
private static final long serialVersionUID = 1L;
/**
* {@inheritDoc}
*/
@Override
protected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {
return new YourBackgroundProcess(arg0, arg1, arg2, ActionContext.getContext());
}
}
public class YourBackgroundProcess extends BackgroundProcess {
private final ActionContext context;
public YourBackgroundProcess(String threadName, ActionInvocation invocation, int threadPriority, ActionContext context) {
super(threadName, invocation, threadPriority);
this.context = context;
}
/**
* {@inheritDoc}
*/
@Override
protected void beforeInvocation() {
ActionContext.setContext(context);
}
/**
* {@inheritDoc}
*/
@Override
protected void afterInvocation() {
ActionContext.setContext(null);
}
}