我必須實現並行進程才能同時調用4個微服務調用。我在哪裏有16個輸入,其中每個微服務需要消耗4個進程。如何在Java中調用REST端點的並行處理/多線程處理
for (int i = 0; i < 16; i++) {
if (m == i) {
LOGGER.info("Inside If condition");
String jsonMes1 = jsonArr.get(i).toString();
URL url = new URL("http://localhost:8086/myService");
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(jsonMessage.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
LOGGER.info(" FAILED ");
} else {
LOGGER.info(" SUCESSFULLY PROCESSED ");
}
n = m;
n++;
} else if (n == i) {
//Same process in different port
o = n;
o++;
} else if (o == i) {
//Same process in different port
p = o;
p++;
} else if (p == i) {
//Same process in different port
m = p;
m++;
}
如果條件運行在不同的端口,所有3個以上的其他重複代碼相同。但是,如果第一個請求完成,然後第二個處理完成,那麼問題就是一次,但我需要以並行方式進行。我只需要在我從JSON數組迭代對象的地方並行處理它。請建議如何以並行方式
你嘗試過什麼嗎? – Turing85
它正在同步處理,但我需要異步繼續處理4進程完成16次處理 – user3428736