Grizzly Http Server - accepting only one connection at a time

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Grizzly Http Server - accepting only one connection at a time

This post has NOT been accepted by the mailing list yet.
I have a Grizzly Http Server with Async processing added. It is queuing my requests and processing only one request at a time, despite adding async support to it.

Path HttpHandler was bound to is: "/"
Port number: 7777

Behavior observed when I hit `http://localhost:7777` from two browsers simultaneously is:
Second call waits till first one is completed. I want my second http call also to work simultaneously in tandom with first http call.

Here are the classes <br>

    package com.grizzly;
    import java.io.IOException;
    import java.net.URI;
    import javax.ws.rs.core.UriBuilder;
    import org.glassfish.grizzly.http.server.HttpServer;
    import org.glassfish.grizzly.http.server.NetworkListener;
    import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
    import org.glassfish.grizzly.strategies.WorkerThreadIOStrategy;
    import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
    import com.grizzly.http.IHttpHandler;
    import com.grizzly.http.IHttpServerFactory;
    public class GrizzlyMain {
    private static HttpServer httpServer;
    private static void startHttpServer(int port) throws IOException {
    URI uri = getBaseURI(port);
    httpServer = IHttpServerFactory.createHttpServer(uri,
       new IHttpHandler(null));
    TCPNIOTransport transport = getListener(httpServer).getTransport();
    ThreadPoolConfig config = ThreadPoolConfig.defaultConfig()
    .setQueueLimit(-1)/* same as default */;
    System.out.println("Blocking Transport(T/F): " + transport.isBlocking());
    System.out.println("Num SelectorRunners: "
       + transport.getSelectorRunnersCount());
    System.out.println("Num WorkerThreads: "
       + transport.getWorkerThreadPoolConfig().getCorePoolSize());
    System.out.println("Server Started @" + uri.toString());
    public static void main(String[] args) throws InterruptedException,
       IOException, InstantiationException, IllegalAccessException,
       ClassNotFoundException {
    System.out.println("Press any key to stop the server...");
    private static NetworkListener getListener(HttpServer httpServer) {
    return httpServer.getListeners().iterator().next();
    private static URI getBaseURI(int port) {
    return UriBuilder.fromUri("").port(port).build();

HttpHandler (with async support built in)

    package com.grizzly.http;
    import java.io.IOException;
    import java.util.Date;
    import java.util.concurrent.ExecutorService;
    import javax.ws.rs.core.Application;
    import org.glassfish.grizzly.http.server.HttpHandler;
    import org.glassfish.grizzly.http.server.Request;
    import org.glassfish.grizzly.http.server.Response;
    import org.glassfish.grizzly.http.util.HttpStatus;
    import org.glassfish.grizzly.threadpool.GrizzlyExecutorService;
    import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
    import org.glassfish.jersey.server.ApplicationHandler;
    import org.glassfish.jersey.server.ResourceConfig;
    import org.glassfish.jersey.server.spi.Container;
    import com.grizzly.Utils;
     * Jersey {@code Container} implementation based on Grizzly
     * {@link org.glassfish.grizzly.http.server.HttpHandler}.
     * @author Jakub Podlesak (jakub.podlesak at oracle.com)
     * @author Libor Kramolis (libor.kramolis at oracle.com)
     * @author Marek Potociar (marek.potociar at oracle.com)
    public final class IHttpHandler extends HttpHandler implements Container {
    private static int reqNum = 0;
    final ExecutorService executorService = GrizzlyExecutorService
    private volatile ApplicationHandler appHandler;
    * Create a new Grizzly HTTP container.
    * @param application
    *          JAX-RS / Jersey application to be deployed on Grizzly HTTP
    *          container.
    public IHttpHandler(final Application application) {
    public void start() {
    public void service(final Request request, final Response response) {
    System.out.println("\nREQ_ID: " + reqNum++);
    System.out.println("THREAD_ID: " + Utils.getThreadName());
    // Instruct Grizzly to not flush response, once we exit service(...) method
    executorService.execute(new Runnable() {
    public void run() {
    try {
    System.out.println("Executor Service Current THREAD_ID: "
    + Utils.getThreadName());
    Thread.sleep(25 * 1000);
    } catch (Exception e) {
    } finally {
    String content = updateResponse(response);
    System.out.println("Response resumed > " + content);
    public ApplicationHandler getApplicationHandler() {
    return appHandler;
    public void destroy() {
    appHandler = null;
    // Auto-generated stuff
    public ResourceConfig getConfiguration() {
    return null;
    public void reload() {
    public void reload(ResourceConfig configuration) {
    private String updateResponse(final Response response) {
    String data = null;
    try {
    data = new Date().toLocaleString();
    } catch (IOException e) {
    data = "Unknown error from our server";
    response.setStatus(500, data);
    return data;


    package com.grizzly.http;
    import java.net.URI;
    import org.glassfish.grizzly.http.server.HttpServer;
    import org.glassfish.grizzly.http.server.NetworkListener;
    import org.glassfish.grizzly.http.server.ServerConfiguration;
     * @author smc
    public class IHttpServerFactory {
    private static final int DEFAULT_HTTP_PORT = 80;
    public static HttpServer createHttpServer(URI uri, IHttpHandler handler) {
    final String host = uri.getHost() == null ? NetworkListener.DEFAULT_NETWORK_HOST
       : uri.getHost();
    final int port = uri.getPort() == -1 ? DEFAULT_HTTP_PORT : uri.getPort();
    final NetworkListener listener = new NetworkListener("IGrizzly", host, port);
    final HttpServer server = new HttpServer();
    final ServerConfiguration config = server.getServerConfiguration();
    if (handler != null) {
    config.addHttpHandler(handler, uri.getPath());
    return server;