use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project pulsar by apache.
the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.
@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
if (schema != null && schema.supportSchemaVersioning()) {
final SchemaInfoProvider schemaInfoProvider;
try {
schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
} catch (ExecutionException e) {
log.error("Failed to load schema info provider for topic {}", topicName, e);
return FutureUtil.failedFuture(e.getCause());
}
schema = schema.clone();
if (schema.requireFetchingSchemaInfo()) {
@SuppressWarnings("rawtypes") Schema finalSchema = schema;
return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
if (null == schemaInfo) {
if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
// no schema info is found
return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
}
}
try {
log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
} catch (RuntimeException re) {
return FutureUtil.failedFuture(re);
}
finalSchema.setSchemaInfoProvider(schemaInfoProvider);
return CompletableFuture.completedFuture(finalSchema);
});
} else {
schema.setSchemaInfoProvider(schemaInfoProvider);
}
}
return CompletableFuture.completedFuture(schema);
}
use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project incubator-pulsar by apache.
the class KeyValueSchemaImpl method setSchemaInfoProviderOnSubschemas.
private void setSchemaInfoProviderOnSubschemas() {
this.keySchema.setSchemaInfoProvider(new SchemaInfoProvider() {
@Override
public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getKey());
}
@Override
public CompletableFuture<SchemaInfo> getLatestSchema() {
return CompletableFuture.completedFuture(((AbstractStructSchema<K>) keySchema).schemaInfo);
}
@Override
public String getTopicName() {
return "key-schema";
}
});
this.valueSchema.setSchemaInfoProvider(new SchemaInfoProvider() {
@Override
public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getValue());
}
@Override
public CompletableFuture<SchemaInfo> getLatestSchema() {
return CompletableFuture.completedFuture(((AbstractStructSchema<V>) valueSchema).schemaInfo);
}
@Override
public String getTopicName() {
return "value-schema";
}
});
}
use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project incubator-pulsar by apache.
the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.
@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
if (schema != null && schema.supportSchemaVersioning()) {
final SchemaInfoProvider schemaInfoProvider;
try {
schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
} catch (ExecutionException e) {
log.error("Failed to load schema info provider for topic {}", topicName, e);
return FutureUtil.failedFuture(e.getCause());
}
schema = schema.clone();
if (schema.requireFetchingSchemaInfo()) {
@SuppressWarnings("rawtypes") Schema finalSchema = schema;
return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
if (null == schemaInfo) {
if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
// no schema info is found
return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
}
}
try {
log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
} catch (RuntimeException re) {
return FutureUtil.failedFuture(re);
}
finalSchema.setSchemaInfoProvider(schemaInfoProvider);
return CompletableFuture.completedFuture(finalSchema);
});
} else {
schema.setSchemaInfoProvider(schemaInfoProvider);
}
}
return CompletableFuture.completedFuture(schema);
}
use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project pulsar by apache.
the class KeyValueSchemaImpl method setSchemaInfoProviderOnSubschemas.
private void setSchemaInfoProviderOnSubschemas() {
this.keySchema.setSchemaInfoProvider(new SchemaInfoProvider() {
@Override
public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getKey());
}
@Override
public CompletableFuture<SchemaInfo> getLatestSchema() {
return CompletableFuture.completedFuture(((AbstractStructSchema<K>) keySchema).schemaInfo);
}
@Override
public String getTopicName() {
return "key-schema";
}
});
this.valueSchema.setSchemaInfoProvider(new SchemaInfoProvider() {
@Override
public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getValue());
}
@Override
public CompletableFuture<SchemaInfo> getLatestSchema() {
return CompletableFuture.completedFuture(((AbstractStructSchema<V>) valueSchema).schemaInfo);
}
@Override
public String getTopicName() {
return "value-schema";
}
});
}
use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project pulsar by yahoo.
the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.
@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
if (schema != null && schema.supportSchemaVersioning()) {
final SchemaInfoProvider schemaInfoProvider;
try {
schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
} catch (ExecutionException e) {
log.error("Failed to load schema info provider for topic {}", topicName, e);
return FutureUtil.failedFuture(e.getCause());
}
schema = schema.clone();
if (schema.requireFetchingSchemaInfo()) {
@SuppressWarnings("rawtypes") Schema finalSchema = schema;
return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
if (null == schemaInfo) {
if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
// no schema info is found
return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
}
}
try {
log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
} catch (RuntimeException re) {
return FutureUtil.failedFuture(re);
}
finalSchema.setSchemaInfoProvider(schemaInfoProvider);
return CompletableFuture.completedFuture(finalSchema);
});
} else {
schema.setSchemaInfoProvider(schemaInfoProvider);
}
}
return CompletableFuture.completedFuture(schema);
}
Aggregations