Package org.springframework.pulsar.core
Class CachingPulsarProducerFactory<T>
java.lang.Object
org.springframework.pulsar.core.DefaultPulsarProducerFactory<T>
org.springframework.pulsar.core.CachingPulsarProducerFactory<T>
- Type Parameters:
T- producer type.
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean,org.springframework.context.Lifecycle,org.springframework.context.Phased,org.springframework.context.SmartLifecycle,PulsarProducerFactory<T>
A
PulsarProducerFactory that extends the default implementation by caching the created producers.
The created producer is wrapped in a proxy so that calls to Producer.close() do
not actually close it. The actual close occurs when the producer is evicted from the
cache or when DisposableBean.destroy() is invoked.
The proxied producer is cached in an LRU fashion and evicted when it has not been used within a configured time period.
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionCachingPulsarProducerFactory(org.apache.pulsar.client.api.PulsarClient pulsarClient, String defaultTopic, List<ProducerBuilderCustomizer<T>> defaultConfigCustomizers, TopicResolver topicResolver, Duration cacheExpireAfterAccess, Long cacheMaximumSize, Integer cacheInitialCapacity) Construct a caching producer factory with the specified values for the cache configuration. -
Method Summary
Modifier and TypeMethodDescriptionAtomicReference<org.springframework.pulsar.core.RestartableComponentSupport.State>default voiddestroy()protected org.apache.pulsar.client.api.Producer<T>doCreateProducer(org.apache.pulsar.client.api.Schema<T> schema, String topic, Collection<String> encryptionKeys, List<ProducerBuilderCustomizer<T>> customizers) Create the actual producer.default voiddoStart()Callback invoked during startup - default implementation does nothing.voiddoStop()intgetPhase()Return the phase that this lifecycle object is supposed to run in.default booleanorg.springframework.core.log.LogAccessorlogger()default voidstart()default voidstop()Methods inherited from class org.springframework.pulsar.core.DefaultPulsarProducerFactory
createProducer, createProducer, createProducer, getDefaultTopic, getPulsarClient, resolveTopicName, setTopicBuilderMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.SmartLifecycle
isAutoStartup, stop
-
Constructor Details
-
CachingPulsarProducerFactory
public CachingPulsarProducerFactory(org.apache.pulsar.client.api.PulsarClient pulsarClient, @Nullable String defaultTopic, List<ProducerBuilderCustomizer<T>> defaultConfigCustomizers, TopicResolver topicResolver, Duration cacheExpireAfterAccess, Long cacheMaximumSize, Integer cacheInitialCapacity) Construct a caching producer factory with the specified values for the cache configuration.- Parameters:
pulsarClient- the client used to create the producersdefaultTopic- the default topic to use for the producersdefaultConfigCustomizers- the optional list of customizers to apply to the created producerstopicResolver- the topic resolver to usecacheExpireAfterAccess- time period to expire unused entries in the cachecacheMaximumSize- maximum size of cache (entries)cacheInitialCapacity- the initial size of cache
-
-
Method Details
-
doCreateProducer
protected org.apache.pulsar.client.api.Producer<T> doCreateProducer(org.apache.pulsar.client.api.Schema<T> schema, @Nullable String topic, @Nullable Collection<String> encryptionKeys, @Nullable List<ProducerBuilderCustomizer<T>> customizers) Description copied from class:DefaultPulsarProducerFactoryCreate the actual producer.- Overrides:
doCreateProducerin classDefaultPulsarProducerFactory<T>- Parameters:
schema- the schema of the messages to be senttopic- the topic the producer will send messages to ornullto use the default topicencryptionKeys- the encryption keys used by the producer, replacing the default encryption keys ornullto use the default encryption keys. Beware thatProducerBuilderonly hasProducerBuilder.addEncryptionKey(java.lang.String)and doesn't have methods to replace the encryption keys.customizers- the optional list of customizers to apply to the producer builder- Returns:
- the created producer
-
getPhase
public int getPhase()Return the phase that this lifecycle object is supposed to run in.Because this object depends on the restartable client, it uses a phase slightly larger than the one used by the restartable client. This ensures that it starts after and stops before the restartable client.
- Specified by:
getPhasein interfaceorg.springframework.context.Phased- Specified by:
getPhasein interfaceorg.springframework.context.SmartLifecycle- Returns:
- the phase to execute in (just after the restartable client)
- See Also:
-
SmartLifecycle.getPhase()
-
currentState
public AtomicReference<org.springframework.pulsar.core.RestartableComponentSupport.State> currentState() -
logger
public org.springframework.core.log.LogAccessor logger() -
doStop
public void doStop() -
isRunning
default boolean isRunning()- Specified by:
isRunningin interfaceorg.springframework.context.Lifecycle
-
start
default void start()- Specified by:
startin interfaceorg.springframework.context.Lifecycle
-
doStart
default void doStart()Callback invoked during startup - default implementation does nothing. -
stop
default void stop()- Specified by:
stopin interfaceorg.springframework.context.Lifecycle
-
destroy
default void destroy()- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-