0

I am newbie to Spring Batch and started with a Spring Batch Application using the batch example provided by importing getting started content using Spring Tool Suite. I have used that batch example and modified the classes to trigger my batch. After I had modified the batch, the job doesn't get triggered. I have included java files that I have modified. I am using Oracle Database to connect and using some custom classes. I get no errors in my output using Spring Boot. Why is the importCheckData() or reader not getting triggered? I have listed my classes below:-

Thank you for helping in advance!

CheckPrintApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CheckPrintApplication {
 public static void main(String[] args) throws Exception {
    SpringApplication.run(CheckPrintApplication.class, args);
    }
}

This is my CheckPrintBatchConfiguration.java File

import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import com.abc.canis.checkprint.data.PayslipRecord;
import com.abc.canis.checkprint.listener.JobCompletionNotificationListener;
import com.abc.canis.checkprint.processor.WebServiceItemProcessor;
import com.abc.canis.checkprint.reader.WebServiceItemReader;

@Configuration
@EnableBatchProcessing
public class CheckPrintBatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;

    // tag::readerwriterprocessor[]

    @Bean
    public ItemReader<PayslipRecord> reader() {

        // custom item reader (dummy), using an iterator within an internal
        // list

        System.out.println("Reading the Data");
        WebServiceItemReader reader = new WebServiceItemReader();
        List<PayslipRecord> pojos = new ArrayList<PayslipRecord>();
        pojos.add(new PayslipRecord("1", "desc1", "100"));
        pojos.add(new PayslipRecord("2", "desc2", "200"));
        pojos.add(new PayslipRecord("3", "desc3", "300"));
        reader.setPojos(pojos);
        reader.setIterator(reader.getPayslipRecord().iterator());
        return reader;

    }

    @Bean
    public WebServiceItemProcessor processor() {
        return new WebServiceItemProcessor();
    }

    @Bean
    public JdbcBatchItemWriter<PayslipRecord> writer() {
        JdbcBatchItemWriter<PayslipRecord> writer = new JdbcBatchItemWriter<PayslipRecord>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<PayslipRecord>());
        writer.setSql(
                "INSERT INTO STGADM.STG_CHECKPRINT_PAYOUT_DATA (check_num, reciver,amount) VALUES (:mCheckNumber, :mReceiverName,mCheckAmount)");
        writer.setDataSource(dataSource);
        return writer;
    }
    // end::readerwriterprocessor[]

    // tag::listener[]

    @Bean
    public JobExecutionListener listener() {
        return new JobCompletionNotificationListener(new JdbcTemplate(dataSource));
    }

    // end::listener[]

    // tag::jobstep[]

    @Bean
    public Job importCheckData() {
        return jobBuilderFactory.get("importCheckData").incrementer(new RunIdIncrementer()).listener(listener())
                .flow(step1()).end().build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<PayslipRecord, PayslipRecord> chunk(10).reader(reader())
                .processor(processor()).writer(writer()).build();
    }
    // end::jobstep[]

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}

This is my property file to make a connection to oracle

#Oracle connection
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle12gDialect
spring.datasource.url= jdbc:oracle:thin:@//www.abc.com:1526/XXXXXX
spring.datasource.username=system
spring.datasource.password=user
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

oracle.username=system
oracle.password=user
oracle.url=jdbc:oracle:thin:@//www.abc.com:1526:1526/XXXXXX

This is my simple POJO

package com.abc.canis.checkprint.data;   
public class PayslipRecord {

    private String mCheckNumber;
    private String mReceiverName;
    private String mCheckAmount;


    public PayslipRecord(String mCheckNumber, String mReceiverName, String mCheckAmount) {
        super();
        this.mCheckNumber = mCheckNumber;
        this.mReceiverName = mReceiverName;
        this.mCheckAmount = mCheckAmount;
    }

    public String getmCheckNumber() {
        return mCheckNumber;
    }
    public void setmCheckNumber(String mCheckNumber) {
        this.mCheckNumber = mCheckNumber;
    }
    public String getmReceiverName() {
        return mReceiverName;
    }
    public void setmReceiverName(String mReceiverName) {
        this.mReceiverName = mReceiverName;
    }
    public String getmCheckAmount() {
        return mCheckAmount;
    }
    public void setmCheckAmount(String mCheckAmount) {
        this.mCheckAmount = mCheckAmount;
    }



}

My output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.1.RELEASE)

2016-10-06 19:09:36.740  INFO 11904 --- [           main] c.a.c.c.main.CheckPrintApplication       : Starting CheckPrintApplication on NCALLPBC9BG9 with PID 11904 (C:\Clients\Repository_STS\checkprint-batch-processing\target\classes started by AH54245 in C:\Clients\Repository_STS\checkprint-batch-processing)
2016-10-06 19:09:36.744  INFO 11904 --- [           main] c.a.c.c.main.CheckPrintApplication       : No active profile set, falling back to default profiles: default
2016-10-06 19:09:36.822  INFO 11904 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5d11346a: startup date [Thu Oct 06 19:09:36 EDT 2016]; root of context hierarchy
2016-10-06 19:09:38.314  INFO 11904 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-10-06 19:09:38.332  INFO 11904 --- [           main] c.a.c.c.main.CheckPrintApplication       : Started CheckPrintApplication in 1.99 seconds (JVM running for 2.585)
2016-10-06 19:09:38.334  INFO 11904 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5d11346a: startup date [Thu Oct 06 19:09:36 EDT 2016]; root of context hierarchy
2016-10-06 19:09:38.336  INFO 11904 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
Kapi
  • 1
  • 1
  • Possible duplicate of [How Spring Boot run batch jobs](http://stackoverflow.com/questions/23447948/how-spring-boot-run-batch-jobs) – Luca Basso Ricci Oct 07 '16 at 05:01
  • To me it seems like the `CheckPrintBatchConfiguration.java` is simply not being picked up and thus not loaded in the applicationcontext. Are all classes in the same package? The recommendation of spring is that you locate your main application class in a root package above other classes. In that case you don't have to import any classes on the Application.java. If you follow a different hierarchy the `@Component` and/or `@Configuration` annotations will not be picked up. – Sander_M Oct 07 '16 at 14:28

1 Answers1

0

Your connection property file contains duplicate configuration. The following configuration should be enough to properly load the datasource.

spring.datasource.url=jdbc:oracle:thin:@//www.abc.com:1526/XXXXXX
spring.datasource.username=system
spring.datasource.password=user
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

Furthermore, you use no @Entity or any other CRUD operation in your code, so for this example you dont need JPA configuration.

However, my best guess is that your CheckPrintBatchConfiguration.java is not being loaded into the applicationcontext.

Either use @Import(CheckPrintBatchConfiguration.class) above your CheckPrintApplication.java or use the recommended hierarchy.

Sander_M
  • 1,109
  • 2
  • 18
  • 36