|
|
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;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|