Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struts2 Async Action

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.

like image 884
speajus Avatar asked Oct 06 '22 09:10

speajus


1 Answers

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;
}
like image 66
Brice Roncace Avatar answered Oct 11 '22 14:10

Brice Roncace