Class AvroSchemaRegistryClientMessageConverter

java.lang.Object
org.springframework.messaging.converter.AbstractMessageConverter
org.springframework.cloud.stream.schema.registry.avro.AbstractAvroMessageConverter
org.springframework.cloud.stream.schema.registry.avro.AvroSchemaRegistryClientMessageConverter
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean, org.springframework.messaging.converter.MessageConverter, org.springframework.messaging.converter.SmartMessageConverter

public class AvroSchemaRegistryClientMessageConverter extends AbstractAvroMessageConverter implements org.springframework.beans.factory.InitializingBean
A MessageConverter for Apache Avro, with the ability to publish and retrieve schemas stored in a schema server, allowing for schema evolution in applications. The supported content types are in the form `application/*+avro`.

During the conversion to a message, the converter will set the 'contentType' header to 'application/[prefix].[subject].v[version]+avro', where:

    • prefix is a configurable prefix (default 'vnd');
      subject is a subject derived from the type of the outgoing object - typically the class name;
      version is the schema version for the given subject;
  • When converting from a message, the converter will parse the content-type and use it to fetch and cache the writer schema using the provided SchemaRegistryClient.

    Author:
    Marius Bogoevici, Vinicius Carvalho, Oleg Zhurakousky, Sercan Karaoglu, Ish Mahajan
    • Field Details

      • AVRO_FORMAT

        public static final String AVRO_FORMAT
        Avro format defined in the Mime type.
        See Also:
      • PREFIX_VALIDATION_PATTERN

        public static final Pattern PREFIX_VALIDATION_PATTERN
        Pattern for validating the prefix to be used in the publised subtype.
      • CACHE_PREFIX

        public static final String CACHE_PREFIX
        Spring Cloud Stream schema property prefix.
        See Also:
      • REFLECTION_CACHE_NAME

        public static final String REFLECTION_CACHE_NAME
        Property for reflection cache.
        See Also:
      • SCHEMA_CACHE_NAME

        public static final String SCHEMA_CACHE_NAME
        Property for schema cache.
        See Also:
      • REFERENCE_CACHE_NAME

        public static final String REFERENCE_CACHE_NAME
        Property for reference cache.
        See Also:
      • DEFAULT_AVRO_MIME_TYPE

        public static final org.springframework.util.MimeType DEFAULT_AVRO_MIME_TYPE
        Default Mime type for Avro.
      • schemaImports

        protected org.springframework.core.io.Resource[] schemaImports
    • Constructor Details

      • AvroSchemaRegistryClientMessageConverter

        public AvroSchemaRegistryClientMessageConverter(SchemaRegistryClient schemaRegistryClient, org.springframework.cache.CacheManager cacheManager, AvroSchemaServiceManager manager)
        Creates a new instance, configuring it with SchemaRegistryClient and CacheManager.
        Parameters:
        schemaRegistryClient - the SchemaRegistryClient used to interact with the schema registry server.
        cacheManager - instance of CacheManager to cache parsed schemas. If caching is not required use NoOpCacheManager
        manager - instance of AvroSchemaServiceManager to manage schemas.
    • Method Details

      • isDynamicSchemaGenerationEnabled

        public boolean isDynamicSchemaGenerationEnabled()
      • setDynamicSchemaGenerationEnabled

        public void setDynamicSchemaGenerationEnabled(boolean dynamicSchemaGenerationEnabled)
        Allows the converter to generate and register schemas automatically. If set to false, it only allows the converter to use pre-registered schemas. Default 'true'.
        Parameters:
        dynamicSchemaGenerationEnabled - true if dynamic schema generation is enabled
      • setSchemaLocations

        public void setSchemaLocations(org.springframework.core.io.Resource[] schemaLocations)
        A set of locations where the converter can load schemas from. Schemas provided at these locations will be registered automatically.
        Parameters:
        schemaLocations - array of locations
      • setSchemaImports

        public void setSchemaImports(org.springframework.core.io.Resource[] schemaImports)
        A set of schema locations where should be imported first. Schemas provided at these locations will be reference, thus they should not reference each other.
        Parameters:
        schemaImports - array of schema imports
      • setPrefix

        public void setPrefix(String prefix)
        Set the prefix to be used in the published subtype. Default 'vnd'.
        Parameters:
        prefix - prefix to be set
      • setReaderSchema

        public void setReaderSchema(org.springframework.core.io.Resource readerSchema)
      • setSubjectNamingStrategy

        public void setSubjectNamingStrategy(SubjectNamingStrategy subjectNamingStrategy)
      • setSubjectNamePrefix

        public void setSubjectNamePrefix(String subjectNamePrefix)
      • afterPropertiesSet

        public void afterPropertiesSet()
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      • toSubject

        protected String toSubject(String subjectNamePrefix, org.apache.avro.Schema schema)
      • supports

        protected boolean supports(Class<?> clazz)
        Specified by:
        supports in class org.springframework.messaging.converter.AbstractMessageConverter
      • supportsMimeType

        protected boolean supportsMimeType(org.springframework.messaging.MessageHeaders headers)
        Overrides:
        supportsMimeType in class org.springframework.messaging.converter.AbstractMessageConverter
      • resolveSchemaForWriting

        protected org.apache.avro.Schema resolveSchemaForWriting(Object payload, org.springframework.messaging.MessageHeaders headers, org.springframework.util.MimeType hintedContentType)
        Specified by:
        resolveSchemaForWriting in class AbstractAvroMessageConverter
      • resolveWriterSchemaForDeserialization

        protected org.apache.avro.Schema resolveWriterSchemaForDeserialization(org.springframework.util.MimeType mimeType)
        Specified by:
        resolveWriterSchemaForDeserialization in class AbstractAvroMessageConverter
      • resolveReaderSchemaForDeserialization

        protected org.apache.avro.Schema resolveReaderSchemaForDeserialization(Class<?> targetClass)
        Specified by:
        resolveReaderSchemaForDeserialization in class AbstractAvroMessageConverter
      • setIgnoreSchemaRegistryServer

        public void setIgnoreSchemaRegistryServer(boolean ignoreSchemaRegistryServer)