1

My Spring Boot Service will do a job and exit after success with 0 (there is no restcontroller), but i want it aslo to exit on every exception so i added @ControllerAdvice on a class and put this method:

@ControllerAdvice
@RequiredArgsConstructor
@Slf4j
public class ImportInekData {

final InekService inekService;

final ImportDataService dataService;

public void doTheJob(){
    log.info("Fetching new list from Inek.");
    UpdatedList updatedList = inekService.getUpdatedList();
    List<Standort> toBeUpdated = updatedList.getToBeUpdated();
    List<String> toBeDeleted = updatedList.getToBeDeleted();
    log.info("List fetched with " + toBeUpdated.size() + " valid entries to be updated and " + toBeDeleted.size() + " entries marked for deletion. ");
    log.info("Pushing to DB...");
    dataService.importAll(toBeUpdated);
}

@EventListener
public void onStart(ContextStartedEvent start){
    log.info("Application started.");
    doTheJob();
    log.info("Import finished.");
    SpringApplication.exit(start.getApplicationContext(), () -> 0);
}

@ExceptionHandler(value = Exception.class)
public String outOnException(Exception e){
    log.error("Exception occurred see logs. Stopping..");
    SpringApplication.exit(context, () -> -1);
    return "dying";
}

}

All is working fine but when i throw an IllegalArgumentException the @ExceptionHandler method is not called. First i had a void method with no parameter and then i began trying with String return and at least one parameter - that is not needed.

How get this working? Is there a better way for my case to react on every exception?

dermoritz
  • 12,519
  • 25
  • 97
  • 185

1 Answers1

0

Controller Advices in spring is a mechanism intended to properly handle the Exceptions at the level of spring MVC.

Spring MVC in a nutshell is a web framework, and as such, it assumes that you have some kind of web endpoint that is called by the end user or maybe frontend. This endpoint is an "entry-point" to your backend code that can have services, query the database, and so forth. If during this backend flow the exception is thrown in general you don't want that the web endpoint will return 500 internal server error, so spring provides tooling for convenient mapping of these exceptions: translating them to json with a "good-looking" message, with correct HTTP code, and so forth.

If you don't have any controllers, then the whole concept of controller advices is not applicable in your flow, so there is no point in using it...

Now the real question is what exactly do you want to achieve with this exception handling? If Application context cannot start usually spring boot application will be closed gracefully...

If you want to close the application programmatically, make sure you've read this thread

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • i read this thread - i want to cloase application programmatically on every exception - "on every exception" seems to be the hard part? (i am already ending the application if job was done successfully) - - this i wrote in first sentence of my question?! – dermoritz Feb 22 '21 at 07:24
  • 1
    Look, controller advices mean spring MVC MVC means a thread pool that always gets connections and serves requests. But you don't need this kind of behavior, you also don't need a web server (tomcat/jetty/whatever) at all. so this is a kind of "one-time-execution" code. In this case Read about command-line-runner: https://stackoverflow.com/questions/58166325/run-spring-boot-application-once-and-exit If some part of the flow throws an exception - it will propagate to the entry point (there should be exactly one entry point) and it all boils down to the simple try catch. – Mark Bramnik Feb 22 '21 at 08:49