使用Spring Boot的Spring Batch不运行批处理。你能帮我解决一下我的配置问题吗?

时间:2016-10-06 23:16:32

标签: spring-boot spring-batch batch-processing

我是Spring Batch的新手,并使用通过使用Spring Tool Suite导入入门内容提供的批处理示例开始使用Spring Batch Application。我已经使用了该批处理示例并修改了类以触发我的批处理。修改批处理后,作业不会被触发。我已经包含了我修改过的java文件。我正在使用Oracle数据库连接和使用一些自定义类。使用Spring Boot我的输出中没有错误。为什么importCheckData()或reader没有被触发? 我在下面列出了我的课程: -

感谢您提前帮助!

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

这是我的 CheckPrintBatchConfiguration.java 文件

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

}

这是我的属性文件,用于连接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

这是我的简单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;
    }



}

我的输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

1 个答案:

答案 0 :(得分:0)

您的连接属性文件包含重复配置。以下配置应足以正确加载数据源。

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

此外,您在代码中不使用@Entity或任何其他CRUD操作,因此对于此示例,您不需要JPA配置。

但是,我最好的猜测是你的CheckPrintBatchConfiguration.java没有被加载到applicationcontext中。

使用@Import(CheckPrintBatchConfiguration.class)use the recommended hierarchy上方的CheckPrintApplication.java