I'm trying to read a csv text file from S3 and then send each of its lines to a distributed queue to get them processed.
When trying to read it, I'm getting "java.net.SocketException: Socket is closed" Exception at different points of the file being read (in different executions). This is the code:
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(MyClass.class.getResourceAsStream("myCredentials.properties")));
String bucketName = "myBucket";
String key = "myFile";
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
InputStream in = object.getObjectContent();
BufferedReader readerS3 = new BufferedReader(new InputStreamReader(in, Charset.forName(fileInfo.getEncoding())));
try {
String line = null;
while ((line = readerS3.readLine()) != null) {
// Sending the line to a distributed queue
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Any idea on how to solve this issue?
UPDATE:
This exception occurs from the second time I run the method, if I stop the whole program and run it again, then the first time I run the method it works ok.
Closed socket connection - The most common cause of SocketException is reading or writing from or to a closed socket connection. It can also occur when the connection is closed before all the data is read in the socket buffer. Slow network - A poor network connection might also cause a SocketException .
java.net.SocketException: Connection reset This SocketException occurs on the server-side when the client closed the socket connection before the response could be returned over the socket. For example, by quitting the browser before the response was retrieved. Connection reset simply means that a TCP RST was received.
The error does gives us some hint as follows : org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset. Which essentially implies that ChromeDriver binary is unable to spawn a new Chrome Browser process.
As suggested by "jsn" in comments to question, the problem is that you need to configure AmazonS3
with ClientConfiguration
:
ClientConfiguration config = new ClientConfiguration();
config.setSocketTimeout(0);
AmazonS3 s3 = new AmazonS3Client(/* credentials */, config);
Thanks, @jsn, your suggestion was my issue.
I have a method that returns just the InputStream so the AmazonS3 object gets garbage collected and that causes it to close the InputStream.
I've made it keep a reference to the AmazonS3 object and that fixed my issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With