import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
@Component
@Slf4j
public class RabbitCallbackMqUtils implements AutoCloseable {
@Value("${rabbitmq.callback.host}")
private String callbackHost;
@Value("${rabbitmq.callback.port}")
private Integer callbackPort;
@Value("${rabbitmq.callback.username}")
private String callbackUsername;
@Value("${rabbitmq.callback.password}")
private String callbackPassword;
@Value("${rabbitmq.callback.virtual-host}")
private String callbackVirtualHost;
@Value("${rabbitmq.callback.exchange}")
private String callbackExchange;
@Value("${rabbitmq.callback.queue}")
private String callbackQueue;
@Value("${rabbitmq.callback.routing.key}")
private String callbackRoutingKey;
private Connection connection;
private Channel channel;
/**
* 发送对象消息
*
* @param obj
*/
public void sendObjectMessage(Object obj) {
try {
if (!isHostReachable(callbackHost, callbackPort, 2000)) {
log.error("MQ服务器无法访问:{}:{}", callbackHost, callbackPort);
return;
}
if (connection == null || !connection.isOpen()) {
initConnection();
}
// 对象转 JSON
String json = JacksonUtil.toJson(obj);
if (StringUtils.isNotBlank(json)) {
channel.basicPublish(callbackExchange, callbackRoutingKey, null, json.getBytes(StandardCharsets.UTF_8));
}
log.info("回调消息发送成功:{}", json);
} catch (Exception e) {
log.error("回调消息发送失败", e);
}
}
/**
* 初始化连接
*
* @throws Exception
*/
private void initConnection() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(callbackHost);
factory.setPort(callbackPort);
factory.setUsername(callbackUsername);
factory.setPassword(callbackPassword);
factory.setVirtualHost(callbackVirtualHost);
factory.setConnectionTimeout(3000);
factory.setHandshakeTimeout(3000);
this.connection = factory.newConnection();
this.channel = connection.createChannel();
channel.exchangeDeclare(callbackExchange, "direct", true);
channel.queueDeclare(callbackQueue, true, false, false, null);
channel.queueBind(callbackQueue, callbackExchange, callbackRoutingKey);
log.info("RabbitMQ 回调连接初始化成功");
}
/**
* 判断 IP:端口 是否能通
*
* @param host
* @param port
* @param timeoutMillis
* @return
*/
private boolean isHostReachable(String host, int port, int timeoutMillis) {
try (Socket socket = new Socket()) {
socket.connect(new java.net.InetSocketAddress(host, port), timeoutMillis);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public void close() {
try {
if (channel != null && channel.isOpen()) {
channel.close();
}
if (connection != null && connection.isOpen()) {
connection.close();
}
} catch (Exception e) {
log.error("关闭连接异常", e);
}
}
}
Rabbitmq发消息工具类
qingcyb2026-04-01 8:32
相关推荐
逆境不可逃1 天前
黑马 RabbitMq 基础篇 学习记录过期动态2 天前
【RabbitMQ基础篇】RabbitMQ从入门到实战weixin_456723163 天前
Java项目的rabbitmq配置vhost铁皮哥3 天前
【后端开发】RabbitMQ、RocketMQ、Kafka 怎么选?我从业务场景重新梳理了一遍宇之广曜4 天前
从 MQ 到 Celery:把异步任务、状态表、重试补偿和 Outbox 一次讲清楚何中应4 天前
RabbitMQ集群搭建未来龙皇小蓝4 天前
SpringBoot API日志系统设计-02:线程池异步化与RabbitMQ解耦晓庆的故事簿5 天前
JAVA搭配RabbitMQ使用qq_269870436 天前
java rabbitmq 队列在Springboot的设计_F_y6 天前
仿RabbitMQ实现消息队列-客户端模块实现