2 回答
TA贡献1860条经验 获得超9个赞
您需要定义一个类型的 bean JavaMailSender(例如在您共享的链接中),例如:
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
// set properties on javaMailSender (user, password, etc)
return javaMailSender;
}
然后,您可以SendMailListener像使用作业资源管理器一样将其注入您的系统中。注入后,可以使用邮件发件人发送电子邮件,这是一个示例:
public class SendMailListener implements JobExecutionListener {
final static Logger LOGGER = LoggerFactory.getLogger(SendMailListener.class);
@Autowired // field injection is not recommended, use constructor injection instead
private JobExplorer explorer;
@Autowired // field injection is not recommended, use constructor injection instead
private JavaMailSender javaMailSender;
@Autowired
private StepExecution stepExecution;
@Override
public void afterJob(JobExecution jobExecution) {
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
LOGGER.info("The job has been completed with the following parameters: " + jobExecution.getJobParameters());
}
//send email here
MimeMessage message = javaMailSender.createMimeMessage();
// set message properties (sender, receiver, content)
javaMailSender.send(message);
}
@Override
public void beforeJob(JobExecution jobExecution) {
// TODO Auto-generated method stub
//send email here
MimeMessage message = javaMailSender.createMimeMessage();
// set message properties (sender, receiver, content)
javaMailSender.send(message);
}
}
TA贡献1712条经验 获得超3个赞
因此,对于 XML 表示法的解决方法,您可以使用类似的作业配置。
@Bean
public Job demoJob(){
return jobs.get("demoJob")
.incrementer(new RunIdIncrementer())
.listener(new JobResultListener())
.start(demostep())
.build();
}
你可以在这里找到合适的文章:https ://howtodoinjava.com/spring-batch/spring-batch-event-listeners/
添加回答
举报