可能重复:
Process dies when trying to put InputStream to Amazon S3
API
有方法OutputStream getOutputStream(String id)
API
有两个具体实施 - FileService
和S3Service
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 API
(here)所以我在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);
}
}
当我调试这个时,我发现进程在一段时间后就会死掉并且没有写入文档,这个实现是不正确的?