1.在Linux环境上面装入rabbitMQ
doker-compose.yml
XML
version: "3.1"
services:
rabbitmq:
image: daocloud.io/library/rabbitmq:management
restart: always
container_name: rabbitmq
ports:
- 6786:5672
- 16786:15672
volumes:
- ./data:/var/lib/rabbitmq
doker-compose up -d 运行
2.进入rabbitMQ提供的客户端路径
自己的路径,和客户端端口号
http://8.140.244.227: 16786
3.在客户端注册虚拟主机
4.创建角色
5.给角色绑定虚拟主机
6.导入RabbitMQ依赖
XML
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.6.0</version>
</dependency>
7.写个工具类,获取连接
java
package com.qf.springbootRbMQ.utils;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class MQUtils {
public static Connection getConnection() throws Exception {
//创建连接工厂对象
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置MQ服务器的相关信息
connectionFactory.setHost("8.140.244.227");
connectionFactory.setPort(6786);//注意:不要写成管理工具的端口号
connectionFactory.setUsername("root");
connectionFactory.setPassword("1234");
connectionFactory.setVirtualHost("/email");//设置虚拟主机
Connection connection = connectionFactory.newConnection();
return connection;
}
}
8.写提供者类Send
java
package com.qf.springbootRbMQ.email;
import com.qf.springbootRbMQ.entity.EmailMessage;
import com.qf.springbootRbMQ.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.springframework.util.SerializationUtils;
public class Send {
//队列的名字
public static final String QUEUE_NAME="QQEmail";
public static void main(String[] args) throws Exception {
//1.获取连接对象
Connection conn = MQUtils.getConnection();
//2. 创建一个channel对象,对于MQ的大部分操作,都定义在了channel对象上
Channel channel = conn.createChannel();
//3.声明了一个队列
/**
* queue -- the name of the queue
* durable -- true代表创建的队列是持久化的(当mq重启后,该对立依然存在)
* exclusive -- 该队列是不是排他的 (该对列是否只能由当前创建该队列的连接使用)
* autoDelete -- 该队列是否可以被mq服务器自动删除
* arguments -- 队列的其他参数,可以为null
*/
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
EmailMessage emailMessage = new EmailMessage();
emailMessage.setQq("1393087444@QQ.com");
emailMessage.setSubject("你好啊,又见面了,发送邮箱给你啊!!!");
emailMessage.setText("<p style='color:green'>谢谢你看我的邮件啦啦啦~~~</p>");
byte[] bytes = SerializationUtils.serialize(emailMessage);
//生产者如何发送消息,使用下面的方法即可
/**
* exchange -- 交换机的名字 ,如果是空串,说明是把消息发给了默认交换机
* routingKey -- 路由的key,当发送消息给默认交换机时,routingkey代表队列的名字
* other properties - 消息的其他属性,可以为null
* body -- 消息的内容,注意,要是有 字节数组
*/
channel.basicPublish("", QUEUE_NAME, null, bytes);
System.out.println(" [x] Sent '" + emailMessage + "'");
//关闭资源
channel.close();
conn.close();
}
}
9.写消费者类Recv
java
package com.qf.springbootRbMQ.email;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.qf.springbootRbMQ.entity.EmailMessage;
import com.qf.springbootRbMQ.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
public class Recv {
private final static String QUEUE_NAME="QQEmail";
public static void custormer() throws Exception {
//1.获取连接对象
Connection conn = MQUtils.getConnection();
//2. 创建一个channel对象,对于MQ的大部分操作,都定义在了channel对象上
Channel channel = conn.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//3.该消费者收到消息之后的处理逻辑,写在DeliverCallback对象中
DeliverCallback deliverCallback =new DeliverCallback() {
@Override
public void handle(String consumerTag, Delivery message) throws IOException {
//这个相当于标识,消费者的ID
System.out.println(consumerTag);
//从Delivery对象中可以获取到生产者,发送的消息的字节数组
byte[] body = message.getBody();
EmailMessage emailMessage = (EmailMessage) SerializationUtils.deserialize(body);
System.out.println(emailMessage);
//在这里写消费者的业务逻辑,例如,发送邮件
MailAccount account = new MailAccount();
account.setHost("smtp.qq.com"); // 设置SMTP服务器地址
account.setPort(25); // 设置SMTP服务器端口
account.setAuth(true); // 设置是否需要身份认证
account.setFrom("1393087444@qq.com"); // 设置发件人邮箱地址
account.setUser("1393087444@qq.com"); // 设置用户名
account.setPass("gqrjqpilpadcjbdi"); // 设置密码
// 发送邮件
MailUtil.send(account, CollUtil.newArrayList("1393087444@qq.com"),emailMessage.getSubject(),emailMessage.getText(),false);
}
};
//4.让当前消费者开始消费(QUEUE_NAME)队列中的消息
/**
* queue -- the name of the queue
* autoAck -- true 代表当前消费者是不是自动确认模式。true代表自动确认。
* deliverCallback -- 当有消息发送给该消费者时,消费者如何处理消息的逻辑
* cancelCallback -- 当消费者被取消掉时,如果要执行代码,写到这里
*/
channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> {});
}
public static void main(String[] args) throws Exception {
custormer();
}
}