简体   繁体   中英

AWS Lambda - Buffered reader

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());

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"));

Then I get: Task timed out after 20.00 seconds.

In the Lambda log, I get the following error:

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?

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.

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

(note: the answer is not related to 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

Try the same URL (that causes timeout of the Lambda function) from your local system and see if you can find the root cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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