GitHub user ragaur-tibco edited a comment on the discussion: intermittent 
Messages loss while consuming the messages from consumer

Hi @lhotari 

we have two class one is producer.java and another consumer.java

PulsarProducerExample .java 
=============================================================


```
`import org.apache.pulsar.client.api.*;

public class PulsarProducerExample {
    public static void main(String[] args) throws PulsarClientException {
        String serviceUrl = "pulsar://localhost:6650";
        String topicName = "topic-Name"";

        // Create a Pulsar client instance
        PulsarClient client = PulsarClient.builder()
                .serviceUrl(serviceUrl)
                .build();

        // Create a producer
        Producer<byte[]> producer = client.newProducer()
                .topic(topicName)
                .create();

        // Send messages
        for (int i = 0; i < 100; i++) {
            String message = "Message " + i;
            producer.send(message.getBytes());
            System.out.println("Sent message: " + message);
        }

        // Close the producer and Pulsar client
        producer.close();
        client.close();
    }
}
`
```

Consumer.java
=============================================================

```
`import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;

public class Consumer{
    public static void main(String[] args) throws Exception {
        String serviceUrl = "pulsar://localhost:6650";
        String topicName = "topic-Name";
        String subscriptionName = "subscriptionName";

        PulsarClient client = PulsarClient.builder()
                .serviceUrl(serviceUrl)
                .build();

        Consumer<byte[]> consumer1 = client.newConsumer()
                .topic(topicName)
                .subscriptionName(subscriptionName)
                .subscriptionType(SubscriptionType.Shared) // Use Shared 
subscription type
                .subscribe();
        while (true) {
           Message msg = consumer1.receive();
            System.out.println("from consumer 
1=============================="+consumer1.receive());
            consumer1.acknowledgeAsync(msg);
        }
    }
}



`
```

Steps to reproduce:

1. Run consumer and topic will be created by itself
2. check the topic stats one consumer created in subscription name
3. stop the consumer and then run the producer and send 100 messages 
4. Run consumer and check the topic stats now you will be able to see that a 
different consumer with different name and some time with same name created (if 
in consumerName parameter provided with the same consumer name which is already 
present) and 100 messages available in previous consumer but not in new 
consumer. Due to which not able to receive messages

Queries:
1. Is there any api from which we can receive the messages from the specific 
consumer name
2. Is there any way to fetch the all the messages with is available in 
subscription from all the consumers 

GitHub link: 
https://github.com/apache/pulsar/discussions/22681#discussioncomment-9415732

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscr...@pulsar.apache.org

Reply via email to