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.
Maybe you should be closing readerS3 in your finally instead of ‘in’. I.e. close the outermost object, which can close it’s wrapped children.
If you close ‘in’ first, then the InputStreamReader and BufferedReader are still open and if they try to do anything with the object that they wrap it will already be closed.