简体   繁体   English

AWS Lambda-缓冲读取器

[英]AWS Lambda - Buffered reader

I am using Java on AWS Lambda to get the URL source code of the site. 我在AWS Lambda上使用Java获取网站的URL源代码。 I have the following code: 我有以下代码:

URL yahoo = new URL(url);
URLConnection yc = yahoo.openConnection();
yc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
BufferedReader in = new BufferedReader(newInputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)a.append(inputLine);
in.close();
System.out.println(a.toString());

With some sites, the code runs absolutely fine. 在某些站点上,代码可以正常运行。 It runs fine every time on my local machine. 每次在我的本地计算机上运行都很好。 However, when running on AWS Lambda, it gets stuck on the following part: 但是,在AWS Lambda上运行时,它会卡在以下部分上:

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));

Then I get: Task timed out after 20.00 seconds. 然后我得到:任务在20.00秒后超时。

In the Lambda log, I get the following error: 在Lambda日志中,出现以下错误:

Payload: java.nio.HeapByteBuffer[pos=0 lim=115 cap=115] 有效负载:java.nio.HeapByteBuffer [pos = 0 lim = 115 cap = 115]

My guess is, does it have something to do with encoding? 我的猜测是,它与编码有关吗? Why some site are processed absolutely fine and with some it gets stuck on that line of code? 为什么某些站点经过绝对正确的处理,而有些却停留在该行代码上?

Thanks a lot for all answers. 非常感谢您提供所有答案。

The simple solution for making this work - is putting your Lambda out of the VPC it's in right now. 实现此工作的简单解决方案-将Lambda退出目前使用的VPC

Read my answer on this thread for detailed explanation on why this happens to you. 阅读我对此线程的回答,以详细了解为什么会发生这种情况。

AWS lambda invoke not calling another lambda function - Node.js AWS Lambda调用不调用另一个Lambda函数-Node.js

(note: the answer is not related to NodeJS) (注意:答案与NodeJS无关)

This is the point at which the connection is made, the request sent, and the first part of the response read. 这是建立连接,发送请求和读取响应的第一部分的时间。 Evidently the server is slow at one or more of those things. 显然,服务器在执行其中一项或多项操作时速度很慢。

I would guess it is related to this bug https://bugs.openjdk.java.net/browse/JDK-8149169 我想这与这个错误有关https://bugs.openjdk.java.net/browse/JDK-8149169

Try the same URL (that causes timeout of the Lambda function) from your local system and see if you can find the root cause. 尝试从本地系统使用相同的URL(这会导致Lambda函数超时),然后查看是否可以找到根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM