我在Struts2
使用了ExecuAndWait
在请求中设置属性(长时间运行)时,我得到错误NPE
,
这里是堆栈轨道:
java.lang.NullPointerException at org.apache.catalina.connector.Request.notifyAttributeAssigned(Request.java:1563) at org.apache.catalina.connector.Request.setAttribute(Request.java:1554) at org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:542) at javax.servlet.ServletRequestWrapper.setAttribute(ServletRequestWrapper.java:239) at com.os.gfnactions.SiteAction.createSite(SiteAction.java:1298) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) at com.os.interceptor.BackgroundProcess$1.run(BackgroundProcess.java:60) at java.lang.Thread.run(Unknown Source)
来源:
行动课:
public String createSite() throws Exception { ---- HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("test", "test"); {At this line I got error} --- }
从ExecuteAndWaitInterceptor.java
231 if ((!executeAfterValidationPass || secondTime) && bp == null) { 232 bp = getNewBackgroundProcess(name, actionInvocation, threadPriority); 233 session.put(KEY + name, bp); 234 performInitialDelay(bp); // first time let some time pass before showing wait page 235 secondTime = false; 236 }
从BackgroundProcess.java
public More ...BackgroundProcess(String threadName, final ActionInvocation invocation, int threadPriority) { 50 this.invocation = invocation; 51 this.action = invocation.getAction(); 52 try { 53 final Thread t = new Thread(new Runnable() { 54 public void More ...run() { 55 try { 56 beforeInvocation(); 57 result = invocation.invokeActionOnly(); 58 afterInvocation(); 59 } catch (Exception e) { 60 exception = e; 61 } 62 63 done = true; 64 } 65 }); 66 t.setName(threadName); 67 t.setPriority(threadPriority); 68 t.start(); 69 } catch (Exception e) { 70 exception = e; 71 } 72 }
PS: struts2的ExecuteAndWait的概念当长时间运行的请求到来时,它将在separate thread
执行并且返回WAIT
,因此客户端在某个时间间隔内重新提交相同的请求来知道它的进程状态(在线程中运行)
我的问题:在上面的情况下,当主请求(启动线程调用动作)返回与WAIT和其他请求来了解我的动作类的行动状态如果request.setAttribute("test", "test");
执行然后有上面提到的错误
我和execAndWait
拦截器抛出NPE
有类似的问题。 你可以在这里找到我的案例研究: execAndWait拦截器没有与我如何解决这个问题的验证工作 ..在这个问题,我发现是, execAndWait
运行在单独的线程,不断抛出wait
直到行动完成,同时它循环本身。 我遇到了这个问题,因为我使用了model driven
拦截器。 由于模型驱动拦截器的getModel()
被execAndWait
拦截器重复地调用。 在getModel方法中,它从头开始重复设置新的POJO对象。
然后进入validate
方法进行验证。 在验证过程中,发现其中一个POJO字段为空。 显然,这是由于在getModel
重新创建了原始的新POJO对象。 因此抛出Null Pointer Exception
。
所以我所做的是我使用了SessionAware
接口。 并且在输入validate
时第一次存储POJO对象。 因为execAndWait
肯定会再次调用所有的方法,重新编写一个对象。 为此,我在getModel()
方法中检查了该对象的可用性。 如果在会话中发现,那么返回相同的对象,而不是创建一个新的对象。
我希望你能从中找到办法。