簡體   English   中英

Java Servlet解析請求主體多線程

[英]Java Servlet parse request body multithread

我已經實現了一個異步Servlet,它需要解析請求主體並將解析的結果存儲在緩存中。 我應該在Servlet中實現parseBody()函數還是實現一個新類來進行解析? 最佳做法是什么?

這是我當前的代碼片段:

public class DocFeedServlet extends FeedServlet {

private static final Logger LOGGER = LoggerFactory.getLogger(DocFeedServlet.class);
private static final ObjectMapper OBJECTMAPPER = new ObjectMapper();


public void init(ServletConfig config) throws ServletException {
    super.init(config);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    final AsyncContext asyncContext = req.startAsync();
    asyncContext.start(new Runnable() {
        @Override
        public void run() {
            String bodyStr = getBody(req);
            if (bodyStr.isEmpty()) {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                asyncContext.complete();
                return;
            }

            int ignoreTime = Integer.parseInt(req.getParameter(Constant.PARAM_IGNORE_TIME));

            List<MockDocCacheKeyVal> mockDocCacheKeyVals = new ArrayList<>();
            List<String> docUpdateFields = new ArrayList<>();
            List<List<String>> docKeepFields = new ArrayList<List<String>>();
            List<String> uuidsToRemove = new ArrayList<>();

            int parseRet = parseBody(bodyStr, mockDocCacheKeyVals, docUpdateFields, docKeepFields, uuidsToRemove, ignoreTime);

            if (parseRet != 0) {
                resp.setStatus(HttpServletResponse.SC_OK);
            } else {
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
            asyncContext.complete();
        }
    });
}

protected int parseBody(String body, List<MockDocCacheKeyVal> mockDocCacheKeyVals, List<String> docUpdateFields, List<List<String>> docKeepFields, List<String> uuidsToRemove, int ignoreTime) {
    try {
        ObjectReader reader = OBJECTMAPPER.reader(new TypeReference<List<Document>>() { });
        List<Document> documents = reader.readValue(body);
        for (Document doc : documents) {
            if (doc.getAction() != null && doc.getAction().equalsIgnoreCase(Constant.DOC_FEED_ACTION_DELETE)) {
                if (doc.getUuid() != null) {
                    uuidsToRemove.add(doc.getUuid());
                }
                continue;
            }
            if (doc.getA() != null) {

            } else if (doc.getB() != null) {

            } else {
                DocumentUtils.pruneWeightSet(doc.getC(), cPruneSize);
                DocumentUtils.pruneWeightSet(doc.getD(), dPruneSize);
                DocumentUtils.pruneWeightSet(doc.getE(), ePruneSize);
            }
        }
        return documents.size();
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    return 0;
}
}

謝謝。

異步請求HttpServletRequest.getInputStream().setReadListener(ReadListener)讀取是通過Servlet 3.1中引入的HttpServletRequest.getInputStream().setReadListener(ReadListener)概念完成的

您將僅基於ReadListener事件進行讀取,並且讀取的內容將足夠多而不會阻塞。 (因此,不會讀取數兆字節的緩沖區!)。

您正在尋找該API,但是這里有地雷 ,因此請確保在完成API之前完全了解該API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM