MQSendMsg.java
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.tianbo.util.RabitMq;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.nio.charset.StandardCharsets;
/**
* 发送 [内容] 到队列
*/
public class MQSendMsg {
/**
*
* @param queueName 队列名称
* @param msg 发送内容
* @return
*/
public static boolean sendMsg(String queueName,String msg,String hostIp,int hostPort,String vHostName,String userName,String password){
try{
// 获取到连接以及mq通道
Connection connection = ConnectionUtil.getConnection(hostIp,hostPort,vHostName,userName,password);
// 从连接中创建通道
Channel channel = connection.createChannel();
// String exchangeName = "amq.topic";
// String routingKey = "consumer.send";
// 声明(创建)队列
channel.queueDeclare(queueName, true, false, false, null);
// 消息内容
channel.basicPublish("", queueName, null, msg.getBytes(StandardCharsets.UTF_8));
System.out.println("消息发送成功>>>" + msg + "<<<");
//关闭通道和连接
channel.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}