apache / rocketmq

Apache RocketMQ is a cloud native messaging and streaming platform, making it simple to build event-driven applications.
https://rocketmq.apache.org/
Apache License 2.0
21.3k stars 11.71k forks source link

[DefaultLitePullConsumerImpl] There is no way to know whether the consumer has closed. #2152

Closed JunLu1003 closed 4 years ago

JunLu1003 commented 4 years ago

BUG REPORT

  1. Please describe the issue you observed:

When the project starts, I use DefaultLitePullConsumerImpl as the consumer, and call the consumer's poll(long timeout) method to do some logic.

public class BizConsumer implements CommandLineRunner {

     private DefaultLitePullConsumer consumer;

     @Override
      public void run(String... args) throws Exception {
          consumer = new DefaultLitePullConsumer("group-name");
          consumer.setNamesrvAddr("server address");
          consumer.subscribe(...);
          consumer.start();
          SomeClass.executeAsync(consumer);
      }

      @PreDestroy
      public void destroy {
          if (consumer != null) {
              consumer.shutdown();
          }
      }
}
public class SomeClass {
    @Async
    public void executeAsync(DefaultLitePullConsumer consumer) {
        while (true) {
            /**
             * When Server shutdown, consumer.poll() will throw a Exception
             */
            List<MessageExt> messages = consumer.poll(5 * 1000);
            ....
        }
   }
}

when the project is closed, I call the consumer's shutdown method, but the consumer's poll(long timeout ) is still running, then I got a Exception: java.lang.IllegalStateException: The consumer not running, please start it first.

I try to find a isClosed() or isRunning() method in DefaultLitePullConsumerImpl class, but I cannot find them.

Is there a way to know that the consumer client has been closed ?

java.lang.IllegalStateException: The consumer not running, please start it first.

  1. Please tell us about your environment:

jdk 8 Springboot 2.2.8 rocketmq-spring-boot-starter 2.1.0 CentOS 7

RongtongJin commented 4 years ago

IMO, it is necessary to add such a method. Could you submit a PR to fix it?

zongtanghu commented 4 years ago

Can you submit a PR to fix this issue? @zhangjidi2016