0

I've an application that run a task that parses a file and persist the data in the database. After that it shutsdown gracefully.

I can see that spring automatically creates 4 tables and populates them with data.

Tables that spring creates:

task_execution
task_execution_params
task_lock
task_task_batch

Im not really interested in these tables and would like spring to stop create them. Is this possible? or am I using spring task wrong..

I've already tried:

spring.cloud.task.initialize-enabled
spring.cloud.task.autoconfiguration.enabled

and they didn't really do what I want.

Thank you.

user2354898
  • 253
  • 2
  • 15
  • Please clarify what it is that you want Spring Cloud Task to do. Based on your current question, I'd recommend to just not use Spring Cloud Task. – Henning Mar 10 '23 at 18:17
  • Application will run when Kubernetes cron job is triggered. And when it’s done I want it to gracefully shutdown. Should I just do System.exit perhaps? – user2354898 Mar 12 '23 at 11:08
  • What do you mean with a graceful shutdown? Is the application also running a webserver that should not interrupt in-flight requests? How is the task started? If you start it from an `ApplicationRunner`, then the JVM will exit gracefully on its own unless you have started additional threads (e.g. in a web-server). – Henning Mar 12 '23 at 11:41
  • When started it will parse a file and persist data in database and then its done and should shutdown. Yes I’ve a web server for spring boot actuator. I need the healthchecks for verifying that it starts successfully. So I guess running it as ApplicationRunner wont exit it? – user2354898 Mar 12 '23 at 12:39

1 Answers1

1

Persisting the task status in the database is one of the core features of Spring Cloud Task. If you don't need that, you most likely don't need Spring Cloud Task.

If you have the web dependency on the classpath (e.g. for the actuators), the application will only stop when the embedded web server is stopped. If the web server has been started by Spring, a good way to do this here is to close the application context, which takes care of the rest.

If your task is executed from an ApplicationRunner, you can listen for the ApplicationReadyEvent that is only sent after all runners have finished. In the listener for this event you can then close the application context.

This could roughly look like this:

@SpringBootApplication
public class DemoApplication {

  private static final Logger LOG = LoggerFactory.getLogger(DemoApplication.class);

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @Bean
  ApplicationRunner applicationRunner() {
    return args -> LOG.info("Hello Task");
  }

  @Component
  static class ShutdownWhenReadyListener implements ApplicationContextAware, ApplicationListener<ApplicationReadyEvent> {

    private ConfigurableApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
      context = (ConfigurableApplicationContext) applicationContext;
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
      context.close();
    }

  }

}

For very simple apps, you can also close the application context directly from main as the method run will only return after the context is fully booted which includes all ApplicationRunners to have run:

@SpringBootApplication
public class DemoApplication {

  private static final Logger LOG = LoggerFactory.getLogger(DemoApplication.class);

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args).close();
  }

  @Bean
  ApplicationRunner applicationRunner() {
    return args -> LOG.info("Hello Task");
  }

}
Henning
  • 3,055
  • 6
  • 30