On Mon, 2011-10-31 at 17:49 -0400, Bill Speirs wrote: > Yea, I don't think that the IOReactor is the issue... I think something > else is taking all the sockets and there are none left for the IOReactor to > call accept. > > Do I need to consume all of the data from the entity before calling close > on the InputStream?
No, you do not. > For error conditions I thought that I should call > abort(). > Correct. The difference is that EntityUtils#consume will try to salvage the underlying connection, whereas HttpUriRequest#abort will not. The important point is to use try-finally to ensure connection release in all cases. > Also, if there is a read timeout on a connection to a host and that > connection is never closed, what happens when I ask the connection manager > for a connection to that same host/route? Would it still hand-out that bad > connection? > HttpClient automatically shuts down the underlying connection in case of an I/O exception including a timeout. The connection manager will evict closed connection immediately upon release. Hope this helps Oleg > Bill- > On Oct 31, 2011 5:31 PM, "Oleg Kalnichevski" <[email protected]> wrote: > > > On Mon, 2011-10-31 at 15:26 -0400, Bill Speirs wrote: > > > I'm having an issue with the NIO Reactor. I'm getting the following > > exception: > > > > > > org.apache.http.nio.reactor.IOReactorException: Failure accepting > > connection > > > at > > org.apache.http.impl.nio.reactor.DefaultListeningIOReactor.processEvent(DefaultListeningIOReactor.java:169) > > > at > > org.apache.http.impl.nio.reactor.DefaultListeningIOReactor.processEvents(DefaultListeningIOReactor.java:149) > > > at > > org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:370) > > > at my code where I call execute() > > > at java.lang.Thread.run(Thread.java:662) > > > Caused by: java.io.IOException: Too many open files > > > at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) > > > at > > sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:152) > > > at > > org.apache.http.impl.nio.reactor.DefaultListeningIOReactor.processEvent(DefaultListeningIOReactor.java:165) > > > ... 4 more > > > > > > > Bill > > > > The NIO reactor is not necessarily the culprit. Connection descriptors > > may be leaking elsewhere. > > > > > This code does some complex things including opening two connection > > > (via HTTP Client) to other services. I believe that I'm correctly > > > cleaning up those connections by getting the entity in the response to > > > both, and calling close on the stream. > > > > > > // first connection to another service > > > InputStream is = response.getEntity().getContent(); > > > > > > // do stuff with is > > > > > > is.close(); > > > > > > > > > // second connection to another service > > > > > > HttpEntity entity = response.getEntity(); > > > > > > if(entity != null) { > > > // close this to release the connection > > > entity.getContent().close(); > > > } > > > > > > > > > That is all I need to do, correct? There isn't anything else to > > > release the socket is there? > > > > > > > You should probably be releasing connection resources from a try-finally > > clause. This would ensure that resources get correctly released in case > > of an abnormal situation: non 2xx response, runtime exception, etc > > > > HttpResponse response = httpclient.execute(httpget, context); > > try { > > // process response > > } finally { > > EntityUtils.consume(response.getEntity()); > > } > > > > Oleg > > > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [email protected] > > For additional commands, e-mail: [email protected] > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
