tencent cloud

Feedback

Spring Boot Starter

Last updated: 2024-06-28 11:33:56

    Overview

    This document describes how to use Spring Boot Starter to send and receive messages and helps you better understand the message sending and receiving processes.

    Prerequisites

    Directions

    Step 1. Add dependencies

    Import Pulsar Starter dependencies to the project.
    <dependency>
    <groupId>io.github.majusko</groupId>
    <artifactId>pulsar-java-spring-boot-starter</artifactId>
    <version>1.0.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core -->
    <dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.4.11</version>
    </dependency>
    

    Step 2. Prepare configurations

    Add Pulsar configuration information to the configuration file.
    pulsar:
    # Namespace name
    namespace: namespace_java
    # Service access address
    service-url: http://pulsar-xxx.tdmq.ap-gz.public.tencenttdmq.com:8080
    # Role token
    token-auth-value: eyJrZXlJZC....
    # Cluster name
    tenant: pulsar-xxx
    
    Parameter
    Description
    namespace
    Namespace name, which can be copied on the Namespace page in the console.
    service-url
    Cluster access address, which can be viewed and copied on the Cluster page in the console.
    
    
    
    token-auth-value
    Role token, which can be copied in the **Token** column on the Role Management page.
    
    
    
    tenant
    Cluster ID, which can be obtained on the Cluster page in the console.

    Step 3. Produce messages

    1. Configure the producer.
    @Configuration
    public class ProducerConfiguration {
    
    @Bean
    public ProducerFactory producerFactory() {
    return new ProducerFactory()
    // topic1
    .addProducer("topic1")
    // topic2
    .addProducer("topic2");
    }
    }
    
    2. Inject the producer.
    @Autowired
    private PulsarTemplate<byte[]> defaultProducer;
    
    3. Send messages.
    // Send the messages
    defaultProducer.send("topic2", ("Hello pulsar client, this is a order message.").getBytes(StandardCharsets.UTF_8));
    
    Note:
    The topic that sends messages is the one declared in the producer configuration.
    The type of PulsarTemplate must be the same as that of the sent message.
    When you send a message to the specified topic, the message type must be the same as that bound to the topic in the producer factory configuration.

    Step 4. Consume messages

    Configure the consumer.
    @PulsarConsumer(topic = "topic1", // Name of the subscribed topic
    subscriptionName = "sub_topic1", // Subscription name
    serialization = Serialization.JSON, // Serialization method
    subscriptionType = SubscriptionType.Shared, // Subscription mode, which is exclusive mode by default
    consumerName = "firstTopicConsumer", // Consumer name
    maxRedeliverCount = 3, // Maximum number of retries
    deadLetterTopic = "sub_topic1-DLQ" // Dead letter topic name
    )
    public void topicConsume(byte[] msg) {
    // TODO process your message
    System.out.println("Received a new message. content: [" + new String(msg) + "]");
    // If the consumption fails, throw an exception, so that the message will enter the retry letter topic and can be consumed again. Once the maximum number of retries is reached, the message will enter the dead letter topic. You need to create retry and dead letter queues for the above process
    }
    

    Step 5. Query messages

    Log in to the console and enter the Message Query page to view the message trace after running the demo.
    
    The message trace is as follows:
    
    
    Note:
    The above is a simple configuration for using TDMQ for Apache Pulsar through Spring Boot Starter. For more information, see Demo or Spring Boot Starter for Apache Pulsar.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support