Skip to content

consumers - metadata comparison#1410

Merged
scottf merged 3 commits intomainfrom
meta-compare
Sep 4, 2025
Merged

consumers - metadata comparison#1410
scottf merged 3 commits intomainfrom
meta-compare

Conversation

@scottf
Copy link
Contributor

@scottf scottf commented Sep 3, 2025

Fixes #1409

comparison now ignores nats header ( _nats. ) but still compares all user headers.

@scottf scottf changed the title consumers - metadata comparison - fixes #1409 consumers - metadata comparison Sep 3, 2025
Copy link
Member

@MauriceVanVeen MauriceVanVeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@scottf
Copy link
Contributor Author

scottf commented Sep 4, 2025

This has manually been confirmed.
1. Run the code against server version 2.10.26, the output was

Meta {foo=bar}

2. Upgrade the server to version 2.11.8, then run the code, the addOrUpdateConsumer did not fail and the output was

Meta {_nats.req.level=0, _nats.ver=2.11.8, _nats.level=1, foo=bar}

Against the old code the run against version 2.11 gave the following

java.lang.IllegalArgumentException: [SUB-90016] Existing consumer cannot be modified. Changed fields: [metadata]

The code used:

private static final String STREAM = "stream1409";
private static final String SUBJECT = "subject1409";
private static final String CONSUMER_NAME = "name1409";
private static final String SERVER = "nats://localhost:4222";

public static void main(String[] args) throws JetStreamApiException, InterruptedException, IOException {
    Options options = Options.builder().server(SERVER).build();
    try (Connection nc = Nats.connect(options)) {
        JetStreamManagement jsm = nc.jetStreamManagement();

        if (nc.getServerInfo().isOlderThanVersion("2.11")) {
            try {
                jsm.deleteStream(STREAM);
            }
            catch (Exception ignore) {
            }

            try {
                jsm.addStream(StreamConfiguration.builder()
                    .name(STREAM)
                    .storageType(StorageType.File)
                    .subjects(SUBJECT)
                    .build());
            }
            catch (Exception e) {
                System.err.println("Fatal error, cannot create stream.");
                System.exit(-1);
            }
        }

        Map<String, String> meta = new HashMap<>();
        meta.put("foo", "bar");
        ConsumerConfiguration cc = ConsumerConfiguration.builder()
            .durable(CONSUMER_NAME)
            .metadata(meta)
            .build();
        PushSubscribeOptions pso = PushSubscribeOptions.builder()
            .stream(STREAM)
            .name(CONSUMER_NAME)
            .configuration(cc).build();

        JetStream js = jsm.jetStream();
        JetStreamSubscription sub = js.subscribe(null, pso);
        ConsumerInfo ci = sub.getConsumerInfo();
        System.out.println("Meta " + ci.getConsumerConfiguration().getMetadata());
    }
}

@scottf scottf merged commit 5ac3549 into main Sep 4, 2025
5 checks passed
@scottf scottf deleted the meta-compare branch September 4, 2025 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

consumers - metadata comparison

2 participants