springspring-data-jpaspring-cloudspring-cloud-task

Spring boot cloud task without springs task tables


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.


Solution

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