我有2个Webservlet,其中一个正在Asynch模式下运行:
@WebServlet(name="myServlet", urlPatterns={"/asyncprocess"}, asyncSupported=true)
@ConditionalOnProperty(name="app.processasynch", havingValue="true")
public class AsyncServletGW extends HttpServlet {
@Autowired
private Stage1WorkersPool wp;
private static final Logger LOGGER = LoggerFactory.getLogger(ProcessBusinessLogicStage1Impl.class);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
AsyncContext aCtx = request.startAsync(request, response);
int paymentId = (new Random()).nextInt(100000);
WorkerThread workerThread = wp.getPool().get(paymentId % wp.getNumWorkingThreads());
ThreadUnitWorkImpl py = new ThreadUnitWorkImpl();
String msgBody;
if ("POST".equalsIgnoreCase(request.getMethod())){
msgBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
py.setXmlRequest(msgBody);
py.setACtx(aCtx);
aCtx.getResponse().setContentType("text/plain");
py.setResponse(aCtx.getResponse());
}else{
LOGGER.error("Not a POST request");
}
LOGGER.debug("Servlet Processing : " + py.toString());
workerThread.addUnitOfWork(py);
}
}
另一个处于同步模式
@WebServlet(name="myServlet", urlPatterns={"/syncprocess"})
@ConditionalOnProperty(name="app.processasynch", havingValue="false")
public class SyncServletGW extends HttpServlet {
@Autowired
private ProcessInSynchStages synchP;
private static final Logger LOGGER = LoggerFactory.getLogger(ProcessBusinessLogicStage1Impl.class);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
ThreadUnitWorkImpl py = new ThreadUnitWorkImpl();
String msgBody;
if ("POST".equalsIgnoreCase(request.getMethod())){
msgBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
response.setContentType("text/plain");
py.setXmlRequest(msgBody);
py.setResponse(response);
}else{
LOGGER.error("Not a POST request");
}
ArrayList<ThreadUnitWorkImpl> tuwList = new ArrayList<>();
tuwList.add(py);
synchP.runInSynch(tuwList);
}
}
调用异步时,我得到了臭名昭著的异常:已解决[org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示形式]
在调试时,我注意到第一次调用异步servlet之后,第二,第三等总是被延迟。 1-2秒后,将引发以上异常,并且正在处理servlet。 但是,在同步模式下,一切运行顺利...有什么建议吗?
答案 0 :(得分:0)
我的愚蠢错误...我忘了通过运行complete()methoid来关闭AsyncContext。...