.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
.setParameter(CoreProtocolPNames.USER_AGENT, "Jakarta-HttpComponents-NIO/1.1");
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params);
BasicHttpProcessor httpproc = new BasicHttpProcessor();
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());
// Initialize default SSL context
SSLContext sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, null, null);
// We are going to use this object to synchronize between the
// I/O event and main threads
RequestCount requestCount = new RequestCount(3);
BufferingHttpClientHandler handler = new BufferingHttpClientHandler(
httpproc,
new MyHttpRequestExecutionHandler(requestCount),
new DefaultConnectionReuseStrategy(),
params);
handler.setEventListener(new EventLogger());
final IOEventDispatch ioEventDispatch = new SSLClientIOEventDispatch(
handler,
sslcontext,
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[requestCount.getValue()];
reqs[0] = ioReactor.connect(
new InetSocketAddress("www.netscape.com", 443),
null,
new HttpHost("www.netscape.com", 443),
null);
reqs[1] = ioReactor.connect(
new InetSocketAddress("www.verisign.com", 443),
null,
new HttpHost("www.verisign.com", 443),
null);
reqs[2] = ioReactor.connect(
new InetSocketAddress("www.yahoo.com", 443),
null,
new HttpHost("www.yahoo.com", 443),
null);
// Block until all connections signal
// completion of the request execution
synchronized (requestCount) {
while (requestCount.getValue() > 0) {
requestCount.wait();
}
}
System.out.println("Shutting down I/O reactor");
ioReactor.shutdown();
System.out.println("Done");
}