这个线程示例有什么问题?

时间:2012-09-06 18:22:57

标签: java amazon-s3

  

可能重复:
  Process dies when trying to put InputStream to 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());
            }
        }
    }

其中DocumentManager写入persistenceService返回的outputStream

  • 此实施适用于FileService
  • 使用S3Service,我们必须将此OutputStream转换为InputStream以符合Amazon S3 APIhere
  • 所以我在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);
         }
     }
    

当我调试这个时,我发现进程在一段时间后就会死掉并且没有写入文档,这个实现是不正确的?

0 个答案:

没有答案