英文:
How to integrate a self-test into Spring-Integration?
问题
我想在spring-integration启动后开始一个自测。我的第一个尝试是在集成流程设置之后启动它:
@Configuration
@EnableIntegration
@EnableIntegrationManagement
@IntegrationComponentScan
public class FlowConfig {
...
@PostConstruct
public void startSelfTest() {
SelfTest selfTest = new SelfTest(rezeptConfig, dataSource, archiveClient);
selfTest.run();
}
...
}
这并不起作用,因为在测试开始时,数据库中的表缺失,这是因为尚未启动liquibase。我猜想liquibase脚本会在初始化之后启动。
有任何想法在哪里启动自测吗?
英文:
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?
答案1
得分: 0
只是猜测,关于 onApplicationEvent
事件在 ApplicationListener 中怎么样?当 Spring 初始化并准备就绪时会调用此方法。
例如,查看这个链接:https://stackoverflow.com/questions/8686507/how-to-add-a-hook-to-the-application-context-initialization-event
英文:
just guessing, what about onApplicationEvent
event in ApplicationListener ? This is called when Spring is initialized and ready.
E.g. check this one https://stackoverflow.com/questions/8686507/how-to-add-a-hook-to-the-application-context-initialization-event
答案2
得分: 0
以下是翻译好的内容:
Liquibase bean 负责创建和更新数据库表,在我的 Selftest 之后启动。一个解决方案是在 @Bean 注解中使用 @DependsOn:
@Bean
@DependsOn("liquibase")
public SelfTest startSelfTest() {
SelfTest selfTest = new SelfTest(rezeptConfig, dataSource, archiveClient);
selfTest.run();
return selfTest;
}
现在 Selftest 在 Liquibase 之后启动。
英文:
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.
答案3
得分: 0
好的,最佳做法是在应用程序上下文中的一切都已初始化的情况下进行低级资源交互。这就是在根据其SmartLifecycle
实现启动bean的阶段。
因此,我建议修改您的解决方案,从一些SmartLifecycle.start()
开始执行。
这正是我们在Spring Integration中随处所做的。
(确保我们确切地谈论的是同一个Spring Integration:https://spring.io/projects/spring-integration)
在文档中查看更多信息:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle-processor
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论