- 我有
API
一个方法OutputStream getOutputStream(String id)
- 这
API
有两个具体的实现 -FileService
和S3Service
- 我有一个执行以下操作的 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
,我们必须将其转换OutputStream
为InputStream
符合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); } }
当我调试这个时,我看到进程在一段时间后死掉并且没有写入文档,这个实现不正确吗?