0

可能重复:
尝试将 InputStream 放入 Amazon S3 时进程终止

  • 我有API一个方法OutputStream getOutputStream(String id)
  • API有两个具体的实现 -FileServiceS3Service
  • 我有一个执行以下操作的 documentManager
public void putDocument(@Nonnull final Document document) throws IllegalArgumentException, PersistenceException {
        final OutputStream stream = persistenceService.getOutputStream(document.getUniqueId());
        try {
            jaxbContext.createMarshaller().marshal(document, stream);
        } catch (final JAXBException e) {
            LOGGER.error(e.getMessage(), e);
            throw new PersistenceException("Failed to marshall document " + document.getUniqueId() + ": " + e.getMessage(), e);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                throw new PersistenceException("Failed to close output stream for " + document.getUniqueId());
            }
        }
    }

persistenceService 返回的写入DocumentManager位置outputStream

  • 此实现适用于FileService
  • S3Service,我们必须将其转换OutputStreamInputStream符合Amazon S3 API此处
  • 所以我确实跟着S3Service.getOutputStream

    public OutputStream getOutputStream(@Nonnull final String uniqueId) throws PersistenceException {
         final PipedOutputStream outputStream = new PipedOutputStream();
         final PipedInputStream inputStream;
         try {
             inputStream = new PipedInputStream(outputStream);
             new Thread(
                     new Runnable() {
                         @Override
                         public void run() {
                             ObjectMetadata objectMetadata = new ObjectMetadata();
                             objectMetadata.setContentType("application/octet-stream");
                             PutObjectRequest putObjectRequest = new     PutObjectRequest("haritdev.sunrun", "sample.file.key",
                                         inputStream, objectMetadata);
                             PutObjectResult result =      amazonS3Client.putObject(putObjectRequest);
                             LOGGER.info("result - " + result.toString());
                             try {
                                 inputStream.close();
                             } catch (IOException e) {
    
                             }
                         }
                     }
             ).start();
         } catch (AmazonS3Exception e) {
             throw new PersistenceException("could not generate output stream for " + uniqueId, e);
         } catch (IOException e) {
             throw new PersistenceException("could not generate input stream for S3 for " + uniqueId, e);
         }
          try {
             return new GZIPOutputStream(outputStream);
         } catch (IOException e) {
             LOGGER.error(e.getMessage(), e);
             throw new PersistenceException("Failed to get output stream for " + uniqueId + ": " + e.getMessage(), e);
         }
     }
    

当我调试这个时,我看到进程在一段时间后死掉并且没有写入文档,这个实现不正确吗?

4

0 回答 0