svobol13
svobol13

Reputation: 1950

Spring boot Start webapplication with delay programmatically

I like spring boot but I can't figure/find out how can I achieve this: I have executable spring application. I need applicationContext, do some stuff and then start "webPart" (REST api). Is it possible to tell spring "don't start jetty automatically, I'll start it myself", or I need to compose application myself?

I would like to do something like that. Do anyone have any idea?

@SpringApplication
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(App.class, args);
        try {
            doSth(ctx);
            startWeb();
        } catch(Exception e) {
            clean();
        }
    }
}

EDIT: I want to use "web-part" but later and when I decided to (f.e. Exception is not thrown...). I don't want to prevent web-context to be used at all.

Upvotes: 1

Views: 2055

Answers (2)

Mohit
Mohit

Reputation: 1755

One way is to destroy the first context (which has web disabled) and then start a new context with web enabled.

ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run(args);

try {
    doSomething(ctx);
} catch (Exception e){
    //abort
} finally {
    ctx.close();
}

//New context has web enabled.
ctx = new SpringApplicationBuilder(Application.class).run(args);
doSomething(ctx);

Upvotes: 2

codeaholicguy
codeaholicguy

Reputation: 1691

You need to prevent jetty/tomcat start automatically like they did here How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate

Upvotes: 0

Related Questions