1 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊
一個(gè)ApplicationListener會(huì)滿足你的需要。它會(huì)收到有關(guān)注冊事件的通知,例如當(dāng) ApplicationContext 準(zhǔn)備就緒時(shí)。您將可以完全訪問所有 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());
}
}
}
添加回答
舉報(bào)