apache/pulsar · error · IllegalStateException

Cannot use shareConfigured() when resourceTypes has already

Error message

Cannot use shareConfigured() when resourceTypes has already been set

What it means

The inverse guard of the resourceTypes checks: shareConfigured() (share-everything mode) cannot be used once specific resource types were already registered, since the builder cannot represent both an exclusive list and share-all mode at once.

Source

Thrown at pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java:259

            throw new IllegalStateException("Cannot set resourceTypes when shareConfigured() has already been called");
        }
        return resourceTypes(List.of(sharedResource));
    }

    @Override
    public PulsarClientSharedResourcesBuilder resourceTypes(
            Collection<PulsarClientSharedResources.SharedResource> sharedResource) {
        if (shareConfigured) {
            throw new IllegalStateException("Cannot set resourceTypes when shareConfigured() has already been called");
        }
        sharedResources.addAll(sharedResource);
        return this;
    }

    @Override
    public PulsarClientSharedResourcesBuilder shareConfigured() {
        if (!sharedResources.isEmpty()) {
            throw new IllegalStateException("Cannot use shareConfigured() when resourceTypes has already been set");
        }
        shareConfigured = true;
        return this;
    }

    @SuppressWarnings("unchecked")
    private <T extends ResourceConfig> T getOrCreateConfig(PulsarClientSharedResources.SharedResource sharedResource) {
        return (T) resourceConfigs.computeIfAbsent(sharedResource, k -> {
            switch (sharedResource.getType()) {
                case EventLoopGroup:
                    return new EventLoopResourceConfig();
                case DnsResolver:
                    return new DnsResolverResourceConfig();
                case ThreadPool:
                    return new ThreadPoolResourceConfig();
                case Timer:
                    return new TimerResourceConfig();
                case MemoryLimitController:

View on GitHub (pinned to 820761864e)

Solutions

  1. Remove the shareConfigured() call when you are explicitly listing resourceTypes; the listed types already define what is shared.
  2. Call shareConfigured() before any resourceTypes(...) call if share-all mode is intended, and skip resourceTypes entirely.
  3. Use a new builder instance if the two configurations must coexist for different clients.

Example fix

// before
builder.resourceTypes(SharedResource.EVENT_LOOP_GROUP).shareConfigured(); // throws
// after
builder.resourceTypes(SharedResource.EVENT_LOOP_GROUP); // explicit list only
Defensive patterns

Strategy: validation

Validate before calling

if (!resourcesToShare.isEmpty()) {
    builder.resourceTypes(resourcesToShare); // do not also call shareConfigured()
} else {
    builder.shareConfigured();
}

Try / catch

try {
    builder.shareConfigured();
} catch (IllegalStateException e) {
    log.warn("resourceTypes already set; explicit list wins, skipping share-all");
}

Prevention

When it happens

Trigger: Calling builder.resourceTypes(...) (either overload) and then builder.shareConfigured() on the same builder instance.

Common situations: Fluent chains that append shareConfigured() 'for good measure' after listing resources; merged configuration from two code modules each adding resources then finalizing with shareConfigured(); framework code that always finalizes with shareConfigured().

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/70f56df14c585ea5. Report an issue: GitHub.