1 回答

TA貢獻1842條經驗 獲得超22個贊
一個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());
}
}
}
添加回答
舉報