Struts2 Asynchronous Action

Looking to use Struts2 with Serlvet 3.0 Async support.

My first approach was to simply process the write to the output stream in action and return zero. This, however, returns with a 404 "resource inaccessible." I am trying to adapt the Bosh servlet inside the struts action, using the ServletRequestAware, ServletResponseAware interfaces to input the response.

I use the location filter manager. Not quite sure if this is doable, but it would be happy if someone else managed to get an asynchronous action as part of the struts action. Perhaps there is an AsyncResult type or other magic to make this work.

+4
source share
1 answer

Make sure the spacer filter allows asynchronous. Here is what it 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 the Action, we get HttpServletRequest and HttpServletResponse and use AsyncContext in the same way as in the 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; } 
+4
source

Source: https://habr.com/ru/post/1439218/


All Articles