0

I am using RocketMQ and want to get Message from Queue every new request

@Service
public class GetMessageFromQueue extends BaseObject {
@Resource
private RocketMQTemplate rocketMQTemplate;

@Value("${demo.rocketmq.topic}")
private String springTopic;

private String ms;

public void getMessage(InternalRequest internalRequest) throws MyExeption {
    logger.info("sending message='{}'", inputData.getData());

    CountDownLatch loginLatch = new CountDownLatch (1);
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(inputData.getCorrID());
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    try {
        //get message by TAG to filter only messsage I want
        consumer.subscribe(springTopic, inputData.getCorrID());
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                logger.info("consumeMessage " + msgs.size());
                for (MessageExt messageExt : msgs) {
                    logger.info("consumeMessage " + new String(messageExt.getBody()));
                    ms = new String(messageExt.getBody());
                }
                loginLatch.countDown ();
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
        logger.error("e " + e);
    }

    try {
        loginLatch.await ();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    consumer.shutdown();

    logger.info("ms " + ms);
    if(!inputData.getCorrID().equals(ms)) {
        throw new MyExeption ("500",ms,ms);
    }
    logger.info("get done " + ms);
}
}

This code work fine in normal case. But if I run many concurrent thread, some time the ms varrable don't get exactly the value come from MessageListenerConcurrently callback.

Where I gent wrong? How can I waiting for result come from consumer here?

user3611168
  • 335
  • 1
  • 6
  • 27

1 Answers1

0

The plan is actually very weird as far as I considered. Let me point out some buggy thing so that you can figure out your actual problem.

  1. You have a latch which initial value is one. The consumer is consumed message from a consumer thread pool, and messages could be many, which means the consumer could consume 2 two message in parallel, say msg1 and msg2 , the variable could be assgined values as msg1 or msg2 which is uncertern.

  2. The consumer is created and the shutdown every time your method is called. So the consumer offset may not be updated to the broker in time and your consumer shutdowns, which means next time you start your consumer with the same consumer group, the same message will be consumed again.

  3. The consumer is set to CONSUME_FROM_FIRST_OFFSET, is this realy what you want? which means if there are 10 messages already send to topic, you may always consume the first one when a new consumer(with new consumer group) starts

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149