at org.apache.http.util.EntityUtils.toString(EntityUtils.java:244) 线程卡死

简述:


          最近遇见做爬虫遇见一个诡异的问题,爬虫的处理流程滞留在一个阶段不忘下执行了。导致出现大量介于爬取开始和爬取结束中间状态停留,从而客户客户天天揪着我问这问那,什么问题又什么时候解决。麻烦的要死,有没有……


         诡异的问题之所以称之为诡异,就是因为他让你摸不到头脑。没有异常也没有日志,线程就在那里杳无音讯了,接着查线程ID,好家伙后面也没有,说明它干私活去了。


问题:

  

         一切都是缘分,瞧今儿一早来。一个surprise,问题复现了,而且现场保存完成。


再查看堆:

"ispSpiderExecutor-1" #42 prio=5 os_prio=0 tid=0x0000000021c21000 nid=0x3858 runnable [0x0000000024fbe000]
   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:170)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136)
	at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152)
	at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270)
	at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:240)
	at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:206)
	at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:169)
	at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
	at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
	at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
	at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
	- locked <0x0000000776edbb08> (a java.io.InputStreamReader)
	at java.io.InputStreamReader.read(InputStreamReader.java:184)
	at java.io.Reader.read(Reader.java:140)
	at org.apache.http.util.EntityUtils.toString(EntityUtils.java:244)
	at org.apache.http.util.EntityUtils.toString(EntityUtils.java:288)
	at com.hbc.api.util.SimpleHttpClientUtil.httpGetRespTxt(SimpleHttpClientUtil.java:77)
	at com.hbc.api.service.YdJxCallClientService.queryBillDetail(YdJxCallClientService.java:341)
	at com.hbc.api.service.YdJxCallClientService.spiderDetailReward(YdJxCallClientService.java:316)
	at com.hbc.api.service.AbstractCallService.spiderDetail(AbstractCallService.java:155)
	at com.hbc.api.service.AbstractCallService.saveToMongodb(AbstractCallService.java:120)
	at com.hbc.api.service.AbstractCallService.synchroData(AbstractCallService.java:829)
	at com.hbc.api.service.YdJxCallClientService.asynchroData(YdJxCallClientService.java:290)
	at com.hbc.api.service.YdJxCallClientService$$FastClassBySpringCGLIB$$77e8e9d4.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- <0x0000000776704e08> (a java.util.concurrent.ThreadPoolExecutor$Worker)

           那么问题变得乐观起来,因为有迹可循呀。

            瞧一瞧,看一看

at org.apache.http.util.EntityUtils.toString(EntityUtils.java:244)

结论:

         是由于使用代理IP,当EntityUtils 流读取一半时,代理ip翘辫子了。导致流卡在了那里,问题找到了,解决办法也便随之而来。


         等解决了再写吧。





  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个使用org.apache.http.client.HttpClient上传文件的Java示例: ```java import java.io.File; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class FileUploader { public static void main(String[] args) { String url = "http://example.com/upload"; String filePath = "/path/to/file.jpg"; HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", new File(filePath)); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); try { httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); String responseString = EntityUtils.toString(responseEntity, "UTF-8"); System.out.println(responseString); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在此示例中,我们使用HttpClient创建一个HttpPost请求对象,并使用MultipartEntityBuilder创建一个multipart/form-data实体,以便我们可以将文件作为二进制数据添加到请求中。最后,我们使用execute方法将请求发送到服务器,并从响应中提取响应字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值