apache/pulsar · error · IllegalStateException
Cannot set resourceTypes when shareConfigured() has already
Error message
Cannot set resourceTypes when shareConfigured() has already been called
What it means
PulsarClientSharedResourcesBuilder supports two mutually exclusive sharing modes: explicitly listing shared resource types via resourceTypes(...), or sharing everything via shareConfigured(). The builder tracks shareConfigured and refuses further resourceTypes(...) calls once shareConfigured() was called, to keep the mode unambiguous.
Source
Thrown at pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java:241
static class OpenTelemetryResourceConfig implements ResourceConfig, OpenTelemetryConfig {
OpenTelemetry openTelemetry;
public OpenTelemetryResourceConfig() {
}
@Override
public OpenTelemetryConfig openTelemetry(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
return this;
}
}
@Override
public PulsarClientSharedResourcesBuilder resourceTypes(
PulsarClientSharedResources.SharedResource... sharedResource) {
if (shareConfigured) {
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");View on GitHub (pinned to 820761864e)
Solutions
- Remove the shareConfigured() call and list only the resource types you want via resourceTypes(...).
- Reorder the fluent chain so resourceTypes(...) is called before any shareConfigured()-dependent code (though the two are mutually exclusive, so pick one).
- Use a fresh builder instance for each configuration mode instead of reusing one.
Example fix
// before builder.shareConfigured().resourceTypes(SharedResource.EVENT_LOOP_GROUP); // throws // after builder.resourceTypes(SharedResource.EVENT_LOOP_GROUP); // selective sharing
Defensive patterns
Strategy: validation
Validate before calling
if (builder instanceof PulsarClientSharedResourcesBuilderImpl
&& shareModeAlreadySet) {
throw new IllegalStateException("Choose either shareConfigured() or resourceTypes(...), not both");
} Try / catch
try {
builder.shareConfigured();
} catch (IllegalStateException e) {
log.warn("Builder already has explicit resourceTypes; skipping share-all mode");
} Prevention
- Pick one sharing mode per builder up front: either shareConfigured() or an explicit resourceTypes list.
- Configure shared resources in one code path, not scattered across modules.
- Use fresh builder instances per client rather than reusing configured builders.
- Document the chosen sharing mode in your client configuration code.
When it happens
Trigger: Calling builder.shareConfigured() and then builder.resourceTypes(...) (varargs overload) on the same builder instance.
Common situations: Copy-pasted builder setup where shareConfigured() is called early and resourceTypes() added later; merging configuration code paths that each configure the builder; migration from 'share all' to selective sharing without removing shareConfigured().
Related errors
- Cannot use shareConfigured() when resourceTypes has already
- Try to reserve/release memory failed, the param memorySize i
- Failed to decode message from topic ${topic} with schemaId $
- The schema is not a KeyValueSchema
- Pulsar client has been closed, can not build LookupService w
AI-assisted analysis of apache/pulsar@820761864e (2026-09-06).
Data as JSON: /api/errors/6126890bf3081e0a.
Report an issue: GitHub.