1

我创建了一个小型 spring 批处理应用程序,它将从文件中读取数据并发送 html 电子邮件。StudentProcessor 创建 MImeMessage 和 ItemWriter 发送电子邮件 该文件包含 Student Id 和 Student Name, Home city, Visiting city StudentDetails POJO:

public class StudentDetails {

  public String getStudentId() {
    return studentId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getHomeCity() {
    return homeCity;
  }

  public void setHomeCity(String homeCity) {
    this.homeCity = homeCity;
  }

  public String getVisitingCity() {
    return visitingCity;
  }

  public void setVisitingCity(String visitingCity) {
    this.visitingCity = visitingCity;
  }

  private String studentId;
  private String name;
  private String homeCity;
  private String visitingCity;

}

我的 job.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">

    <bean id="studentDetails" class="com.ubs.classfiercompositeWriter.StudentDetails" scope="prototype" />

    <import resource = "context.xml" />
    <import resource = "database.xml" />
    <job id="classifiercompositeWriter" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1">
            <tasklet>
                <chunk reader="cvsFileItemReader" processor="StudentEmailRowMapper" writer="feedStatusItemWriter"
                       commit-interval="1" >

                </chunk>
            </tasklet>
        </step>
    </job>


    <bean id="cvsFileItemReader"
          class="org.beanio.spring.BeanIOFlatFileItemReader"
          scope="step">
        <property name="streamMapping" value="classpath:/record-mapping.xml"></property>
        <property name="streamName" value="dataFile"></property>
        <property name="resource" value="file:C:/ABC/src/main/resources/data/input.csv"></property>
    </bean>





    <bean id="StudentEmailRowMapper" class="com.ubs.classfiercompositeWriter.StudentProcessor"></bean>


    <bean id="feedStatusItemWriter"
          class="org.springframework.batch.item.mail.javamail.MimeMessageItemWriter">
        <property name="javaMailSender" ref="mailSender" />
    </bean>


</beans>

我常见的 content.xml 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" id="jobRepository">

        <property ref="transactionManager" name="transactionManager"/>

    </bean>

    <bean class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" id="transactionManager"/>
    <bean class="org.springframework.batch.core.launch.support.SimpleJobLauncher" id="jobLauncher">
        <property ref="jobRepository" name="jobRepository"/>

    </bean>


    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="mailhost-arec.ch"/>
        <property name="port" value="25"/>
    </bean>

</beans>

学生处理器:

public class StudentProcessor implements
  ItemProcessor<StudentDetails, MimeMessagePreparator> {
  @Override
  public MimeMessagePreparator process(StudentDetails student)
    throws Exception {
    MimeMessagePreparator msg = new MimeMessagePreparator() {
      @Override
      public void prepare(MimeMessage mimeMessage) throws Exception {
        mimeMessage.setFrom("noreply@abc.com");
        mimeMessage.setRecipients(Message.RecipientType.TO,"nik.kard@gma.com");
        mimeMessage.setSubject("Welcome message !!");
        mimeMessage.setText("Hello " + student.getName());
        System.out.println("Hello mime");

        System.out.println(mimeMessage.toString());

      }
    };

    return msg;
  }
}

我在 MimeMessagePreparator 准备函数中放置了一个调试器,但代码没有到达调试器。它只是跳过并转到“return msg”行;由于没有生成味精。ItemWriter 失败。我不确定这里有什么问题。有人可以建议吗?

4

1 回答 1

0

您放置的断点位置只会在它实际发送电子邮件时触发,该电子邮件 MimeMessageItemWriter期望MimeMessage从输出中接收类型为 的对象,ItemProcessor但现在您返回的是MimeMessagePreparator. 所以它永远不会被执行,因为它没有收到正确的参数类型,因此断点永远不会被触发。

所以MimeMessageItemProcessor请返回:

@Component
public class StudentProcessor implements ItemProcessor<StudentDetails, MimeMessagePreparator> {

  @Autowired
  private JavaMailSender javaMailSender;

  @Override
  public MimeMessagePreparator process(StudentDetails student) throws Exception {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        mimeMessage.setFrom("noreply@abc.com");
        mimeMessage.setRecipients(Message.RecipientType.TO,"nik.kard@gma.com");
        mimeMessage.setSubject("Welcome message !!");
        mimeMessage.setText("Hello " + student.getName());
        System.out.println("Hello mime");
        return mimeMessage;
    };
}
于 2020-03-11T17:48:36.523 回答