0

I have gone through few posts related to the similar issue, i am calling spring batch application through shell script and getting the exit status.Everything works fine on successful execution. ExitStatus gets populated as 0. However if there is any database error(to create database error i gave the wrong port of database) then ExitStatus is being returned as empty. Code is below

I have referred below posts and implemented similarly

Make a spring-batch job exit with non-zero code if an exception is thrown

Spring batch return custom process exit code

Shell Script: java -jar $JOBDIR/lib/feed*.jar

result=$?
    echo $result            

Java:

 public static void main(String[] args) {
       ConfigurableApplicationContext context 
                  =SpringApplication.run(App.class, args);

    int exitCode = SpringApplication.exit(context);
    System.out.print("Exit code is" + exitCode);
    System.exit(exitCode);
  }

    @Primary
@Bean(destroyMethod = "")
public DataSource dataSource() throws Exception {
      return BatchDataSource.create(url, user, password);
    }

in case of database error it is not even reaching end of the main method System.exit(exitCode); Can any one guide me what is wrong??

reddynr
  • 149
  • 4
  • 10
  • This is not related to Spring Batch per se. If I understand correctly, your Spring application context fails to start due to an error in a bean creation and you want to capture that in a non zero exit code. Have you tried to wrap the spring boot app start with `System.exit` ? See example here: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-batch/src/main/java/smoketest/batch/SampleBatchApplication.java#L66-L68. – Mahmoud Ben Hassine Sep 18 '19 at 08:02
  • I am exactly doing the same(I have posted the code above). since Datasource is build before spring context it is not even reaching the point of setting the exit code. – reddynr Sep 19 '19 at 01:53

1 Answers1

0

if there is any database error(to create database error i gave the wrong port of database) then ExitStatus is being returned as empty.

That's because in that case, your job is not executed at all. According to your configuration, the dataSource bean creation error prevent the Spring application context from being correctly started and your job is not even executed.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50