问题
My code downloads a file from back-end server. Since we will be retrieving records numbering in millions, we have used Struts2 execAndWait
Interceptor. To test this, we are inserting delay of 30 secs.
I am able to download file without inserting delay (where test data very small), but after inserting it, i never get the file. Logs show that action class is being repeatedly executed due to <meta-refresh>
of 5 secs in wait file, even input-stream is populated.
What could be the reason for such behaviour ?
Code Set-up:
Struts.xml
<action name="file-download" class="com.company.namespace.test.TestDownloadActionClass">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="execAndWait">
<param name="delay">10000</param>
<param name="delaySleepInterval">500</param>
</interceptor-ref>
<result name="wait" type="freemarker" >/dir/resources/First-Page.ftl</result>
<result name="error" type="freemarker" >/dir/resources/Error-Page.ftl</result>
<result name="success" type="stream">
<param name="contentDisposition">attachment; filename="${downloadFilename}"</param>
<param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
</action>
P.S: I haven't tested this code on million data sets. Test data consists of only few data.
回答1:
The reason is you are using the Execute and Wait Interceptor in the wrong way. The interceptor is running an action in the background thread. While the action is executed, that could take a long time, the wait
result returned to the browser. When the result is executed the response is committed. The wait
result is returned after the specified parameter delay
.
Parameters:
delay
(optional) - an initial delay in millis to wait before the wait page is shown (returning wait as result code). Default is no initial delay.delaySleepInterval
(optional) - only used with delay. Used for waking up at certain intervals to check if the background process is already done. Default is 100 millis.
The wait
result is not returned if the initial delay
parameter is large enough to finish a job.
This interceptor also supports using an initial wait delay. An initial delay is a time in milliseconds we let the server wait before the wait page is shown to the user. During the wait this interceptor will wake every 100 millis to check if the background process is done premature, thus if the job for some reason doesn't take to long the wait page is not shown to the user.
来源:https://stackoverflow.com/questions/28667979/file-not-downloaded-with-execandwait-struts-interceptor-after-inserting-delay