Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38815

Re: it's Closeable, but I don't want to close() it yet.

Newsgroups comp.lang.java.programmer
Date 2019-03-11 07:24 -0700
References <slrnq7dcgo.cfl.avl@logic.at> <a9eaf659-81c4-43d7-9686-32e803da4e3e@googlegroups.com>
Message-ID <c13a9c9f-17f9-43b6-adfe-dab3bc2946eb@googlegroups.com> (permalink)
Subject Re: it's Closeable, but I don't want to close() it yet.
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Monday, March 11, 2019 at 9:27:11 AM UTC-4, Eric Douglas wrote:
> On Wednesday, February 27, 2019 at 10:52:01 AM UTC-5, Andreas Leitgeb wrote:
> > Everything runs fine, except eclipse warns me about spots
> > where the entity is requested, used, and then dropped.
> > Eclipse thinks it might need to be close()d.
> > 
> > Apart from ignoring the warning per eclipse settings or
> > adding @SuppressWarnings, is there maybe a way to tell
> > the compiler that a certain ref is not meant to be close()d?
> > Letting it know that - despite the entity's ultimate fate of
> > being eventually closed - this is not yet the time&place for it?
> 
> I have just been ignoring that compile warning.  I changed that to warn me and tried to refactor my code to not throw warnings.  I found that I have not been closing HttpClient which apparently runs fine, I'm guessing they get closed on cleanup.
> 
> So I'm calling org.apache.http.impl.client.HttpClients.createDefault() which returns a org.apache.http.impl.client.CloseableHttpClient, so Eclipse recognizes it as Closeable even if you return it into the org.apache.http.client.HttpClient which is not Closeable.
> Then you call execute() which returns a org.apache.http.client.methods.CloseableHttpResponse.  This also implements an interface which does not reference close.  So I've found there are 2 solutions to this.
> 1) You can fool this compiler, because it doesn't warn about variables which are passed in or out of methods, assuming the method calling these methods will take care of closing them.  You can do this in 3 methods.  One method calls createDefault and returns the interface, one method calls execute and returns the interface, and one method retrieves the value from the HttpResponse.
> 2) I try to do it all in one method and close the client and response after getting the value, but if it's closed, how do I pass out the value?  HttpResponse has multiple methods.  I may need to getEntity, getStatusLine, getHeaders.  Do I assume I only want the one value, or pass everything out in a new custom object?
> In this case I do only have one method using the HttpResponse which is getting the headers to look for a charset value, defaulting to UTF-8 if it doesn't find one, and passing out a String value from IOUtils.toString(ent.getContent(), charset) but to say that's all you should want to do with the HttpResponse doesn't seem like a safe assumption.

Another annoyance, Eclipse doesn't recognize the close if you put it in another method, so I have a static method to open and read the CloseableHttpClient with 3 possible exit points and I have to put this whole if block on each one.  If I call this method it still complains.
     private static void closeThis(Closeable closeMe) {
          if (closeMe != null) {
               try {
                    closeMe.close();
               } catch (IOException e) {
                    e.printStackTrace();
               }
          }
     }

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar


Thread

it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-02-27 15:51 +0000
  Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-27 08:48 -0800
    Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-02-27 19:03 +0000
      Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-27 11:09 -0800
        Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-02-28 12:20 +0000
          Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 06:19 -0800
            Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 06:52 -0800
              Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 08:24 -0800
                Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 08:34 -0800
                Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 09:00 -0800
                Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 11:12 -0800
                Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 11:26 -0800
                Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 11:33 -0800
                Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 11:37 -0800
                Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 11:41 -0800
                Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 11:48 -0800
                Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 12:51 -0800
                Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-03-01 08:47 +0000
                Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-03-01 09:31 +0000
                Re: it's Closeable, but I don't want to close() it yet. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-03-03 02:01 +0100
                Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-03-04 09:38 +0000
                Re: it's Closeable, but I don't want to close() it yet. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-03-04 12:26 +0100
                Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-03-05 14:34 +0000
                Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-03-05 07:34 -0800
                Re: it's Closeable, but I don't want to close() it yet. Arne Vajhøj <arne@vajhoej.dk> - 2019-03-04 13:59 -0500
  Re: it's Closeable, but I don't want to close() it yet. Marcel Mueller <news.5.maazl@spamgourmet.org> - 2019-02-28 08:30 +0100
    Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-02-28 19:10 +0000
      Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 11:34 -0800
      Re: it's Closeable, but I don't want to close() it yet. Marcel Mueller <news.5.maazl@spamgourmet.org> - 2019-02-28 22:24 +0100
        Re: it's Closeable, but I don't want to close() it yet. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-02-28 23:25 +0100
          Re: it's Closeable, but I don't want to close() it yet. Andreas Leitgeb <avl@logic.at> - 2019-03-01 09:43 +0000
  Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 12:11 -0800
    Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 13:49 -0800
      Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 13:52 -0800
        Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 13:59 -0800
          Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-02-28 16:17 -0800
  Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 12:26 -0800
  Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-02-28 13:44 -0800
  Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-03-01 07:40 -0800
    Re: it's Closeable, but I don't want to close() it yet. bursejan@gmail.com - 2019-03-01 08:34 -0800
  Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-03-11 06:27 -0700
    Re: it's Closeable, but I don't want to close() it yet. Eric Douglas <e.d.programmer@gmail.com> - 2019-03-11 07:24 -0700

csiph-web