So, in my application, I am sending a request from the http server handler to another server. The fact is that the second server is stuck on reading the object. I tried to figure it out myself, but I don't see the problem. All threads are closed, maybe the answer to the client is in the wrong place? I do not know why this is so.
This is my client:
public class ClientSimulator { private Random random; private static int clientCounter = 1; public static void main(String[] args) throws Exception { new ClientSimulator(); new ClientSimulator(); new ClientSimulator(); } private ClientSimulator() { this.random = new Random(); RuntimeMXBean rmb = ManagementFactory.getRuntimeMXBean(); long arrivalTime = rmb.getUptime(); System.out.println("thread no. " + clientCounter++ + " arrival time: " + arrivalTime); try { String myurl= "http://localhost:8080/sender"; String serverResponse = createClient(myurl); System.out.println(serverResponse); } catch (Exception e) { e.printStackTrace(); } } private String createClient(String myurl) throws Exception { URL url; BufferedReader reader = null; StringBuilder stringBuilder; try {
This is how I send a request from the main server to another server:
@Override public void handle(HttpExchange httpExchange) throws IOException { String template = "\nClient no. %s connected!"; //Getting task Task t; ObjectInputStream ois = new ObjectInputStream(httpExchange.getRequestBody()); try { System.out.print("Recieved object:"); t = (Task) ois.readObject(); t.setDeadline(deadline); t.setHard(isHard); System.out.print(" not sorted array: "); int[] arr = (int[]) t.getData(); for (int anArr : arr) { System.out.print(anArr + " "); } ois.close(); String response = "response for client no. " + clientCounter; httpExchange.sendResponseHeaders(200, response.length()); OutputStream os = httpExchange.getResponseBody(); os.write(response.getBytes()); os.close(); clientCounter++; HttpURLConnection test = (HttpURLConnection) new URL(fogServ1URL).openConnection(); test.setDoOutput(true); System.out.println("test__1"); ObjectOutputStream stream = new ObjectOutputStream(test.getOutputStream()); stream.flush(); System.out.println("test__2"); stream.writeObject(t); System.out.println("test__3"); stream.close(); System.out.println("test__4"); test.getResponseCode(); System.out.println("test__5"); //this doesn't print } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
This is the handler from the second server:
class RootHandler implements HttpHandler{ private static int clientCounter = 1; @Override public void handle(HttpExchange exchange) throws IOException { System.out.println("\nRoot handler; \n\tclient no. " + clientCounter++); Task t; ObjectInputStream ois = new ObjectInputStream(exchange.getRequestBody()); try { System.out.println("Recieved object:");
I do not understand, because I send Task from the client to the main server in the same way and its work. I just can't read the output on the following servers. What am I doing wrong?
Btw. If someone is wondering why I am sending the same object to another server: I plan to create more servers, the main server will send them requests depending on the type / containing headers.
source share