Spring Batch 电子邮件监听器

我想将 Spring Batch 配置为在作业开始时发送电子邮件,并在作业完成或失败时发送电子邮件。我通过搜索看到了一些示例,例如codecentric 上的链接,但我没有在我的项目中使用 XML 注释。


我有一个看起来像这样的听众,但不确定我可能需要什么其他作品。任何输入将不胜感激


public class SendMailListener implements JobExecutionListener {


    final static Logger LOGGER = LoggerFactory.getLogger(SendMailListener.class);


    @Autowired

    private JobExplorer explorer;


    @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


    }


    @Override

    public void beforeJob(JobExecution jobExecution) {

        // TODO Auto-generated method stub


    }

    //send email here

}

编辑:


我有与听众一起定义的工作:


@Bean(name = "mccINSTANCETESTjob")

    public Job mccOrz004job(JobBuilderFactory jobFactory, //

            @Qualifier("instanceTestSetupStep") Step orz004SetupStep, //

            @Qualifier("callM204Step") Step callM204Step, //

            @Autowired SingleInstanceListener listener, @Autowired SendMailListener mailListener) { //

        return jobFactory.get("mccINSTANCETESTjob") //

                .incrementer(new RunIdIncrementer()) //

                .listener(mailListener) //

                .start(orz004SetupStep) //

                .next(callM204Step) //

                .build();

    }


波斯汪
浏览 128回答 2
2回答

慕码人2483693

您需要定义一个类型的 bean JavaMailSender(例如在您共享的链接中),例如:@Beanpublic 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);    }}

交互式爱情

因此,对于 XML 表示法的解决方法,您可以使用类似的作业配置。@Beanpublic Job demoJob(){return jobs.get("demoJob")        .incrementer(new RunIdIncrementer())        .listener(new JobResultListener())        .start(demostep())        .build();}你可以在这里找到合适的文章:https ://howtodoinjava.com/spring-batch/spring-batch-event-listeners/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java