AWS Lambda-缓冲读取器 [英] AWS Lambda - Buffered reader

查看:123
本文介绍了AWS Lambda-缓冲读取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS Lambda上使用Java来获取网站的URL源代码.我有以下代码:

I am using Java on AWS Lambda to get the URL source code of the site. 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());

在某些站点上,代码可以正常运行.每次在我的本地计算机上运行都很好.但是,在AWS Lambda上运行时,它会卡在以下部分上:

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:

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

然后我得到:任务在20.00秒后超时.

Then I get: Task timed out after 20.00 seconds.

在Lambda日志中,出现以下错误:

In the Lambda log, I get the following error:

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

Payload: 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?

非常感谢所有答案.

推荐答案

实现此目的的简单解决方案-将Lambda移出当前使用的VPC.

The simple solution for making this work - is putting your Lambda out of the VPC it's in right now.

在此线程上阅读我的答案,以详细了解为什么会发生这种情况.

Read my answer on this thread for detailed explanation on why this happens to you.

AWS lambda调用不调用另一个lambda函数-Node.js

(注意:答案与NodeJS无关)

(note: the answer is not related to NodeJS)

这篇关于AWS Lambda-缓冲读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆