消息中间件的通信交互基于队列和通道,下面说明并记录几种向RabbitMQ中间件发送消息的方法。
1.调用自写工具类的函数
在工具类中将连接与通道建立,随后发送消息到指定队列。
工具类:
/**
* @Author Lee
* @Date 2021/5/18 15:28
*/
public class ProducerUtil {
/**
* 获取连接
*
*/
public static Connection getConnection(){
//新建连接工厂类
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置主机地址信息
connectionFactory.setHost("81.70.132.101");
connectionFactory.setPort(5672);
//用户信息
connectionFactory.setUsername("admin");
connectionFactory.setPassword("Aisino123.,");
connectionFactory.setConnectionTimeout(600000);
connectionFactory.setVirtualHost("/");
Connection connection = null;
try
{
connection = connectionFactory.newConnection();
}catch (Exception e)
{
e.printStackTrace();
}
return connection;
}
/**
* 建立通道
*
*/
public static Channel getChannel(Connection connection, String queueName, String exchangeName) throws IOException
{
//创建通道
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(exchangeName,"direct",true);
//声明队列
channel.queueDeclare(queueName,true,false,false,null);
//绑定交换机与队列
channel.queueBind(queueName,exchangeName,queueName);
//同一时刻发送消息的数量
channel.basicQos(1);
return channel;
}
/**
* 发送消息
*
*/
public static void sendMessage(Channel channel,String queueName,String exchangeName,String msg) throws IOException
{
System.out.println("当前发送的消息是 :"+msg);
channel.basicPublish(exchangeName,queueName,null, msg.getBytes());
}
/**
* 关闭资源
*
*/
public static void closeResource(Connection connection,Channel channel){
try
{
if (channel.isOpen()) {
channel.abort();
}
if (connection.isOpen()) {
connection.close();
}
}
catch (Exception e)
{
System.out.println("-----------------------"+e);
}
}
}
发送消息:
@Component
@Service
public class LoginPublisher {
public String sendMessage() throws IOException {
//获取MQ的连接、通道信息
Connection connection = ProducerUtil.getConnection();
Channel channel = ProducerUtil.getChannel(connection,MqConst.QUEUE_LOGINLOG,MqConst.QUEUE_LOGINLOG+MqConst.EXCHANGE_SUF);
//组装消息体
JSONObject jsonObject = new JSONObject();
jsonObject.put("system_name","测试系统12");
jsonObject.put("login_count","61");
//发送消息
ProducerUtil.sendMessage(channel,MqConst.QUEUE_LOGINLOG,MqConst.QUEUE_LOGINLOG+MqConst.EXCHANGE_SUF,jsonObject.toJSONString());
//关闭资源
ProducerUtil.closeResource(connection,channel);
return "success";
}
}
2.使用MQ通道的发送函数
建立连接与通道后,使用通道的方法来发送消息到队列。
@Component
@Service
public class AnalysePublisher {
public String sendMessage() throws IOException, TimeoutException {
/*
* 创建连接工厂与连接信息
*
*/
ConnectionFactory f = new ConnectionFactory();
f.setHost("81.70.132.101");
f.setPort(5672);
f.setUsername("admin");
f.setPassword("Aisino123.,");
/*
* 与rabbitmq服务器建立连接与通道
*
*/
Connection c = f.newConnection();
Channel ch = c.createChannel();
/*
* 声明队列
*
*/
// ch.queueDeclare("task_queue", true,false,false,null);
/*
* 发布消息
*
*/
JSONObject jsonObject = new JSONObject();
jsonObject.put("year","2021");
jsonObject.put("month","01");
jsonObject.put("service_field","07");
jsonObject.put("involved_field_count",1125);
jsonObject.put("service_field_type","01");
ch.basicPublish("class5toclass6_analyseshow_EX", "class5toclass6_analyseshow_RK", MessageProperties.PERSISTENT_TEXT_PLAIN, jsonObject.toJSONString().getBytes());
System.out.println("消息已发送: "+jsonObject.toJSONString());
// c.close();
return "success";
}
}
3.使用rabbit模板的发送函数
使用封装好的rabbit模板来发送消息。
@Component
@Service
public class ClassifyPublisher {
public String sendMessage() throws IOException {
//方法一
JSONObject jsonObject = new JSONObject();
jsonObject.put("service_field","06");
jsonObject.put("service_field_type","01");
jsonObject.put("involved_field_count",900);
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.convertAndSend
//交换机
("class1toclass6_classifyshow_EX",
//路由键
"class1toclass6_classifyshow_RK",
//包含消息的json对象
jsonObject);
//方法二
//获取MQ的连接、通道信息
// Connection connection = ProducerUtil.getConnection();
// Channel channel = ProducerUtil.getChannel(connection,MqConst.QUEUE_CLASSIFY,MqConst.QUEUE_CLASSIFY+MqConst.EXCHANGE_SUF);
// //组装消息体
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("service_field","05");
// jsonObject.put("service_field_type","02");
// jsonObject.put("involved_field_count",1000);
// //发送消息
// ProducerUtil.sendMessage(channel,MqConst.QUEUE_CLASSIFY,MqConst.QUEUE_CLASSIFY+MqConst.EXCHANGE_SUF,jsonObject.toJSONString());
// //关闭资源
// ProducerUtil.closeResource(connection,channel);
return "success";
}
}