Spring cloud stream - send message after application initalization

主宰稳场 提交于 2019-12-01 21:04:16

@PostConstruct is triggered too early (when the configuration bean is created, but before the context is started and the binding takes place). What you want is to trigger the sending of the message once the context is completely initialized, or at least after the output channels are bound.

You have a few options, all relying on the creation of an additional bean:

  1. To use the SmartLifecycle support from Spring (make sure that isAutoStartup returns true by default and the phase is zero - the default - so that the bean is started after outputs are bound).

  2. Use an ApplicationListener for ContextRefreshedEvent.

  3. Since this is a Spring Boot application you can use an ApplicationRunner bean (which gets invoked after the context has been created).

kevin628

You might look into Spring's Task Execution and Scheduling features.

In particular, it sounds like you want something like what section 34.4 covers.

Also, I spotted this answer to a similar question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!