HelloWorld工作模式架构图:
1. 引入依赖:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.8.0</version>
</dependency>
2. 通过可视化管理界面添加用户并分配虚拟机
消息队列 RabbitMQ[二] RabbitMQ可视化管控台创建用户并为用户分配虚拟机
3. 生产者代码:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Producer_HelloWorld {
public static void main(String[] args) throws IOException, TimeoutException {
// 1. 创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
// 2. 设置参数
connectionFactory.setHost("127.0.0.1"); // IP地址 默认地址localhost
connectionFactory.setPort(5672); // 端口号 默认5672
connectionFactory.setVirtualHost("/demo_virtual"); // 虚拟机名称 默认/
connectionFactory.setUsername("zdy"); // 用户名 默认guest
connectionFactory.setPassword("zdy"); // 密码 默认guest
// 3. 创建连接 Connection
Connection connection = connectionFactory.newConnection();
// 4. 创建频道 Channel
Channel channel = connection.createChannel();
// 5. 创建队列
/*
* queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map arguments)
* queue: 队列名称
* durable: 是否持久化(当MQ关闭再启动时数据还在)
* exclusive: 是否独占(只能有一个消费者监听该队列)
* autoDelete: 当没有消费者时,是否自动删除队列
* arguments: 参数
* */
channel.queueDeclare("hello_world", true, false, false, null);
// 6. 发送消息
/*
* basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
* exchange: 交换机名称
* routingKey: 路由key
* props: 属性
* body: 要发送的消息
* */
String body = "hello rabbitmq";
channel.basicPublish("", "hello_world", null, body.getBytes());
channel.close();
connection.close();
}
}
4. 消费者代码:
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer_HelloWorld {
public static void main(String[] args) throws IOException, TimeoutException {
// 1. 创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
// 2. 设置参数
connectionFactory.setHost("127.0.0.1"); // IP地址 默认地址localhost
connectionFactory.setPort(5672); // 端口号 默认5672
connectionFactory.setVirtualHost("/demo_virtual"); // 虚拟机名称 默认/
connectionFactory.setUsername("zdy"); // 用户名 默认guest
connectionFactory.setPassword("zdy"); // 密码 默认guest
// 3. 创建连接 Connection
Connection connection = connectionFactory.newConnection();
// 4. 创建频道 Channel
Channel channel = connection.createChannel();
Consumer consumer = new Consumer() {
public void handleConsumeOk(java.lang.String s) {
}
public void handleCancelOk(java.lang.String s) {
}
public void handleCancel(java.lang.String s) throws IOException {
}
public void handleShutdownSignal(java.lang.String s, ShutdownSignalException e) {
}
public void handleRecoverOk(java.lang.String s) {
}
/*
* handleDelivery(java.lang.String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes)
* s: 标识
* envelope: 获取一些信息,交换机路由key等
* basicProperties: 配置信息
* bytes: 接收到的数据
* */
public void handleDelivery(java.lang.String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {
System.out.println(new String(bytes));
}
};
// 5. 接收消息
/*
* basicConsume(String queue, boolean autoAck, Consumer callback)
* queue: 接收队列名称
* autoAck: 是否自动确认(后期消息可靠性文章中进行讲解)
* callback: 接收到消息执行的回调函数
* */
channel.basicConsume("hello_world", true, consumer);
}
}
1. 引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
2. 编写生产者代码(创建新的SpringBoot项目)
2.1 编写配置文件application.yml
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: zdy
password: zdy
virtual-host: /demo_virtual
2.2. 编写RabbitMQ配置文件
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String QUEUE_NAME = "hello_world_queue";
// 2. 创建Queue队列
@Bean("hello_world_queue")
public Queue createQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
}
2.3. 编写测试类
import com.zdy.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHelloWorldSend(){
// 生产者生产消息
rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "hello rabbitmq");
}
}
3. 编写消费者代码(创建新的SpringBoot项目)
3.1. 编写配置文件application.yml(与生产者的一样)
3.2. 编写消息监听类
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQListener {
@RabbitListener(queues = "hello_world_queue")
public void listenQueue(Message message){
System.out.println(new String(message.getBody()));
}
}
3.3. 运行项目
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)