Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a longrunning batch job on startup of tomcat server?

I'm launching a spring-batch job on application start using

spring.batch.job.names=MyJob

@Configuration
public class MyJob {
    @Bean
    public Job testJob() throws IOException {
        return jobBuilderFactory.get(MyJob.class.getSimpleName())
                .start(import())
                .build();
    }
}

Unfortunately this somehow delays the tomcat server startup. The job has a runtime of several minutes, thus I'm getting the following error:

Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.

Question: how can I run this job without preventing tomcat to start up? Eg running the job async?

like image 442
membersound Avatar asked Dec 31 '25 02:12

membersound


1 Answers

You can include a ServletContextListener.

Put your code in the contextInitialized method.

like image 119
Andres Avatar answered Jan 02 '26 16:01

Andres