0

I want to start a selftest after spring-integration is started. My first approach was to start it after the setup of the integration flow:

@Configuration
@EnableIntegration
@EnableIntegrationManagement
@IntegrationComponentScan
public class FlowConfig {
...
    @PostConstruct
    public void startSelfTest() {
        SelfTest selfTest = new SelfTest(rezeptConfig, dataSource, archiveClient);
        selfTest.run();
    }
...
}

This does not work because when the test was started the tables in the database were missing because liquibase was not yet started. I guess the liquibase scripts would be started after initialization.

Any ideas what is the best place to start a selftest?

3 Answers3

0

just guessing, what about onApplicationEvent event in ApplicationListener ? This is called when Spring is initialized and ready.

E.g. check this one How to add a hook to the application context initialization event?

Michal Drozd
  • 1,311
  • 5
  • 13
  • 26
  • I'm not sure if the liquibase bean is started after the context is initialized but you're right, this would be the correct place to start the Selftest. – Oliver Boehm Sep 30 '20 at 13:10
0

The Liquibase bean which is responsible to create and update the DB tables are started after my Selftest. One solution is to use @DependsOn togehther with the @Bean annotation:

    @Bean
    @DependsOn("liquibase")
    public SelfTest startSelfTest() {
        SelfTest selfTest = new SelfTest(rezeptConfig, dataSource, archiveClient);
        selfTest.run();
        return selfTest;
    }

Now the Selftest is started after Liquibase.

0

Well, the best practice to do low-level resources interaction is when everything is initialized in the application context already. And that is the phase when beans are started according their SmartLifecycle implementation.

So, what I suggest to revise your solution to be done from some SmartLifecycle.start().

That's exactly what we do everywhere around in Spring Integration. (Be sure that we talk exactly about the same Spring Integration: https://spring.io/projects/spring-integration)

See more info in docs: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle-processor

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118