I have a following code snippet to make my HttpServer a multithreaded one,but its still working as single threaded.Can you please help me out? I'm using Java HttpServer API :
// Bind to port 8083
httpServer = HttpServer.create(new InetSocketAddress(HTTP_PORT), 0);
// Adding '/send' context
httpServer.createContext("/send", new SendHandler());
ExecutorService executor = Executors.newCachedThreadPool();
httpServer.setExecutor(executor);
// Start the server
httpServer.start();
Full Java Class :
public class GCMMockServer {
static HttpServer httpServer;
static final int HTTP_PORT = 8083;
public static void main(String[] args) {
GCMMockServer.start();
}
public static void start() {
try {
// Bind to port 8083
httpServer = HttpServer.create(new InetSocketAddress(HTTP_PORT), 0);
// Adding '/send' context
httpServer.createContext("/send", new SendHandler());
ExecutorService executor = Executors.newCachedThreadPool();
httpServer.setExecutor(executor);
// Start the server
httpServer.start();
System.out.println("GCM Mock Server started at (localhost,8083)");
} catch (IOException ex) {
Logger.getLogger(GCMMockServer.class.getName()).log(Level.SEVERE,
null, ex);
}
}
public static void stop() {
httpServer.stop(5);
}
// Handler for '/send' context
static class SendHandler implements HttpHandler {
@Override
public void handle(HttpExchange he) throws IOException {
System.out.println("Serving the request");
// Serve for POST requests only
if (he.getRequestMethod().equalsIgnoreCase("POST")) {
try {
// REQUEST Headers
Headers requestHeaders = he.getRequestHeaders();
Set<Map.Entry<String, List<String>>> entries = requestHeaders
.entrySet();
System.out.println("Inside POST method");
int contentLength = Integer.parseInt(requestHeaders
.getFirst("Content-length"));
List<String> contentTypes = null;
for (Iterator<Entry<String, List<String>>> iterator = entries
.iterator(); iterator.hasNext();) {
Entry<String, List<String>> entry = iterator.next();
String key = entry.getKey();
System.out.println("Key : " + key + ", values : ");
if (key.equalsIgnoreCase("Content-type")) {
contentTypes = entry.getValue();
}
for (Iterator<String> iterator2 = entry.getValue()
.iterator(); iterator2.hasNext();) {
String value = iterator2.next();
System.out.println("-----------" + value);
}
}
System.out.println("Content length : " + contentLength);
// REQUEST Body
InputStream is = he.getRequestBody();
if (contentTypes.contains("application/json")) {
InputStreamReader isr = new InputStreamReader(is,
"utf-8");
BufferedReader br = new BufferedReader(isr);
// From now on, the right way of moving from bytes to
// utf-8 characters:
int b;
StringBuilder buf = new StringBuilder(512);
while ((b = br.read()) != -1) {
buf.append((char) b);
}
JSONObject req = new JSONObject(buf.toString());
System.out.println("Request body : " + req);
String to = (String) req.get("to");
String message = (String) ((JSONObject) req.get("data"))
.get("message");
System.out.println("Request message : " + message
+ ", to : " + to);
}
// RESPONSE Headers
Headers responseHeaders = he.getResponseHeaders();
System.out.println("Response Headers : "
+ responseHeaders.toString());
String response = "Message received";
// Send RESPONSE Headers
he.sendResponseHeaders(HttpURLConnection.HTTP_OK,
response.length());
// RESPONSE Body
OutputStream os = he.getResponseBody();
os.write(response.getBytes());
is.close();
os.flush();
os.close();
he.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (he.getRequestMethod().equalsIgnoreCase("GET")) {
System.out.println("Nothing to serve in GET request type");
// RESPONSE Headers
Headers responseHeaders = he.getResponseHeaders();
String response = "Hi This is Sandeep";
System.out.println("Response Headers : "
+ responseHeaders.toString());
// Send RESPONSE Headers
he.sendResponseHeaders(HttpURLConnection.HTTP_OK,
response.length());
// RESPONSE Body
OutputStream os = he.getResponseBody();
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(response);
// is.close();
osw.close();
os.close();
he.close();
}
}
}
}
Consider changing the start of your
handle
method:Now start the server, open up a browser, load your url, refresh twise within 5s and watch your console log.
I have a feeling you'll be seeing multiple threads just fine.
Regarding your test:
The Test you use is single threaded (a
for
-loop). So there will be only one active invocation of your servlet at any one time. As the next iteration of yourfor
loop will only start once the last one is finished, the previous thread in the executor thread pool was recycled. There simply was no need for mutlithreading based on your test.