Aim - Decrypt a .pgp encrypted file , read the data as a stream , perform Transformation to vendor requirement, encrypt as stream and write to a File.
Logic - Custom Reader, Writer and Tasklet , Stored the decrypted/encrypted data onto ExecutionContext and pass to different steps.
Works for - Small file (~1MB)
Issue faced - Tried with a (~10MB - 10K records) - Reading step was successful, but when begin writing data as an Encrypted File - Memory issue - java.lang.OutOfMemoryError: Java heap space
Code snippet -
<job id="testJob" xmlns="http://www.springframework.org/schema/batch">
<!-- Read Encrypted file and Decrypt -->
<batch:step id="decryptFile" next="readAndWriteData">
<batch:tasklet ref="fileDecryptionTasklet">
<batch:listeners>
<batch:listener ref="decryptFileListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
<!-- Read data from decryption step and write to Stream -->
<batch:step id="readAndWriteData" next="encryptFile">
<batch:tasklet>
<batch:chunk reader="hrdsCustomReader" processor="Processor"
writer="CustomWriter" commit-interval="${.ftp.comit.interval}" />
<batch:listeners>
<batch:listener ref="encryptFileListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
<!-- Write to vendor specific file -->
<batch:step id="encryptFile">
<batch:tasklet ref="fileEncryptionTasklet" />
</batch:step>
</job>
Tasklet and custom writers code snippets -
@Override
public String read() throws Exception, UnexpectedInputException,
ParseException {
decryptedData = (String) stepExecution.getJobExecution()
.getExecutionContext().get("DecryptedData");
if (decryptedData != null)
//logger.info("decryptedData in Custom Reader - \n" + decryptedData);
stepExecution.getJobExecution().getExecutionContext()
.put("DecryptedData", null);
return decryptedData;
}
public void write(List items) throws Exception {
logger.info("Begin writing data as an Encrypted File");
Iterator itr = items.iterator();
while(itr.hasNext()) {
String element = itr.next();
lineBuffer.append(element+LINE_SEPARATOR);
}
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("EncryptedData", lineBuffer);
}
public RepeatStatus execute(StepContribution step, ChunkContext chunk)
throws Exception {
InputStream inputstream = new FileInputStream(inputdirectory);
Message encryptMessage = MessageBuilder
.withPayload(inputstream)
.setHeader(
FileEncryptionTransformer.ENCRYPTION_OPERATION_HEADER,
"decryptAndVerify")
.setHeader(
FileEncryptionTransformer.ENCRYPTION_OPERATION_HEADER,
EncryptionUtil.DECRYPT_STREAM_OPERATION)
.setHeader(FileEncryptionTransformer.SOURCE_FILE_NAME_HEADER,
filename).build();
InputStream inputStream = pgptransformer
.doTransformStream(encryptMessage);
String strData = IOUtils.toString(inputStream, "UTF-8");
inputstream.close();
chunk.getStepContext().getStepExecution().getExecutionContext().put("DecryptedData", strData);
return null;
}
public RepeatStatus execute(StepContribution step, ChunkContext chunk) throws Exception { lineBuffer = (StringBuffer) chunk.getStepContext() .getJobExecutionContext().get("EncryptedData"); byte[] bytes = lineBuffer.toString().getBytes(); InputStream inputStream = new ByteArrayInputStream(bytes); Message encryptMessage = MessageBuilder .withPayload(inputStream) .setHeader(PGPFileTransformer.OUTPUT_FILE_FOLDER, outputdirectory) .setHeader( FileEncryptionTransformer.ENCRYPTION_OPERATION_HEADER, "signAndEncrypt") .setHeader( FileEncryptionTransformer.ENCRYPTION_OPERATION_HEADER, EncryptionUtil.ENCRYPT_STREAM_OPERATION) .setHeader(FileEncryptionTransformer.SOURCE_FILE_NAME_HEADER, filename).build(); pgptransformer.doTransform(encryptMessage); inputStream.close(); chunk.getStepContext().getStepExecution().getExecutionContext().put("EncryptedData", null); return null; }
Appreciate if somebody can help resolve the issue.