Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] when define Consumer in class field, and restart consumer, message received More than once #22759

Closed
2 of 3 tasks
sdvdxl opened this issue May 22, 2024 · 1 comment
Closed
2 of 3 tasks
Labels
type/bug The PR fixed a bug or issue reported a bug

Comments

@sdvdxl
Copy link

sdvdxl commented May 22, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Read release policy

  • I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.

Version

server: pulsar-3.2.2 standalone

client: java-client 3.2.3

Minimal reproduce step

java code:

  1. enable testProducer(); disable: testConsumer(); and start
  2. disable testProducer(); enable: testConsumer(); and restart
  3. disable testProducer(); enable: testConsumer(); and restart
  4. delete static Consumer<byte[]> consumer; and restart
  5. delete static Consumer<byte[]> consumer; and restart
package top.todu.leaning.pulsar.sample;

import cn.hutool.core.date.DateTime;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.client.api.*;

/**
 * @author du 2024/5/20 14:48
 */
public class PulsarSample {
  private static PulsarClient pulsarClient;
  private static String host = "pulsar://10.10.81.28:6650";
  private static String topic = "data_sync_queue_test";
  static Consumer<byte[]> consumer;

  public static void main(String[] args) throws Exception {
    pulsarClient = PulsarClient.builder().serviceUrl(host).build();

    testConsumer();
    testProducer();
    System.out.println("-=-------");
    TimeUnit.SECONDS.sleep(10);
    consumer.close();
    pulsarClient.close();
  }

  private static void testProducer() throws PulsarClientException {
    Producer<byte[]> producer =
        pulsarClient
            .newProducer()
            .topic(topic)
            .messageRoutingMode(MessageRoutingMode.SinglePartition)
            .create();
    for (int i = 0; i < 5; i++) {
      String msg = i + " Hello, Pulsar! " + DateTime.now();
      System.out.println("send msg: " + msg);
      producer.newMessage().value(msg.getBytes()).key("const").send();
    }
    producer.close();
  }

  private static void testConsumer() throws PulsarClientException {
    consumer =
        pulsarClient
            .newConsumer()
            .subscriptionName("data_sync_queue_test")
            .topic(topic)
            .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
            .subscriptionType(SubscriptionType.Key_Shared)
            .messageListener(
                new MessageListener<byte[]>() {
                  @Override
                  public void received(Consumer<byte[]> consumer, Message<byte[]> msg) {
                    System.out.println("Received message: " + new String(msg.getData()));
                    try {
                      consumer.acknowledge(msg);
                    } catch (PulsarClientException e) {
                      throw new RuntimeException(e);
                    }
                  }
                })
            .subscribe();
  }

  

}

What did you expect to see?

message should receive only once

What did you see instead?

if defined static Consumer<byte[]> consumer;, restart application, message will receive more than once.

if delete the field, it is good.

Anything else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!
@sdvdxl sdvdxl added the type/bug The PR fixed a bug or issue reported a bug label May 22, 2024
@sdvdxl
Copy link
Author

sdvdxl commented May 22, 2024

this may be a mistake. other place used consumer.unsubscribe()

@sdvdxl sdvdxl closed this as completed May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

No branches or pull requests

1 participant