0

I have the following scheduled piece of code in my Spring Boot Application:

@Scheduled(fixedDelay = DELAY_SECONDS)
private void processJobQueue() {

    BlockingQueue<ReportDeliverable> jobQueue = JobQueueFactory.getJobQueueInstance();

    while (!jobQueue.isEmpty()) {

        //do stuff
        if (rCount == 0) {

            status = send(reportDeliverable);

            if (status == TransferStatus.FAILURE) { 
                populateQueue(reportDeliverable);
            }

            if (status == TransferStatus.SUCCESS) { //write the metadata to database

                int i = dbMetadataWriter.writeMetadata(reportDeliverable);

            }
        } else if (rCount == -1) {
            populateQueue(reportDeliverable);
        } else
            logger.info("Record exists in MetaData for {}. Ignoring the File transfer....", reportDeliverable.getFilePath());
    }

}

In my DBMetadataWriter component, the writeMetadataWriter() looks something like this:

@Component
public class DBMetadataWriter {

    public int writeMetadata(final ReportDeliverable reportDeliverable) {

        int nbInserted = 0;

        try {
            nbInserted = jdbcTemplate.update(PORTAL_METADATA_INSERT, insertDataValues);

        } catch (Exception e) {
            logger.error("Could not insert metadata for {}, Exception: {} ", reportDeliverable.toString(), e.getMessage());
        }

        return nbInserted;
    }

In some cases, when writing the insert to the database, I get table space issues with the database at which point I think it would be wise for me to shut down the spring boot application until table space problems are resolved.

What would be the correct way to handle these rare cases? What technique can I use to gracefully shutdown the spring boot application and how can I do it in the above code?

My entry point class where I initially validate all my database connections before processing etc has the following...

@Component
public class RegisterReportSchedules implements ApplicationListener<ContextRefreshedEvent> {

 @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    }

    private void shutdownApplication() {
       int exitCode = SpringApplication.exit(applicationContext, (ExitCodeGenerator) () -> 0);
       System.exit(exitCode);
    }

    }
M06H
  • 1,493
  • 2
  • 25
  • 45
  • check this https://stackoverflow.com/questions/26547532/how-to-shutdown-a-spring-boot-application-in-a-correct-way – pvpkiran Dec 05 '17 at 13:46
  • 1
    Possible duplicate of [How to shutdown a Spring Boot Application in a correct way?](https://stackoverflow.com/questions/26547532/how-to-shutdown-a-spring-boot-application-in-a-correct-way) – pvpkiran Dec 05 '17 at 13:46
  • Catch SQLException and check for database error code like for ORACLE it is ORA-00942 and call shutdownApplication() method – Yogi Dec 07 '17 at 14:13

1 Answers1

0

You have exit() method on SpringApplication class, which can be used for exiting Spring boot application gracefully.

It requires 2 paramerter:

  1. ApplicationContext
  2. ExitCodeGenerator

For further reading:

  1. https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html#exit-org.springframework.context.ApplicationContext-org.springframework.boot.ExitCodeGenerator...-

Code Example:

@Autowired
public void shutDown(ExecutorServiceExitCodeGenerator exitCodeGenerator) {
    SpringApplication.exit(applicationContext, exitCodeGenerator);
}

Call this method when you get exception for No Table space

Yogi
  • 1,536
  • 7
  • 20
  • where should I call this as I already have a `applicationContext` autowired in my entry point code. See update in question – M06H Dec 05 '17 at 17:30