Struts2 ExecAndWait NullPointerException

I used ExecuAndWait in ExecuAndWait

I get an NPE error when setting an attribute in a request in action (which takes a long time)

Here is the stack track:

 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) 

Source snipate:

Action class:

 public String createSite() throws Exception { ---- HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("test", "test"); {At this line I got error} --- } 

From 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 } 

From 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: the concept of ExecuteAndWait struts2 When a long request arrives, it will be executed in a separate thread and return a WAIT result. Therefore, again, the client retransmits the same request at a certain interval in order to find out the status of its process (which is running in the thread).

My problem: In the above case, when the main request (which initiates the thread to call the action) returns with WAIT, and again another request will know the status of the action at this time in my action class if request.setAttribute("test", "test"); execute, that is, the error I mentioned above

0
java multithreading apache struts2
source share
1 answer

I had a similar problem with execAndWait intercepting an NPE interceptor. Here you can find my research: the execAndWait interceptor does not work with checking how I solved this problem .. In this problem, I found out that execAndWait works in a separate thread and continues to throw wait until the action is completed, and at the same time, it cycles in cycles. I ran into this problem because I used model driven interceptor with it. Because of this, getModel() model-driven interceptor was repeatedly called by execAndWait . In the getModel method, he tuned the new POJO object over and over again from scratch.

And then he went into the validate method for validation. Inside the validation process, he found one of the POJO fields to be zero. Obviously, this was due to the recreation of the unprocessed new POJO in getModel . And so a Null Pointer Exception .

So what I did, I used the SessionAware interface. And saved the POJO object the first time it entered validate . Since execAndWait will definitely call all methods again and overwrite the object from scratch. To do this, I checked the availability of this object in the getModel() method. If this is found in the session, return the same object, rather than create a new object.

I hope you find a way out of this.

0
source share

All Articles