1 回答
TA贡献1842条经验 获得超21个赞
一个ApplicationListener会满足你的需要。它会收到有关注册事件的通知,例如当 ApplicationContext 准备就绪时。您将可以完全访问所有 Bean 和注入。
@Component
public class StartupApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
@Inject
private ConsumerRegistry registry;
@Inject
@Value("${consumerCount}")
private int consumerCount;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
//do your logic
for (int i = 0; i < consumerCount; i++) {
// Each registered consumer results in a thread that consumes messages.
// Incoming messages will be delivered to any consumer thread that's not busy.
registry.registerConsumer(new Consumer());
}
}
}
添加回答
举报