Package org.springframework.pulsar.core
Class PulsarTemplate<T>
java.lang.Object
org.springframework.pulsar.core.PulsarTemplate<T>
- Type Parameters:
T
- the message payload type
- All Implemented Interfaces:
Aware
,BeanNameAware
,SmartInitializingSingleton
,ApplicationContextAware
,PulsarOperations<T>
public class PulsarTemplate<T>
extends Object
implements PulsarOperations<T>, ApplicationContextAware, BeanNameAware, SmartInitializingSingleton
A template for executing high-level Pulsar operations.
- Author:
- Soby Chacko, Chris Bono, Alexander Preuß, Christophe Bornet, Jonas Geiregat
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static interface
A callback for executing arbitrary operations on aPulsarTemplate
.Nested classes/interfaces inherited from interface org.springframework.pulsar.core.PulsarOperations
PulsarOperations.SendMessageBuilder<T>
-
Constructor Summary
ConstructorDescriptionPulsarTemplate
(PulsarProducerFactory<T> producerFactory) Construct a template instance without interceptors that uses the default schema resolver.PulsarTemplate
(PulsarProducerFactory<T> producerFactory, List<org.apache.pulsar.client.api.interceptor.ProducerInterceptor> interceptors) Construct a template instance with interceptors that uses the default schema resolver and default topic resolver and enables observation recording.PulsarTemplate
(PulsarProducerFactory<T> producerFactory, List<org.apache.pulsar.client.api.interceptor.ProducerInterceptor> interceptors, SchemaResolver schemaResolver, TopicResolver topicResolver, boolean observationEnabled) Construct a template instance with optional observation configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
If observations are enabled, attempt to obtain the Observation registry and convention.<R> R
executeInTransaction
(PulsarTemplate.TemplateCallback<T, R> callback) Execute some arbitrary operation(s) on the template and return the result.void
logWarningForLambdaCustomizer
(long frequency) How often to log a warning when a Lambda producer builder customizer is used.newMessage
(T message) Create abuilder
for configuring and sending a message.org.apache.pulsar.client.api.MessageId
Sends a message to the specified topic in a blocking manner.org.apache.pulsar.client.api.MessageId
Sends a message to the specified topic in a blocking manner.org.apache.pulsar.client.api.MessageId
Sends a message to the default topic in a blocking manner.org.apache.pulsar.client.api.MessageId
Sends a message to the default topic in a blocking manner.CompletableFuture<org.apache.pulsar.client.api.MessageId>
Sends a message to the specified topic in a non-blocking manner.CompletableFuture<org.apache.pulsar.client.api.MessageId>
Sends a message to the specified topic in a non-blocking manner.CompletableFuture<org.apache.pulsar.client.api.MessageId>
Sends a message to the default topic in a non-blocking manner.CompletableFuture<org.apache.pulsar.client.api.MessageId>
Sends a message to the default topic in a non-blocking manner.void
setApplicationContext
(ApplicationContext applicationContext) void
setBeanName
(String beanName) Gets the transaction properties.
-
Constructor Details
-
PulsarTemplate
Construct a template instance without interceptors that uses the default schema resolver.- Parameters:
producerFactory
- the factory used to create the backing Pulsar producers.
-
PulsarTemplate
public PulsarTemplate(PulsarProducerFactory<T> producerFactory, List<org.apache.pulsar.client.api.interceptor.ProducerInterceptor> interceptors) Construct a template instance with interceptors that uses the default schema resolver and default topic resolver and enables observation recording.- Parameters:
producerFactory
- the factory used to create the backing Pulsar producers.interceptors
- the interceptors to add to the producer.
-
PulsarTemplate
public PulsarTemplate(PulsarProducerFactory<T> producerFactory, List<org.apache.pulsar.client.api.interceptor.ProducerInterceptor> interceptors, SchemaResolver schemaResolver, TopicResolver topicResolver, boolean observationEnabled) Construct a template instance with optional observation configuration.- Parameters:
producerFactory
- the factory used to create the backing Pulsar producersinterceptors
- the list of interceptors to add to the producerschemaResolver
- the schema resolver to usetopicResolver
- the topic resolver to useobservationEnabled
- whether to record observations
-
-
Method Details
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
-
transactions
Gets the transaction properties.- Returns:
- the transaction properties
- Since:
- 1.1.0
-
logWarningForLambdaCustomizer
public void logWarningForLambdaCustomizer(long frequency) How often to log a warning when a Lambda producer builder customizer is used.- Parameters:
frequency
- how often to log warning (every Nth occurrence) or non-positive to not log warning.
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()If observations are enabled, attempt to obtain the Observation registry and convention.- Specified by:
afterSingletonsInstantiated
in interfaceSmartInitializingSingleton
-
send
Description copied from interface:PulsarOperations
Sends a message to the default topic in a blocking manner.- Specified by:
send
in interfacePulsarOperations<T>
- Parameters:
message
- the message to send- Returns:
- the id assigned by the broker to the published message
-
send
public org.apache.pulsar.client.api.MessageId send(@Nullable T message, @Nullable org.apache.pulsar.client.api.Schema<T> schema) Description copied from interface:PulsarOperations
Sends a message to the default topic in a blocking manner.- Specified by:
send
in interfacePulsarOperations<T>
- Parameters:
message
- the message to sendschema
- the schema to use ornull
to send using the default schema resolution- Returns:
- the id assigned by the broker to the published message
-
send
Description copied from interface:PulsarOperations
Sends a message to the specified topic in a blocking manner.- Specified by:
send
in interfacePulsarOperations<T>
- Parameters:
topic
- the topic to send the message to ornull
to send to the default topicmessage
- the message to send- Returns:
- the id assigned by the broker to the published message
-
send
public org.apache.pulsar.client.api.MessageId send(@Nullable String topic, @Nullable T message, @Nullable org.apache.pulsar.client.api.Schema<T> schema) Description copied from interface:PulsarOperations
Sends a message to the specified topic in a blocking manner.- Specified by:
send
in interfacePulsarOperations<T>
- Parameters:
topic
- the topic to send the message to ornull
to send to the default topicmessage
- the message to sendschema
- the schema to use ornull
to send using the default schema resolution- Returns:
- the id assigned by the broker to the published message
-
sendAsync
Description copied from interface:PulsarOperations
Sends a message to the default topic in a non-blocking manner.- Specified by:
sendAsync
in interfacePulsarOperations<T>
- Parameters:
message
- the message to send- Returns:
- a future that holds the id assigned by the broker to the published message
-
sendAsync
public CompletableFuture<org.apache.pulsar.client.api.MessageId> sendAsync(@Nullable T message, @Nullable org.apache.pulsar.client.api.Schema<T> schema) Description copied from interface:PulsarOperations
Sends a message to the default topic in a non-blocking manner.- Specified by:
sendAsync
in interfacePulsarOperations<T>
- Parameters:
message
- the message to sendschema
- the schema to use ornull
to send using the default schema resolution- Returns:
- a future that holds the id assigned by the broker to the published message
-
sendAsync
public CompletableFuture<org.apache.pulsar.client.api.MessageId> sendAsync(@Nullable String topic, @Nullable T message) Description copied from interface:PulsarOperations
Sends a message to the specified topic in a non-blocking manner.- Specified by:
sendAsync
in interfacePulsarOperations<T>
- Parameters:
topic
- the topic to send the message to ornull
to send to the default topicmessage
- the message to send- Returns:
- a future that holds the id assigned by the broker to the published message
-
sendAsync
public CompletableFuture<org.apache.pulsar.client.api.MessageId> sendAsync(@Nullable String topic, @Nullable T message, @Nullable org.apache.pulsar.client.api.Schema<T> schema) Description copied from interface:PulsarOperations
Sends a message to the specified topic in a non-blocking manner.- Specified by:
sendAsync
in interfacePulsarOperations<T>
- Parameters:
topic
- the topic to send the message to ornull
to send to the default topicmessage
- the message to sendschema
- the schema to use ornull
to send using the default schema resolution- Returns:
- a future that holds the id assigned by the broker to the published message
-
newMessage
Description copied from interface:PulsarOperations
Create abuilder
for configuring and sending a message.- Specified by:
newMessage
in interfacePulsarOperations<T>
- Parameters:
message
- the payload of the message- Returns:
- the builder to configure and send the message
-
setBeanName
- Specified by:
setBeanName
in interfaceBeanNameAware
-
executeInTransaction
Execute some arbitrary operation(s) on the template and return the result. The template is invoked within a local transaction and do not participate in a global transaction (if present).- Type Parameters:
R
- the callback return type- Parameters:
callback
- the callback- Returns:
- the result
- Since:
- 1.1.0
-