springboot 2.x 如何设置应用启动完成监听器?

我想在 springboot 2.x 应用启动完成的监听器,在监听器中,来订阅 rocket mq 的主题(topic), 以便消费消息,要如何做?

1 个解决方案

AllenJiang
中间件研发,关注微信公众号 : 小哈学Java, 回复"666", 即可免费领取10G学习&面试资料

只需要定义一个 listener, 让它来实现 ApplicationListener<ApplicationStartedEvent> 接口,在 onApplicationEvent() 方法内,写上的订阅逻辑即可,代码如下:

@Component
@Slf4j
public class ApplicationStartupListener implements ApplicationListener<ApplicationStartedEvent> {

	@Override
	public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
		log.info("the application already startup, ready to register the consumer ...");
		// todo 订阅逻辑
		log.info("the consumer register success !!!");
	}
}