On Mon, 2018-06-11 at 23:33 +0800, zhangminglei wrote:
> Hi, Oleg. I fixed it. Thank you. Codes below is nice.
>
>
I would not dare call anything with Exception#e.printStackTrace in it
nice, but that certainly should fix the resource leak issue.
Oleg
>
> public class HttpClientWithSync {
> public static void main(String[] args) {
>
> String url = "https://www.baidu.com/";
>
> String url1 = "http://hc.apache.org/httpcomponents-client-4.5.x
> /tutorial/html/fundamentals.html";
>
> String url2 = "http://news.baidu.com/";
>
> String[] strs = new String[] {url, url, url};
>
> CloseableHttpClient httpclient = HttpClients.createDefault();
>
> for (int i = 0; i < 3; i++) {
> HttpGet httpget = new HttpGet(strs[i]);
> System.out.println();
> CloseableHttpResponse response = null;
> try {
> System.out.println("Waiting for the contents, hurry!
> hurry ! hurry!.....");
>
> // take a while and block the main thread.
> response = httpclient.execute(httpget);
>
> if (response.getStatusLine().getStatusCode() ==
> HttpStatus.SC_OK) {
> System.out.println("============ Get the entity
> ============ "+ response.getEntity());
> }
> // after getting the value from the url and then do this
> or do not if something unexpected to happen.
> System.out.println("Do some other stuff");
> } catch (IOException e) {
> e.printStackTrace();
> } finally {
> try {
> response.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
> }
>
>
>
> > 在 2018年6月11日,下午11:26,zhangminglei <[email protected]> 写道:
> >
> > Yes. I know it is leaking connections…. But it can stop the program
> > in the end. If I make the url the same, it can not stop….
> >
> > > 在 2018年6月11日,下午11:16,Oleg Kalnichevski <[email protected] <mailto:
> > > [email protected]>> 写道:
> > >
> > > On Mon, 2018-06-11 at 23:13 +0800, zhangminglei wrote:
> > > > Hi, Oleg.
> > > >
> > > > I found the interesting thing. If I change the code like the
> > > > below,
> > > > everything will be fine.
> > >
> > > No, it will not. Your code keeps on leaking connections. Please
> > > fix it.
> > >
> > > Oleg
> > >
> > >
> > > > I just make the url different from one of them. Url, Url1, Url2
> > > > .
> > > > This time, the program can stoped. Confused…..
> > > >
> > > > Minglei.
> > > >
> > > > public class HttpClientWithSync {
> > > > public static void main(String[] args) {
> > > >
> > > > String url = "https://www.baidu.com/";
> > > >
> > > > String url1 = "http://hc.apache.org/httpcomponents-client-
> > > > 4.5.x
> > > > /tutorial/html/fundamentals.html";
> > > >
> > > > String url2 = "http://news.baidu.com/";
> > > >
> > > > String[] strs = new String[] {url, url1, url2};
> > > >
> > > > CloseableHttpClient httpclient =
> > > > HttpClients.createDefault();
> > > >
> > > > for (int i = 0; i < 3; i++) {
> > > > HttpGet httpget = new HttpGet(strs[i]);
> > > > System.out.println();
> > > > try {
> > > > System.out.println("Waiting for the contents, hurry!
> > > > hurry ! hurry!.....");
> > > >
> > > > // take a while and block the main thread.
> > > > HttpResponse response = httpclient.execute(httpget);
> > > >
> > > > if (response.getStatusLine().getStatusCode() ==
> > > > HttpStatus.SC_OK) {
> > > > System.out.println("============ Get the entity
> > > > ============ "+ response.getEntity());
> > > > }
> > > > // after getting the value from the url and then do
> > > > this
> > > > or do not if something unexpected to happen.
> > > > System.out.println("Do some other stuff");
> > > > } catch (IOException e) {
> > > > e.printStackTrace();
> > > > }
> > > > }
> > > > try {
> > > > httpclient.close();
> > > > } catch (IOException e) {
> > > > e.printStackTrace();
> > > > }
> > > > }
> > > > }
> > > >
> > > >
> > > > > 在 2018年6月11日,下午10:27,Oleg Kalnichevski <[email protected]> 写道:
> > > > >
> > > > > On Mon, 2018-06-11 at 21:12 +0800, zhangminglei wrote:
> > > > > > Hi, Oleg,
> > > > > >
> > > > > > Thank you for your response, But my question is why this
> > > > > > program
> > > > > > never stopped ? And always running…
> > > > > >
> > > > >
> > > > > The first two requests cause all available connections to get
> > > > > leaked.
> > > > > All subsequent requests block indefinitely waiting for
> > > > > connections
> > > > > to
> > > > > become available in the connection pool.
> > > > >
> > > > > Oleg
> > > > >
> > > > > > Minglei.
> > > > > >
> > > > > > > 在 2018年6月11日,下午8:27,Oleg Kalnichevski <[email protected]>
> > > > > > > 写道:
> > > > > > >
> > > > > > > On Fri, 2018-06-08 at 15:30 +0800, zhangminglei wrote:
> > > > > > > > Hi, friends.
> > > > > > > >
> > > > > > > > I am using HttpClient for a few tests. And I use the
> > > > > > > > below
> > > > > > > > code
> > > > > > > > to
> > > > > > > > run without success. But the process never stoped.
> > > > > > > > I use jstack for this and found it always wait for
> > > > > > > > something.
> > > > > > > > Could
> > > > > > > > you help me please ? Thanks.
> > > > > > > >
> > > > > > > > parking to wait for <0x000000076c1d0980> (a
> > > > > > > > java.util.concurrent.locks.AbstractQueuedSynchronizer$C
> > > > > > > > onditi
> > > > > > > > onOb
> > > > > > > > ject
> > > > > > > > )
> > > > > > > >
> > > > > > > > Best
> > > > > > > > Minglei
> > > > > > > >
> > > > > > > > public class HttpClientWithSync {
> > > > > > > >
> > > > > > > > public static void main(String[] args) {
> > > > > > > >
> > > > > > > > String url = "https://www.baidu.com/";
> > > > > > > >
> > > > > > > > String[] strs = new String[] {url, url, url};
> > > > > > > >
> > > > > > > > CloseableHttpClient httpclient =
> > > > > > > > HttpClients.createDefault();
> > > > > > > >
> > > > > > > > for (int i = 0; i < 3; i++) {
> > > > > > > > HttpGet httpget = new HttpGet(strs[i]);
> > > > > > > > System.out.println();
> > > > > > > > try {
> > > > > > > > System.out.println("Waiting for the
> > > > > > > > contents,
> > > > > > > > hurry!
> > > > > > > > hurry ! hurry!.....");
> > > > > > > >
> > > > > > > > // take a while and block the main thread.
> > > > > > > > HttpResponse response =
> > > > > > > > httpclient.execute(httpget);
> > > > > > > >
> > > > > > > > if (response.getStatusLine().getStatusCode()
> > > > > > > > ==
> > > > > > > > HttpStatus.SC_OK) {
> > > > > > > > System.out.println("============ Get the
> > > > > > > > entity
> > > > > > > > ============ "+ response.getEntity());
> > > > > > > > }
> > > > > > > > // after getting the value from the url and
> > > > > > > > then
> > > > > > > > do
> > > > > > > > this
> > > > > > > > or do not if something unexpected to happen.
> > > > > > > > System.out.println("Do some other stuff");
> > > > > > > > } catch (IOException e) {
> > > > > > > > e.printStackTrace();
> > > > > > > > }
> > > > > > > > }
> > > > > > > > try {
> > > > > > > > httpclient.close();
> > > > > > > > } catch (IOException e) {
> > > > > > > > e.printStackTrace();
> > > > > > > > }
> > > > > > > > }
> > > > > > > > }
> > > > > > > >
> > > > > > >
> > > > > > > Your code is leaking connections.
> > > > > > >
> > > > > > > Please see
> > > > > > >
> > > > > > > http://hc.apache.org/httpcomponents-client-4.5.x/tutorial
> > > > > > > /html/
> > > > > > > fund
> > > > > > > amen <http://hc.apache.org/httpcomponents-client-
> > > > > > > 4.5.x/tutorial/html/fundamen>
> > > > > > > tals.html#d5e145
> > > > > > >
> > > > > > > Oleg
> > > > > > >
> > > > > > >
> > > > > > > >
> > > > > > > > The full thread dump are
> > > > > > > >
> > > > > > > > 2018-06-08 11:02:08
> > > > > > > > Full thread dump Java HotSpot(TM) 64-Bit Server VM
> > > > > > > > (25.161-
> > > > > > > > b12
> > > > > > > > mixed
> > > > > > > > mode):
> > > > > > > >
> > > > > > > > "Attach Listener" #12 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce8904a000
> > > > > > > > nid=0x1207 waiting on condition [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "Service Thread" #10 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce8c04b800
> > > > > > > > nid=0x5503 runnable [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "C1 CompilerThread3" #9 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce8c002800 nid=0x5303 waiting on condition
> > > > > > > > [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "C2 CompilerThread2" #8 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce89084000 nid=0x5103 waiting on condition
> > > > > > > > [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "C2 CompilerThread1" #7 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce8c002000 nid=0x4f03 waiting on condition
> > > > > > > > [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "C2 CompilerThread0" #6 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce8c001000 nid=0x4d03 waiting on condition
> > > > > > > > [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "Monitor Ctrl-Break" #5 daemon prio=5 os_prio=31
> > > > > > > > tid=0x00007fce8a83b800 nid=0x4b03 runnable
> > > > > > > > [0x0000700007e38000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > > at java.net.SocketInputStream.socketRead0(Native
> > > > > > > > Method)
> > > > > > > > at
> > > > > > > > java.net.SocketInputStream.socketRead(SocketInputStream
> > > > > > > > .java:
> > > > > > > > 116)
> > > > > > > > at
> > > > > > > > java.net.SocketInputStream.read(SocketInputStream.java:
> > > > > > > > 171)
> > > > > > > > at
> > > > > > > > java.net.SocketInputStream.read(SocketInputStream.java:
> > > > > > > > 141)
> > > > > > > > at
> > > > > > > > sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:2
> > > > > > > > 84)
> > > > > > > > at
> > > > > > > > sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:32
> > > > > > > > 6)
> > > > > > > > at
> > > > > > > > sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
> > > > > > > > - locked <0x000000076adce880> (a
> > > > > > > > java.io.InputStreamReader)
> > > > > > > > at
> > > > > > > > java.io.InputStreamReader.read(InputStreamReader.java:1
> > > > > > > > 84)
> > > > > > > > at
> > > > > > > > java.io.BufferedReader.fill(BufferedReader.java:161)
> > > > > > > > at
> > > > > > > > java.io.BufferedReader.readLine(BufferedReader.java:324
> > > > > > > > )
> > > > > > > > - locked <0x000000076adce880> (a
> > > > > > > > java.io.InputStreamReader)
> > > > > > > > at
> > > > > > > > java.io.BufferedReader.readLine(BufferedReader.java:389
> > > > > > > > )
> > > > > > > > at
> > > > > > > > com.intellij.rt.execution.application.AppMainV2$1.run(A
> > > > > > > > ppMain
> > > > > > > > V2.j
> > > > > > > > ava:
> > > > > > > > 64)
> > > > > > > >
> > > > > > > > "Signal Dispatcher" #4 daemon prio=9 os_prio=31
> > > > > > > > tid=0x00007fce8881c000 nid=0x4903 runnable
> > > > > > > > [0x0000000000000000]
> > > > > > > > java.lang.Thread.State: RUNNABLE
> > > > > > > >
> > > > > > > > "Finalizer" #3 daemon prio=8 os_prio=31
> > > > > > > > tid=0x00007fce89003800
> > > > > > > > nid=0x3903 in Object.wait() [0x0000700007c32000]
> > > > > > > > java.lang.Thread.State: WAITING (on object monitor)
> > > > > > > > at java.lang.Object.wait(Native Method)
> > > > > > > > - waiting on <0x000000076ab08ec0> (a
> > > > > > > > java.lang.ref.ReferenceQueue$Lock)
> > > > > > > > at
> > > > > > > > java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java
> > > > > > > > :143)
> > > > > > > > - locked <0x000000076ab08ec0> (a
> > > > > > > > java.lang.ref.ReferenceQueue$Lock)
> > > > > > > > at
> > > > > > > > java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java
> > > > > > > > :164)
> > > > > > > > at
> > > > > > > > java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.j
> > > > > > > > ava:20
> > > > > > > > 9)
> > > > > > > >
> > > > > > > > "Reference Handler" #2 daemon prio=10 os_prio=31
> > > > > > > > tid=0x00007fce8b004000 nid=0x3703 in Object.wait()
> > > > > > > > [0x0000700007b2f000]
> > > > > > > > java.lang.Thread.State: WAITING (on object monitor)
> > > > > > > > at java.lang.Object.wait(Native Method)
> > > > > > > > - waiting on <0x000000076ab06b68> (a
> > > > > > > > java.lang.ref.Reference$Lock)
> > > > > > > > at java.lang.Object.wait(Object.java:502)
> > > > > > > > at
> > > > > > > > java.lang.ref.Reference.tryHandlePending(Reference.java
> > > > > > > > :191)
> > > > > > > > - locked <0x000000076ab06b68> (a
> > > > > > > > java.lang.ref.Reference$Lock)
> > > > > > > > at
> > > > > > > > java.lang.ref.Reference$ReferenceHandler.run(Reference.
> > > > > > > > java:1
> > > > > > > > 53)
> > > > > > > >
> > > > > > > > "main" #1 prio=5 os_prio=31 tid=0x00007fce8a008000
> > > > > > > > nid=0x1c03
> > > > > > > > waiting
> > > > > > > > on condition [0x0000700007110000]
> > > > > > > > java.lang.Thread.State: WAITING (parking)
> > > > > > > > at sun.misc.Unsafe.park(Native Method)
> > > > > > > > - parking to wait for <0x000000076c1d0980> (a
> > > > > > > > java.util.concurrent.locks.AbstractQueuedSynchronizer$C
> > > > > > > > onditi
> > > > > > > > onOb
> > > > > > > > ject
> > > > > > > > )
> > > > > > > > at
> > > > > > > > java.util.concurrent.locks.LockSupport.park(LockSupport
> > > > > > > > .java:
> > > > > > > > 175)
> > > > > > > > at
> > > > > > > > java.util.concurrent.locks.AbstractQueuedSynchronizer$C
> > > > > > > > onditi
> > > > > > > > onOb
> > > > > > > > ject
> > > > > > > > .await(AbstractQueuedSynchronizer.java:2039)
> > > > > > > > at
> > > > > > > > org.apache.http.pool.AbstractConnPool.getPoolEntryBlock
> > > > > > > > ing(Ab
> > > > > > > > stra
> > > > > > > > ctCo
> > > > > > > > nnPool.java:377)
> > > > > > > > at
> > > > > > > > org.apache.http.pool.AbstractConnPool.access$200(Abstra
> > > > > > > > ctConn
> > > > > > > > Pool
> > > > > > > > .jav
> > > > > > > > a:67)
> > > > > > > > at
> > > > > > > > org.apache.http.pool.AbstractConnPool$2.get(AbstractCon
> > > > > > > > nPool.
> > > > > > > > java
> > > > > > > > :243
> > > > > > > > )
> > > > > > > > - locked <0x000000076d160008> (a
> > > > > > > > org.apache.http.pool.AbstractConnPool$2)
> > > > > > > > at
> > > > > > > > org.apache.http.pool.AbstractConnPool$2.get(AbstractCon
> > > > > > > > nPool.
> > > > > > > > java
> > > > > > > > :191
> > > > > > > > )
> > > > > > > > at
> > > > > > > > org.apache.http.impl.conn.PoolingHttpClientConnectionMa
> > > > > > > > nager.
> > > > > > > > leas
> > > > > > > > eCon
> > > > > > > > nection(PoolingHttpClientConnectionManager.java:282)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.conn.PoolingHttpClientConnectionMa
> > > > > > > > nager$
> > > > > > > > 1.ge
> > > > > > > > t(Po
> > > > > > > > olingHttpClientConnectionManager.java:269)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.execchain.MainClientExec.execute(M
> > > > > > > > ainCli
> > > > > > > > entE
> > > > > > > > xec.
> > > > > > > > java:191)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.execchain.ProtocolExec.execute(Pro
> > > > > > > > tocolE
> > > > > > > > xec.
> > > > > > > > java
> > > > > > > > :185)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.execchain.RetryExec.execute(RetryE
> > > > > > > > xec.ja
> > > > > > > > va:8
> > > > > > > > 9)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.execchain.RedirectExec.execute(Red
> > > > > > > > irectE
> > > > > > > > xec.
> > > > > > > > java
> > > > > > > > :111)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.client.InternalHttpClient.doExecut
> > > > > > > > e(Inte
> > > > > > > > rnal
> > > > > > > > Http
> > > > > > > > Client.java:185)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.client.CloseableHttpClient.execute
> > > > > > > > (Close
> > > > > > > > able
> > > > > > > > Http
> > > > > > > > Client.java:83)
> > > > > > > > at
> > > > > > > > org.apache.http.impl.client.CloseableHttpClient.execute
> > > > > > > > (Close
> > > > > > > > able
> > > > > > > > Http
> > > > > > > > Client.java:108)
> > > > > > > > at
> > > > > > > > org.apache.flink.streaming.connectors.fs.HttpClientWith
> > > > > > > > Sync.m
> > > > > > > > ain(
> > > > > > > > Http
> > > > > > > > ClientWithSync.java:37)
> > > > > > > >
> > > > > > > > "VM Thread" os_prio=31 tid=0x00007fce88819000
> > > > > > > > nid=0x3503
> > > > > > > > runnable
> > > > > > > >
> > > > > > > > "GC task thread#0 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce8a014000
> > > > > > > > nid=0x2503 runnable
> > > > > > > >
> > > > > > > > "GC task thread#1 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce8a014800
> > > > > > > > nid=0x2703 runnable
> > > > > > > >
> > > > > > > > "GC task thread#2 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce89004800
> > > > > > > > nid=0x2903 runnable
> > > > > > > >
> > > > > > > > "GC task thread#3 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce88800800
> > > > > > > > nid=0x2b03 runnable
> > > > > > > >
> > > > > > > > "GC task thread#4 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce88808800
> > > > > > > > nid=0x2d03 runnable
> > > > > > > >
> > > > > > > > "GC task thread#5 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce88809000
> > > > > > > > nid=0x2f03 runnable
> > > > > > > >
> > > > > > > > "GC task thread#6 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce89005800
> > > > > > > > nid=0x3103 runnable
> > > > > > > >
> > > > > > > > "GC task thread#7 (ParallelGC)" os_prio=31
> > > > > > > > tid=0x00007fce89006000
> > > > > > > > nid=0x3303 runnable
> > > > > > > >
> > > > > > > > "VM Periodic Task Thread" os_prio=31
> > > > > > > > tid=0x00007fce888c7000
> > > > > > > > nid=0x5703 waiting on condition
> > > > > > > >
> > > > > > > > JNI global references: 364
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > > -------------------------------------------------------
> > > > > > > ------
> > > > > > > ----
> > > > > > > ----
> > > > > > > To unsubscribe, e-mail: [email protected]
> > > > > > > pache.
> > > > > > > org
> > > > > > > <mailto:[email protected]>
> > > > > > > For additional commands, e-mail: httpclient-users-help@hc
> > > > > > > .apach
> > > > > > > e.or
> > > > > > > g <mailto:[email protected]>
> > > > >
> > > > > -----------------------------------------------------------
> > > > > ------
> > > > > ----
> > > > > To unsubscribe, e-mail: [email protected]
> > > > > e.org
> > > > > <mailto:[email protected] <mailto:ht
> > > > > [email protected]> <mailto:httpclient-
> > > > > [email protected] <mailto:httpclient-users-unsu
> > > > > [email protected]>>>
> > > > > For additional commands, e-mail: [email protected]
> > > > > che.or <mailto:[email protected]> <mailto:ht
> > > > > [email protected] <mailto:httpclient-users-hel
> > > > > [email protected]>>
> > > > > g <mailto:[email protected] <mailto:httpcli
> > > > > [email protected]> <mailto:httpclient-users-help@h
> > > > > c.apache.org <mailto:[email protected]>>>
> > >
> > > ---------------------------------------------------------------
> > > ------
> > > To unsubscribe, e-mail: [email protected]
> > > g <mailto:[email protected]> <mailto:htt
> > > [email protected] <mailto:httpclient-users-
> > > [email protected]>>
> > > For additional commands, e-mail: [email protected].
> > > org <mailto:[email protected]> <mailto:httpclie
> > > [email protected] <mailto:[email protected]
> > > he.org>>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]