以下是Spring使用RabbitMQ进行消息传递的完整攻略,包含两个示例说明。
示例1:简单队列模式
步骤1:添加依赖
在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
步骤2:配置文件
在application.properties文件中添加以下配置:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
步骤3:发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Producer implements CommandLineRunner {
@Autowired
private RabbitTemplate rabbitTemplate;
public static void main(String[] args) {
SpringApplication.run(Producer.class, args);
}
@Override
public void run(String... args) throws Exception {
String message = "Hello World!";
rabbitTemplate.convertAndSend("my_queue", message);
System.out.println("Message sent: " + message);
}
}
步骤4:接收消息
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Consumer {
public static void main(String[] args) {
SpringApplication.run(Consumer.class, args);
}
@RabbitListener(queues = "my_queue")
public void receiveMessage(String message) {
System.out.println("Message received: " + message);
}
}
步骤5:运行程序
运行发送消息的程序后,您将看到以下输出:
Message sent: Hello World!
运行接收消息的程序后,您将看到以下输出:
Message received: Hello World!
示例2:工作队列模式
步骤1:添加依赖
同示例1。
步骤2:配置文件
同示例1。
步骤3:发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Producer implements CommandLineRunner {
@Autowired
private RabbitTemplate rabbitTemplate;
public static void main(String[] args) {
SpringApplication.run(Producer.class, args);
}
@Override
public void run(String... args) throws Exception {
for (int i = 1; i <= 10; i++) {
String message = "Message " + i;
rabbitTemplate.convertAndSend("my_queue", message);
System.out.println("Message sent: " + message);
}
}
}
步骤4:接收消息
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Consumer {
public static void main(String[] args) {
SpringApplication.run(Consumer.class, args);
}
@RabbitListener(queues = "my_queue")
public void receiveMessage(String message) throws InterruptedException {
System.out.println("Message received: " + message);
Thread.sleep(5000);
System.out.println("Message processed");
}
}
步骤5:运行程序
运行发送消息的程序后,您将看到以下输出:
Message sent: Message 1
Message sent: Message 2
Message sent: Message 3
Message sent: Message 4
Message sent: Message 5
Message sent: Message 6
Message sent: Message 7
Message sent: Message 8
Message sent: Message 9
Message sent: Message 10
运行接收消息的程序后,您将看到以下输出:
Message received: Message 1
Message received: Message 2
Message received: Message 3
Message received: Message 4
Message received: Message 5
Message received: Message 6
Message received: Message 7
Message received: Message 8
Message received: Message 9
Message received: Message 10
Message processed
Message processed
Message processed
Message processed
Message processed
Message processed
Message processed
Message processed
Message processed
Message processed
以上就是Spring使用RabbitMQ进行消息传递的完整攻略,包含两个示例说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:spring 使用RabbitMQ进行消息传递的示例代码 - Python技术站