I have similar code like this answer. But now the question is when to start the listener programmatically, once the spring app starts. Should I write an event listener for an application event or are there more preferable ways?
Asked
Active
Viewed 32 times
1 Answers
0
You can either use an ApplicationListener
or implement SmartLifecycle
and start the containers in the start()
method.

Gary Russell
- 166,535
- 14
- 146
- 179
-
what about @PostConstruct in a Listener (implements MessageListener) bean which does factory.createContainer(topic).setupMessageListener(this) ? – Valentin Bossi Jul 04 '23 at 09:35
-
1It is not good practice to start activity in a `@PostConstruct` method because it might be that not all beans have been constructed; this could cause problems, especially if there is some bean dependence that Spring cannot detect. Using `SmartLifecycle` ensures that all beans will have been initialized. Furthermore, manually creating a container like that means you are responsible for its lifecycle (including stopping it when the context is closed); you can do that in the `stop()` method. – Gary Russell Jul 04 '23 at 13:09