Looking to use Struts2 with Serlvet 3.0 Async support.
My first approach was to just handle to writing to the outputstream in the action and returning null. This however returns with a 404 "resource not available". I am attempting to adapt a Bosh servlet inside of a struts action, using ServletRequestAware, ServletResponseAware interfaces to inject the response.
I am using the struts filter dispatcher. Not entirely sure if this is doable,but would be sure happy if someone else has managed to get async to work within a struts action. Perhaps here is an AsyncResult type or someother magic to make this work.
Make sure the struts filter allows async. Here's what that looks like in the web.xml file:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<async-supported>true</async-supported>
</filter>
Then from within an Action obtain the HttpServletRequest
and HttpServletResponse
and use the AsyncContext
as you would within a servlet:
public String execute() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
final AsyncContext asyncContext = req.startAsync(req, res);
asyncContext.start(new Runnable() {
@Override
public void run() {
try {
// doing some work asynchronously ...
}
finally {
asyncContext.complete();
}
}
});
return Action.SUCCESS;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With