“Java.net.socketexception: socket closed” when reading S3 file

I'm trying to read the CSV text file from S3 and send each line to the distributed queue for processing

When trying to read it, I get "Java. Net. Socketexception: socket is closed" exceptions at different points of the file being read (in different execution) 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 ideas on how to solve this problem?

to update:

This exception has occurred since the second time I run the method. If I stop the whole program and run it again, I can run the method for the first time

Solution

Maybe you should close readers3 at the end instead of 'in' That is, close the outermost object, it can close its packaging

If 'in' is closed first, inputstreamreader and BufferedReader are still open. If they try to do anything with the object they wrap, it will be closed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>