Java 类org.apache.http.nio.reactor.IOEventDispatch 实例源码

项目:nio-benchmark    文件:AsyncServer.java   
public void start() throws Exception {
    // Create HTTP protocol basic processing chain
    HttpProcessor httpProcessor = HttpProcessorBuilder.create()
            .add(new ResponseDate()).add(new ResponseContent())
            .add(new ResponseConnControl()).build();
    // Create server
    HttpAsyncService protocolHandler = new HttpAsyncService(httpProcessor,
            uriMapper);
    NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory = new DefaultNHttpServerConnectionFactory();
    IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(
            protocolHandler, connFactory);
    IOReactorConfig config = IOReactorConfig.custom()
            .setIoThreadCount(threads).setSoReuseAddress(true).build();
    ListeningIOReactor ioReactor = new DefaultListeningIOReactor(config);
    // Start server
    ioReactor.listen(new InetSocketAddress(port));
    ioReactor.execute(ioEventDispatch);
}
项目:PhET    文件:ElementalEchoServer.java   
public static void main(String[] args) throws Exception {
    HttpParams params = new SyncBasicHttpParams(); 
    IOEventDispatch ioEventDispatch = new DefaultIoEventDispatch();
    ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
    ioReactor.listen(new InetSocketAddress(8080));
    try {
        ioReactor.execute(ioEventDispatch);
    } catch (InterruptedIOException ex) {
        System.err.println("Interrupted");
    } catch (IOException e) {
        System.err.println("I/O error: " + e.getMessage());
    }
    System.out.println("Shutdown");
}
项目:LibOppo    文件:OppoMessenger.java   
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public OppoMessenger(final String remoteHost, final int remotePort, final int localPort,
                     final HttpClientService httpClient)
        throws IOException {
    this.httpClient = httpClient;

    // Set up the server
    final HttpProcessor processor = HttpProcessorBuilder.create()
            .add(new ResponseContent())
            .add(new ResponseContentEncoding())
            .add(new ResponseConnControl())
            .build();

    final HttpAsyncService service = new HttpAsyncService(processor, mapper);
    final NHttpConnectionFactory<DefaultNHttpServerConnection> connectionFactory = new DefaultNHttpServerConnectionFactory(ConnectionConfig.DEFAULT);
    final IOEventDispatch dispatch = new DefaultHttpServerIODispatch(service, connectionFactory);

    server = new DefaultListeningIOReactor(IOReactorConfig.DEFAULT);
    server.listen(new InetSocketAddress(localPort));

    new Thread(new Runnable() {
        @Override public void run() {
            try {
                server.execute(dispatch);
            } catch (final IOException e) {
                logger().level(Error).message("HTTP server failed").error(e).log();
            }
        }
    }, "Oppo HTTP server");

    // Set up the client
    deviceUrlBase = "http://" + remoteHost + ':' + remotePort + '/';
}
项目:jsonrpc4j    文件:JsonRpcHttpAsyncClient.java   
private void initialize() {
    if (initialized.getAndSet(true)) {
        return;
    }
    IOReactorConfig.Builder config = createConfig();
    // params.setParameter(CoreProtocolPNames.USER_AGENT, "jsonrpc4j/1.0");
    final ConnectingIOReactor ioReactor = createIoReactor(config);
    createSslContext();
    int socketBufferSize = Integer.getInteger("com.googlecode.jsonrpc4j.async.socket.buffer", 8 * 1024);
    final ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(socketBufferSize).build();
    BasicNIOConnFactory nioConnFactory = new BasicNIOConnFactory(sslContext, null, connectionConfig);
    pool = new BasicNIOConnPool(ioReactor, nioConnFactory, Integer.getInteger("com.googlecode.jsonrpc4j.async.connect.timeout", 30000));
    pool.setDefaultMaxPerRoute(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.route", 500));
    pool.setMaxTotal(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.total", 500));

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
                IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, sslContext, connectionConfig);
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }
        }
    }, "jsonrpc4j HTTP IOReactor");

    t.setDaemon(true);
    t.start();

    HttpProcessor httpProcessor = new ImmutableHttpProcessor(new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue(false));
    requester = new HttpAsyncRequester(httpProcessor, new DefaultConnectionReuseStrategy());
}
项目:PhET    文件:NHttpClient.java   
public static void main(String[] args) throws Exception {
    HttpParams params = new SyncBasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");

    final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params);

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
            new RequestContent(),
            new RequestTargetHost(),
            new RequestConnControl(),
            new RequestUserAgent(),
            new RequestExpectContinue()});

    // We are going to use this object to synchronize between the 
    // I/O event and main threads
    CountDownLatch requestCount = new CountDownLatch(3);

    BufferingHttpClientHandler handler = new BufferingHttpClientHandler(
            httpproc,
            new MyHttpRequestExecutionHandler(requestCount),
            new DefaultConnectionReuseStrategy(),
            params);

    handler.setEventListener(new EventLogger());

    final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);

    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }
            System.out.println("Shutdown");
        }

    });
    t.start();

    SessionRequest[] reqs = new SessionRequest[3];
    reqs[0] = ioReactor.connect(
            new InetSocketAddress("www.yahoo.com", 80), 
            null, 
            new HttpHost("www.yahoo.com"),
            new MySessionRequestCallback(requestCount));
    reqs[1] = ioReactor.connect(
            new InetSocketAddress("www.google.com", 80), 
            null,
            new HttpHost("www.google.ch"),
            new MySessionRequestCallback(requestCount));
    reqs[2] = ioReactor.connect(
            new InetSocketAddress("www.apache.org", 80), 
            null,
            new HttpHost("www.apache.org"),
            new MySessionRequestCallback(requestCount));

    // Block until all connections signal
    // completion of the request execution
    requestCount.await();

    System.out.println("Shutting down I/O reactor");

    ioReactor.shutdown();

    System.out.println("Done");
}
项目:PhET    文件:NHttpFileServer.java   
public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specify document root directory");
        System.exit(1);
    }

    boolean useFileChannels = true;
    if (args.length >= 2) {
        String s = args[1];
        if (s.equalsIgnoreCase("disableFileChannels")) {
            useFileChannels = false;
        }
    }

    HttpParams params = new SyncBasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 20000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
            new ResponseDate(),
            new ResponseServer(),
            new ResponseContent(),
            new ResponseConnControl()
    });

    AsyncNHttpServiceHandler handler = new AsyncNHttpServiceHandler(
            httpproc,
            new DefaultHttpResponseFactory(),
            new DefaultConnectionReuseStrategy(),
            params);

    final HttpFileHandler filehandler = new HttpFileHandler(args[0], useFileChannels);

    NHttpRequestHandlerResolver resolver = new NHttpRequestHandlerResolver() {

        public NHttpRequestHandler lookup(String requestURI) {
            return filehandler;
        }

    };

    handler.setHandlerResolver(resolver);

    // Provide an event logger
    handler.setEventListener(new EventLogger());

    IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params);
    ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
    try {
        ioReactor.listen(new InetSocketAddress(8080));
        ioReactor.execute(ioEventDispatch);
    } catch (InterruptedIOException ex) {
        System.err.println("Interrupted");
    } catch (IOException e) {
        System.err.println("I/O error: " + e.getMessage());
    }
    System.out.println("Shutdown");
}
项目:PhET    文件:NHttpClientConnManagement.java   
public void execute() throws IOException {
    IOEventDispatch dispatch = new DefaultClientIOEventDispatch(
            new ManagedClientHandler(this.handler, this), this.params);
    this.ioreactor.execute(dispatch);
}
项目:PhET    文件:NHttpServer.java   
public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specify document root directory");
        System.exit(1);
    }
    HttpParams params = new SyncBasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
            new ResponseDate(),
            new ResponseServer(),
            new ResponseContent(),
            new ResponseConnControl()
    });

    BufferingHttpServiceHandler handler = new BufferingHttpServiceHandler(
            httpproc,
            new DefaultHttpResponseFactory(),
            new DefaultConnectionReuseStrategy(),
            params);

    // Set up request handlers
    HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
    reqistry.register("*", new HttpFileHandler(args[0]));

    handler.setHandlerResolver(reqistry);

    // Provide an event logger
    handler.setEventListener(new EventLogger());

    IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params);
    ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
    try {
        ioReactor.listen(new InetSocketAddress(8080));
        ioReactor.execute(ioEventDispatch);
    } catch (InterruptedIOException ex) {
        System.err.println("Interrupted");
    } catch (IOException e) {
        System.err.println("I/O error: " + e.getMessage());
    }
    System.out.println("Shutdown");
}
项目:PhET    文件:NHttpSSLServer.java   
public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specify document root directory");
        System.exit(1);
    }

    ClassLoader cl = NHttpSSLServer.class.getClassLoader();
    URL url = cl.getResource("test.keystore");
    KeyStore keystore  = KeyStore.getInstance("jks");
    keystore.load(url.openStream(), "nopassword".toCharArray());
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
            KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keystore, "nopassword".toCharArray());
    KeyManager[] keymanagers = kmfactory.getKeyManagers(); 
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(keymanagers, null, null);

    HttpParams params = new SyncBasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "Jakarta-HttpComponents-NIO/1.1");

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
            new ResponseDate(),
            new ResponseServer(),
            new ResponseContent(),
            new ResponseConnControl()
    });

    BufferingHttpServiceHandler handler = new BufferingHttpServiceHandler(
            httpproc,
            new DefaultHttpResponseFactory(),
            new DefaultConnectionReuseStrategy(),
            params);

    // Set up request handlers
    HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
    reqistry.register("*", new HttpFileHandler(args[0]));

    handler.setHandlerResolver(reqistry);

    // Provide an event logger
    handler.setEventListener(new EventLogger());

    IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
            handler, 
            sslcontext,
            params);

    ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
    try {
        ioReactor.listen(new InetSocketAddress(8080));
        ioReactor.execute(ioEventDispatch);
    } catch (InterruptedIOException ex) {
        System.err.println("Interrupted");
    } catch (IOException e) {
        System.err.println("I/O error: " + e.getMessage());
    }
    System.out.println("Shutdown");
}
项目:esper    文件:EsperHttpServiceNIO.java   
public void start(EPServiceProviderSPI engine) {
    HttpParams parameters = new BasicHttpParams();
    parameters
            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());

    BufferingHttpServiceHandler handler = new BufferingHttpServiceHandler(
            httpproc,
            new DefaultHttpResponseFactory(),
            new DefaultConnectionReuseStrategy(),
            parameters);

    // Set up request handlers
    HttpRequestHandlerRegistry reqistry = setupRegistry(engine);

    handler.setHandlerResolver(reqistry);

    // Provide an event logger
    handler.setEventListener(new EventLogger());

    IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, parameters);
    try {
        ioReactor = new DefaultListeningIOReactor(2, parameters);
        ioReactor.listen(new InetSocketAddress(this.getServiceConfig().getPort()));
    } catch (IOException e) {
        log.error("I/O for service '" + this.getServiceName() + "' error: " + e.getMessage());
        return;
    }

    runnable = new EsperHttpServiceNIORunnable(this.getServiceName(), this.getServiceConfig().getPort(), ioReactor, ioEventDispatch);
    reactorThread = new Thread(runnable);
    reactorThread.setDaemon(true);
    reactorThread.start();
}
项目:esper    文件:EsperHttpServiceNIORunnable.java   
public EsperHttpServiceNIORunnable(String serviceName, int port, ListeningIOReactor ioReactor, IOEventDispatch ioEventDispatch) {
    this.serviceName = serviceName;
    this.port = port;
    this.ioReactor = ioReactor;
    this.ioEventDispatch = ioEventDispatch;
}