作者 朱兆平

增加mq发送到交换方法,修改TCS发送回执信息到交换不直接发队列

  1 +package com.tianbo.util.RabitMq.exchange;
  2 +
  3 +public class ExGetMsg {
  4 +}
  1 +package com.tianbo.util.RabitMq.exchange;
  2 +
  3 +import com.rabbitmq.client.AMQP;
  4 +import com.rabbitmq.client.BuiltinExchangeType;
  5 +import com.rabbitmq.client.Channel;
  6 +import com.rabbitmq.client.Connection;
  7 +import com.tianbo.util.RabitMq.ConnectionUtil;
  8 +
  9 +import java.nio.charset.StandardCharsets;
  10 +
  11 +/**
  12 + * 交换接收消息处理类
  13 + */
  14 +public class ExSendMsg {
  15 +
  16 + /**
  17 + * 发送消息到消息交换
  18 + * @param exchangeName 交换路由名称
  19 + * @param exchangeType 交换路由类型
  20 + * @param routingKey 交换路由绑定的routingkey
  21 + * @param queueName 队列名称
  22 + * @param msg 要发送的的消息
  23 + * @param hostIp 服务器ip
  24 + * @param hostPort 服务器端口
  25 + * @param vHostName 服务器vHostName名称
  26 + * @param userName 服务器链接用户名
  27 + * @param password 服务器链接密码
  28 + * @return 返回成功失败 失败=false
  29 + */
  30 + public static boolean sendMsg(String exchangeName,
  31 + String exchangeType,
  32 + String routingKey,
  33 + String queueName,
  34 + String msg,
  35 + String hostIp,
  36 + int hostPort,
  37 + String vHostName,
  38 + String userName,
  39 + String password){
  40 + try{
  41 + // 获取到连接以及mq通道
  42 + Connection connection = ConnectionUtil.getConnection(hostIp,hostPort,vHostName,userName,password);
  43 + // 从连接中创建通道
  44 + Channel channel = connection.createChannel();
  45 + // 声明(创建)队列
  46 + channel.exchangeDeclare(exchangeName, exchangeType,true);
  47 + // 消息内容
  48 + channel.basicPublish(exchangeName, routingKey, null, msg.getBytes(StandardCharsets.UTF_8));
  49 + System.out.println("消息发送成功>>>" + msg + "<<<");
  50 + //关闭通道和连接
  51 + channel.close();
  52 + connection.close();
  53 + }catch (Exception e){
  54 + e.printStackTrace();
  55 + return false;
  56 + }
  57 + return true;
  58 + }
  59 +
  60 +}