Пишу многопоточный сервер и почему то не могу получить ответ от сервера в консоль . В качестве клиента использую curl -X POST -d "temperature=18" http://localhost:9000/queue/weather Вроде бы данные попадают в out но не получаю ответа от сервера . Все переменные правильные смотрел в дебаге. В простом сервере без пула все отрабатывает верно
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private final HashMap<String, Service> modes = new HashMap<>();
public void start() {
modes.put("queue", new QueueService());
modes.put("topic", new TopicService());
ExecutorService pool = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors()
);
try (ServerSocket server = new ServerSocket(9000)) {
while (!server.isClosed()) {
Socket socket = server.accept();
pool.execute(() -> {
try (OutputStream out = socket.getOutputStream();
InputStream input = socket.getInputStream()) {
byte[] buff = new byte[1_000_000];
var total = input.read(buff);
var text = new String(Arrays.copyOfRange(buff, 0, total), StandardCharsets.UTF_8);
var req = Req.of(text);
var resp = modes.get(req.method()).process(req);
var temp = ("HTTP/1.1 " + resp.status() + " OK\r\n");
out.write(temp.getBytes());
var temp2 = resp.text();
out.write(temp2.getBytes());
out.flush();
System.out.println("hi");
} catch (IOException e) {
e.printStackTrace();
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().start();
}
}
public class Req {
private final String method;
private final String mode;
private final String text;
private Req(String method, String mode, String text) {
this.method = method;
this.mode = mode;
this.text = text;
}
public static Req of(String content) {
/* TODO parse a content */
String[] array = content.split("\r\n");
String[] temp = array[0].split("/");
String tempMethod = temp[1];
String[] temp2 = temp[2].split(" ");
String tempMode = temp2[0];
return new Req(tempMethod, tempMode, array[7]);
}
public String method() {
return method;
}
public String mode() {
return mode;
}
public String text() {
return text;
}
}
public class Resp {
private final String text;
private final int status;
public Resp(String text, int status) {
this.text = text;
this.status = status;
}
public String text() {
return text;
}
public int status() {
return status;
}
}
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class QueueService implements Service {
private ConcurrentHashMap<String, ConcurrentLinkedQueue<String>> queue = new ConcurrentHashMap<>();
private AtomicInteger id = new AtomicInteger();
@Override
public Resp process(Req req) {
queue.putIfAbsent(req.mode(), new ConcurrentLinkedQueue<>());
queue.get(req.mode()).add(req.text());
var result = queue.get(req.mode()).poll();
return new Resp(result, id.incrementAndGet());
}
}