Struts2的ExecAndWait NullPointerException异常 [英] Struts2 ExecAndWait NullPointerException

查看:321
本文介绍了Struts2的ExecAndWait NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 ExecuAndWait Struts2的

我收到错误 NPE ,同时设置属性在操作请求(这是长时间运行)

下面是堆栈跟踪:

 显示java.lang.NullPointerException
    在org.apache.catalina.connector.Request.notifyAttributeAssigned(Request.java:1563)
    在org.apache.catalina.connector.Request.setAttribute(Request.java:1554)
    在org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:542)
    在javax.servlet.ServletRequestWrapper.setAttribute(ServletRequestWrapper.java:239)
    在com.os.gfnactions.SiteAction.createSite(SiteAction.java:1298)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)
    在java.lang.reflect.Method.invoke(来源不明)
    在com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    在com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
    在com.os.interceptor.BackgroundProcess $ 1.run(BackgroundProcess.java:60)
    在java.lang.Thread.run(来源不明)

来源snipate:


  

Action类:


 公共字符串createSite()抛出异常
    {
        ----        HttpServletRequest的请求= ServletActionContext.getRequest();
        了request.setAttribute(测试,测试); {在此行中我得到了错误}
        ---
    }

从<一个href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.struts/struts2-core/2.1.2/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java\"相对=nofollow> ExecuteAndWaitInterceptor.java

  231 IF((executeAfterValidationPass || secondTime)及!&安培; BP == NULL){
232 BP = getNewBackgroundProcess(姓名,actionInvocation,threadPriority);
233 session.put(KEY +名称,BP);
234 performInitialDelay(BP); //第一次让一些时间通过显示等待页面之前,
235 secondTime = FALSE;
236}

从<一个href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.struts/struts2-core/2.1.2/org/apache/struts2/interceptor/BackgroundProcess.java\"相对=nofollow> BackgroundProcess.java

 详细信息... BackgroundProcess(字符串threadName,最终ActionInvocation调用,诠释threadPriority){
50 this.invocation =请求;
51 this.action = invocation.getAction();
52尝试{
53最后线程t =新主题(新的Runnable(){
54公共无效更多...的run(){
55尝试{
56 beforeInvocation();
57结果= invocation.invokeActionOnly();
58 afterInvocation();
59}赶上(例外五){
除60 = E;
61}
62
63 DONE = TRUE;
64}
65});
66 t.setName(threadName);
67 t.setPriority(threadPriority);
68 t.start();
69}赶上(例外五){
除70 = E;
71}
72}

PS:的struts2的ExecuteAndWait概念
当长时间运行的请求时它会在单独的线程和结果返回等待执行,如此反复客户重新提交相同的请求一些间隔以了解它的进程的状态(这是在线程中运行)

我的问题:在上述情况下,当主请求(其中启动线程调用动作)等一会儿再等请求返回来认识行动的状态在这个时候我的动作类,如果了request.setAttribute(测试,测试); 执行再有就是错误,我上面有

解决方案

我也有类似的问题, execAndWait 拦截投掷 NPE 。你可以在这里找到我的案例:<一href=\"http://stackoverflow.com/questions/29876965/execandwait-interceptor-not-working-with-validation/29877137\">execAndWait拦截器不带验证我如何解决这个问题的工作。在这个问题我找到了,那 execAndWait 在单独的线程中运行,并不断抛出直到操作完成,并在平均时间周期它本身。我面对这个问题,因为我用模型驱动拦截它。由于该 getModel()模型驱动拦截一再被 execAndWait 拦截器调用。在getModel方法,它一次又一次从头开始设置新的POJO对象。

,然后将其进入验证的验证方法。内部验证过程中就发现了POJO领域之一空。显然,它发生是由于重新创建在 getModel 生新的POJO的对象。并由此被扔空指针异常

所以我所做的就是我用 SessionAware 接口。并储存首次POJO对象,当它进入验证。因为 execAndWait 必将再次调用的所有方法,重新从头开始编写一个对象。为此我检查对象在 getModel可用性()方法。如果在会话中发现,然后返回相同的对象,而中创建一个新对象。

我希望你会发现从这个办法。

I have used ExecuAndWait in Struts2

I am getting error NPE while setting attribute in request in action(which is long running)

Here is 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: concept of ExecuteAndWait of struts2 When long running request comes it going to execute in separate thread and result return WAIT, So again client resubmit same request in some interval to know status of its process(which is running in thread)

My Problem: In above case when main request(Which initiate thread to invoke action) return with WAIT and again other request come to know status of action at this time in my action class if request.setAttribute("test", "test"); execute then there is error which I have mention above

解决方案

I had a similar problem with execAndWait interceptor throwing NPE. You can find my case study here: execAndWait interceptor not working with validation on how I solved this problem.. In this problem what I find out was, that execAndWait runs in separate thread and keeps throwing wait until action is completed and in mean time it cycles itself. I faced this problem, because I used model driven interceptor with it. Due to that the getModel() of model driven interceptor was repeatedly being called by execAndWait interceptor. In getModel method, it was setting the new POJO object again and again from scratch.

And then it was entering into validate method for validation. Inside validation process it found one of the POJO field to be null. Obviously, it happened due to re-creating of raw new POJO object in getModel. And thus was throwing Null Pointer Exception.

So what I did was I used SessionAware interface. And stored the POJO object for the first time when it entered validate. Because execAndWait will surely call all methods again and re-write an object from scratch. For this purpose I checked the availability of that object in getModel() method. If that found in session, then return that same object, rather of creating a new object.

I hope you'll find a way from this.

这篇关于Struts2的ExecAndWait NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆