nodece commented on issue #12059:
URL: https://github.com/apache/pulsar/issues/12059#issuecomment-922236224


   @yakir-Yang I tried to run the code provided by you, it works fine.
   
   Set up the environment:
   
   - JAVA: 11
   - Pulsar: 2.8.0
   - Pulsar-client: 2.8.1
   
   ```
   docker run -it \
     -p 6650:6650 \
     -p 8080:8080 --name pulsar-issues-test \
     apachepulsar/pulsar:2.8.0 \
     bin/pulsar standalone
   ```
   
   The code so like:
   
   ```
   import java.nio.charset.StandardCharsets;
   import java.util.Base64;
   import java.util.concurrent.TimeUnit;
   import org.apache.pulsar.client.admin.PulsarAdmin;
   import org.apache.pulsar.client.api.Consumer;
   import org.apache.pulsar.client.api.KeySharedPolicy;
   import org.apache.pulsar.client.api.Message;
   import org.apache.pulsar.client.api.MessageId;
   import org.apache.pulsar.client.api.Producer;
   import org.apache.pulsar.client.api.PulsarClient;
   import org.apache.pulsar.client.api.PulsarClientException;
   import org.apache.pulsar.client.api.SubscriptionMode;
   import org.apache.pulsar.client.api.SubscriptionType;
   
   public class Main {
       public static void main(String[] args) throws Exception {
           String url = "http://localhost:8080";;
           String topic = "persistent://public/default/issues-12059";
           PulsarAdmin admin = 
PulsarAdmin.builder().serviceHttpUrl(url).build();
           PulsarClient client = PulsarClient.builder().serviceUrl(url).build();
           Producer<byte[]> producer = 
client.newProducer().topic(topic).create();
           Thread producerThread = new Thread(() -> {
               while (true) {
                   try {
                       
producer.send("hello-pulsar".getBytes(StandardCharsets.UTF_8));
                       Thread.sleep(5 * 1000);
                   } catch (PulsarClientException | InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           });
   
           producerThread.start();
   
           Consumer<byte[]> consumer = client.newConsumer()
                   .topic(topic)
                   .consumerName("javatest")
                   .subscriptionName("kk-subscription")
                   .ackTimeout(10, TimeUnit.SECONDS)
                   .subscriptionType(SubscriptionType.Key_Shared)
                   .keySharedPolicy(KeySharedPolicy.autoSplitHashRange())
                   .subscriptionMode(SubscriptionMode.NonDurable)
                   .subscribe();
   
           int n = 0;
           while (true) {
               Message<byte[]> message = consumer.receive();
               if (message == null) {
                   continue;
               }
   
               MessageId msgId;
   
               switch (n % 4) {
                   case 0:
                       msgId = message.getMessageId();
                       System.out.println("create MessageId from 
message.getMessageId()");
                       break;
                   case 1:
                       System.out.println(
                               "create MessageId from 
MessageId.fromByteArray(message.getMessageId().toByteArray())");
                       msgId = 
MessageId.fromByteArray(message.getMessageId().toByteArray());
                       break;
                   case 2:
                       System.out.println(
                               "create MessageId from 
MessageId.fromByteArrayWithTopic(message.getMessageId().toByteArray(), topic)");
                       msgId = 
MessageId.fromByteArrayWithTopic(message.getMessageId().toByteArray(), topic);
                       break;
                   case 3:
                       System.out.println(
                               "create MessageId from 
MessageId.fromByteArrayWithTopic(Base64.getDecoder().decode(messageId), 
topic)");
                       String messageId = 
Base64.getEncoder().encodeToString(message.getMessageId().toByteArray());
                       msgId = 
MessageId.fromByteArrayWithTopic(Base64.getDecoder().decode(messageId), topic);
                       break;
                   default:
                       throw new IllegalStateException("Unexpected value: " + 
n);
               }
   
               consumer.acknowledgeAsync(msgId);
               Thread.sleep(2 * 1000);
               admin.topics().getStats(topic).getSubscriptions().forEach((key, 
value) -> {
                   long unackedMessages = value.getUnackedMessages();
                   System.out.println("subscription: " + key + ", 
unackedMessages: " + unackedMessages);
                   if (unackedMessages != 0) {
                       System.out.println("expect value.getUnackedMessages() is 
0, but got " + unackedMessages);
                       System.exit(1);
                   }
               });
               n++;
           }
       }
   }
   ```
   If you use other versions of pulsar, please tell me.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to