1

I have created a small spring batch application which will read data from the file and send an html email . The StudentProcessor creates MImeMessage and ItemWriter sends the email The file contains Student Id and 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;

}

My job.xml is

<?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>

My common content.xml file is:

<?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>

STudent Processor:

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;
  }
}

I put an debugger in MimeMessagePreparator prepare function but the code doesn't reach the debugger. It just skips and goes to line "return msg"; Since the msg doesnt get generated. The ItemWriter fails. I am not sure what is the issue here. Can some one please advise?

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
Nikhil Nadkar
  • 43
  • 2
  • 11
  • On a side note I would recommend using Lombok for your pojos. Saves you generating all the getters and setters. Just import it in your pom.xml, and use the @Data annotation in your pojo – Coder Mar 11 '20 at 18:59

1 Answers1

0

The breakpoint location you put will only be triggered when it actually sends the email in MimeMessageItemWriter which expects to receive the object in the type of MimeMessage from the output of ItemProcessor but now you are returning a MimeMessagePreparator . So it never be executed as it does not receive the right argument type and hence the breakpoint will never be triggered.

So return the MimeMessage from the ItemProcessor please :

@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;
    };
}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172