ExSendMsg.java
2.2 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.tianbo.util.RabitMq.exchange;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.tianbo.util.RabitMq.ConnectionUtil;
import java.nio.charset.StandardCharsets;
/**
* 交换接收消息处理类
*/
public class ExSendMsg {
/**
* 发送消息到消息交换
* @param exchangeName 交换路由名称
* @param exchangeType 交换路由类型
* @param routingKey 交换路由绑定的routingkey
* @param queueName 队列名称
* @param msg 要发送的的消息
* @param hostIp 服务器ip
* @param hostPort 服务器端口
* @param vHostName 服务器vHostName名称
* @param userName 服务器链接用户名
* @param password 服务器链接密码
* @return 返回成功失败 失败=false
*/
public static boolean sendMsg(String exchangeName,
String exchangeType,
String routingKey,
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();
// 声明(创建)队列
channel.exchangeDeclare(exchangeName, exchangeType,true);
// 消息内容
channel.basicPublish(exchangeName, routingKey, null, msg.getBytes(StandardCharsets.UTF_8));
System.out.println("消息发送成功>>>" + msg + "<<<");
//关闭通道和连接
channel.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}