Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"java.net.SocketException: Socket is closed" while reading a S3 file

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.

like image 260
Fgblanch Avatar asked Jul 11 '12 20:07

Fgblanch


People also ask

What does Java net SocketException socket closed mean?

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 .

Is Java net SocketException connection reset?

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.

What is socket exception selenium?

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.


2 Answers

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);
like image 64
yegor256 Avatar answered Oct 17 '22 21:10

yegor256


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.

like image 20
Sarel Botha Avatar answered Oct 17 '22 19:10

Sarel Botha