
Originally Posted by
DHGarrette
We may add support for email sending in the near future.
It's pretty trivial to create a tasklet that is wired up to a spring MailSender. In most cases, the implementation that is more important is WHAT to send, not how to send it. However providing an abstract implemention of an EmailTasklet that reuses the interaction with the MailSender would be more generally useful; a user can then plugin in their specific logic for generating the message content.
Here's what mine looks like. All of the email parameters and MailSender are wired up in the spring bean config as runtime parameters:
Code:
**
* A tasklet that sends an email to indicate that the batch is complete.
*
* @author Charles Hudak
* @since Mar 5, 2009
*
*/
public class SendDeviceBatchCompleteEmailTasklet implements Tasklet, StepExecutionListener
{
private StepExecution stepExecution;
private MailSender mailSender;
private String[] recipients;
private String sender;
private String subject;
private String message;
/**
* Set the mail sender.
*
* @param mailSender the sender
*/
public void setMailSender(MailSender mailSender)
{
this.mailSender = mailSender;
}
/**
* Set the recipients for the message.
*
* @param recipients the recipients
*/
public void setRecipients(String[] recipients)
{
this.recipients = recipients;
}
/**
* Set the sender for the email.
*
* @param sender the sender
*/
public void setSender(String sender)
{
this.sender = sender;
}
/**
* Set the subject to be sent for the email.
*
* @param subject the subject string
*/
public void setSubject(String subject)
{
this.subject = subject;
}
/**
* Set the message format style message to be sent
* in the email body. The batch id and batch filename
* will be used as arguments, in that order.
*
* @param message the message format style string
*/
public void setMessage(String message)
{
this.message = message;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)
*/
@Override
public RepeatStatus execute(StepContribution stepcontribution, ChunkContext chunkcontext) throws Exception
{
Long batchId = this.stepExecution.getJobParameters().getLong(BatchDeviceProcessingConstants.BATCH_UPLOAD_HISTORY_ID);
String batchFile = this.stepExecution.getJobParameters().getString(BatchDeviceProcessingConstants.BATCH_UPLOAD_FILE_NAME);
String formattedMessage = MessageFormat.format(this.message, batchId, batchFile);
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(this.sender);
mailMessage.setTo(this.recipients);
mailMessage.setSubject(this.subject);
mailMessage.setText(formattedMessage);
mailSender.send(mailMessage);
return RepeatStatus.FINISHED;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.StepExecutionListener#afterStep(org.springframework.batch.core.StepExecution)
*/
@Override
public ExitStatus afterStep(StepExecution execution)
{
return null;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.StepExecutionListener#beforeStep(org.springframework.batch.core.StepExecution)
*/
@Override
public void beforeStep(StepExecution execution)
{
this.stepExecution = execution;
}
}