diff --git a/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java index 5ab03f15a7..6fe8ed5848 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java @@ -111,6 +111,34 @@ * for obtaining and releasing resources that the background process will need to execute successfully. To use your * background process extension, extend ExecuteAndWaitInterceptor and implement the getNewBackgroundProcess() method. *
+ * + *+ * The background process is keyed by action name alone, so within one session a given action can only run once at a + * time - a second browser tab joins the process already running instead of starting its own. Override + * {@link #getBackgroundProcessName(ActionProxy)} to widen that key, for example with the transaction token, so that + * each tab gets its own process: + *
+ * + *
+ * public class TokenizedExecuteAndWaitInterceptor extends ExecuteAndWaitInterceptor {
+ * @Override
+ * protected String getBackgroundProcessName(ActionProxy proxy) {
+ * String token = TokenHelper.getToken();
+ * return token == null
+ * ? super.getBackgroundProcessName(proxy)
+ * : super.getBackgroundProcessName(proxy) + "_" + token;
+ * }
+ * }
+ *
+ *
+ * + * Two caveats apply to any key that varies per request. First, the entry is dropped from the session only when a + * request observes the process as done, so a per-tab or per-token key strands one background process - and the action + * instance it holds - in the session for every run the user abandons; unlike the action-name key, that growth is + * unbounded. Second, the wait page must carry the value used in the key on every refresh (for instance + * <s:url includeParams="all"/> together with the token interceptor); if it does not, each refresh starts another + * background process rather than joining the one already running. + *
* * *Example code:
diff --git a/core/src/test/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptorTokenScopeTest.java b/core/src/test/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptorTokenScopeTest.java new file mode 100644 index 0000000000..e298cb84d4 --- /dev/null +++ b/core/src/test/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptorTokenScopeTest.java @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.interceptor; + +import jakarta.servlet.http.HttpSession; +import org.apache.struts2.ActionContext; +import org.apache.struts2.ActionProxy; +import org.apache.struts2.ActionProxyFactory; +import org.apache.struts2.DefaultActionProxyFactory; +import org.apache.struts2.ObjectFactory; +import org.apache.struts2.StrutsInternalTestCase; +import org.apache.struts2.action.Action; +import org.apache.struts2.config.Configuration; +import org.apache.struts2.config.ConfigurationException; +import org.apache.struts2.config.ConfigurationProvider; +import org.apache.struts2.config.entities.ActionConfig; +import org.apache.struts2.config.entities.InterceptorMapping; +import org.apache.struts2.config.entities.PackageConfig; +import org.apache.struts2.config.entities.ResultConfig; +import org.apache.struts2.dispatcher.HttpParameters; +import org.apache.struts2.inject.ContainerBuilder; +import org.apache.struts2.mock.MockResult; +import org.apache.struts2.ognl.OgnlUtil; +import org.apache.struts2.util.TokenHelper; +import org.apache.struts2.util.location.LocatableProperties; +import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest; +import org.apache.struts2.views.jsp.StrutsMockHttpSession; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Covers how the background process is keyed within a single session: by action name only, and by + * action name plus transaction token when {@link ExecuteAndWaitInterceptor#getBackgroundProcessName} + * is overridden as described in that interceptor's javadoc. + */ +public class ExecuteAndWaitInterceptorTokenScopeTest extends StrutsInternalTestCase { + + private StrutsMockHttpServletRequest request; + private Map