apache / rocketmq-spring

Apache RocketMQ Spring Integration
https://rocketmq.apache.org/
Apache License 2.0
2.11k stars 898 forks source link

Is it possible to create consumer that subscribe to certain topic on the fly? #224

Closed chwii closed 4 years ago

chwii commented 4 years ago

I was able to do so with rocketmq-client like below

public DefaultMQPushConsumer create(Object...args){
// basic setup omitted
    defaultMQPushConsumer.subscribe(certainTopic, "*");
    defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently(){
        //custom consumeMessage() logic
    });
}

Is this possible with rocketmq-spring-boot-starter? Or is it ok if I stick to rocketmq-client in my Spring Boot project

xiangwangcheng commented 4 years ago

I'm not sure what do you mean of 'on the fly', but you can create a consumer subscribing to a certain topic like below. image

chwii commented 4 years ago

@xiangwangcheng Of course I read the consumer demo before I open this issue, unfortunately they didn't meet my needs because I have no idea what topic I should subscribe to in coding phase(actually I do but topics can be added dynamically). What I want to implement here is to create consumer and register listener(different onMessage logic maybe) on method invocation instead of hard-coded classes like the demo. With rocketmq-client I can just wrap the creation logic like my snippet and can call create(args) in case of topic addition. Dose this make sense to you?

RongtongJin commented 4 years ago

@chwii It‘s a little complicated if you need to create a consumer at runtime using rocketmq-spring. It is recommended that you still use rocketmq-client in your spring boot project.