tencent cloud

All product documents
TDMQ for RabbitMQ
Spring Boot Starter
Last updated: 2024-01-03 11:45:32
Spring Boot Starter
Last updated: 2024-01-03 11:45:32

Overview

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

Prerequisites

You have created the required resources as instructed in Resource Creation and Preparation.

Directions

Step 1. Add dependencies

Add dependencies to the pom.xml file.
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Step 2. Prepare configurations

1. Add RabbitMQ configuration information to the configuration file (with the YAML configuration as an example).
spring:
rabbitmq:
# The host address can be obtained in the console. You can also use the RabbitMQ service address
host: amqp-xx.rabbitmq.x.tencenttdmq.com
port: 5672
# Name of the role to be used, which can be obtained on the **Role Management** page in the console
username: admin
# Role token
password: eyJrZXlJZ....
# Full name of vhost, which can be obtained on the **Vhost** tab in the console
virtual-host: amqp-xxx|Vhost
Parameter
Description
host
Cluster access address, which can be obtained from the Client Access section on the Basic Info page of the cluster.
Loading…

port
Cluster access address, which can be obtained from the Client Access section on the Basic Info page of the cluster.
username
Enter the name of the user created in the console.
password
Enter the password of the user created in the console.
virtual-host
Vhost name, which can be obtained from the vhost list in the console.
2. Create a configuration file loading program (with the Fanout exchange as an example).
Note
For the configurations of exchanges in other types, see Demo.
/**
* Fanout exchange configuration
*/
@Configuration
public class FanoutRabbitConfig {

/**
* Exchange
*/
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout-logs", true, false);
}


/**
* Message queue
*/
@Bean
public Queue fanoutQueueA() {
return new Queue("ps_queue", true);
}

@Bean
public Queue fanoutQueueB() {
// You can use this method to bind a dead letter queue
Map<String, Object> requestParam = new HashMap<>();
requestParam.put("x-dead-letter-exchange", "my-deadLetter-exchange");
// Set the message validity period
requestParam.put("x-message-ttl", 60000);
return new Queue("ps_queue1", true, false,true, requestParam);
}

/**
* Bind the message queue to the exchange
*/
@Bean
public Binding bindingFanoutA() {
return BindingBuilder.bind(fanoutQueueA())
.to(fanoutExchange());
}

@Bean
public Binding bindingFanoutB() {
return BindingBuilder.bind(fanoutQueueB())
.to(fanoutExchange());
}
}
Parameter
Description
fanout-logs
Bound exchange name, which can be obtained from the exchange list in the console.
ps_queue
Name of the first queue bound to the exchange, which can be obtained from the queue list in the console.
my-deadLetter-exchange
Dead letter exchange name, which can be obtained from the exchange list in the console.
ps_queue1
Name of the second queue bound to the exchange, which can be obtained from the queue list in the console.

Step 3. Send messages

Create and compile the message sending program DemoApplication.java and use RabbitTemplate to send message.
@Autowired
private RabbitTemplate rabbitTemplate;

public String send() {
String msg = "This is a new message.";
// Send the message
// Parameter description: Parameter 1: Exchange name, which can be obtained from the exchange list in the console. Parameter 2: Routing key. Parameter 3: Message content
rabbitTemplate.convertAndSend("direct_logs", "", msg);
return "success";
}

Step 4. Consume messages

Create and compile the message receiving program FanoutReceiver.java (with Fanout exchange as an example).
@Component
public class FanoutReceiver {
// Register a listener to listen on the specified message queue
@RabbitHandler
@RabbitListener(queues = "ps_queue") // Name of the queue bound to the exchange, which can be obtained from the queue list in the console
public void listenerPsQueue(String msg) {
// Business processing...
System.out.println("(ps_queue) receiver message. [" + msg + "]");
}
}

Step 5. View messages

If you want to confirm whether the messages have been successfully sent to TDMQ for RabbitMQ, you can view the status of connected consumers on the Cluster> Queue page in the console.

Note
For other samples, see Spring AMQP official documentation.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

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