Rabbitmq 生产消息
1.变量从配置文件中取得
2.一个服务端建立一个长连接,所有消息用一个
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
/**
* @description: MQ消息发送工具类
* @date 2021/8/25 10:21
*/
@Slf4j
@Component
public class SendMsgUntil {
@Value("${spring.rabbitmq.host}")
private String host = null;
@Value("${spring.rabbitmq.port}")
private Integer port = null;
@Value("${spring.rabbitmq.username}")
private String userName = null;
@Value("${spring.rabbitmq.password}")
private String password = null;
public static ConnectionFactory factory = null;
/**
* 持久消息队列配置
*/
public static AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
.deliveryMode(2)
.contentType("application/json")
.contentEncoding("UTF-8")
.build();
/**
* 非持久配置
*/
public static AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder()
.contentEncoding("UTF-8") // 编码方式
.contentType("application/json")
.build();
public SendMsgUntil(){
}
/**
* @description: MQ消息发送 非持久
* @param exgName routingKey obj
* @author panlupeng
* @date 2021/8/25 10:20
*/
public static Connection con = null;
public static Channel channel = null;
public void sendMsg(String exgName,String routingKey,String obj) {
if(null == factory){
factory = new ConnectionFactory();
factory.setHost(host);
factory.setPort(port);
factory.setUsername(userName);
factory.setPassword(password);
}
try {
//创建连接工厂
if(null == con || !con.isOpen()){
con = factory.newConnection();
}
if(null == channel || !channel.isOpen()){
channel = con.createChannel();
}
channel.basicPublish(exgName, routingKey, basicProperties, obj.getBytes("UTF-8"));
} catch(Exception e) {
try {
if (null != channel) {
channel.close();
}
}catch (Exception e1){
log.error(e1.getMessage());
}
try{
if(null != con){
con.close();
}
}catch (Exception e2){
log.error(e2.getMessage());
}
log.error(e.getMessage(),e);
}
}
/**
* @description: MQ消息发送 持久
* @param exgName routingKey obj
* @author panlupeng
* @date 2021/8/25 10:20
*/
public void sendMsgDurable(String exgName,String routingKey,String obj) {
if(null == factory){
factory = new ConnectionFactory();
factory.setHost(host);
factory.setPort(port);
factory.setUsername(userName);
factory.setPassword(password);
}
try {
//创建连接工厂
if(null == con || !con.isOpen()){
con = factory.newConnection();
}
if(null == channel || !channel.isOpen()){
channel = con.createChannel();
}
channel.basicPublish(exgName, routingKey, properties, obj.getBytes("UTF-8"));
} catch(Exception e) {
try {
if (null != channel) {
channel.close();
}
}catch (Exception e1){
log.error(e1.getMessage());
}
try{
if(null != con){
con.close();
}
}catch (Exception e2){
log.error(e2.getMessage());
}
log.error(e.getMessage(),e);
}
}
/**
* @description: 数据转化
* @param obj
* @return bytep[]
* @author panlupeng
* @date 2021/8/25 10:20
*/
private byte[] toBytes(Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
oos.close();
return baos.toByteArray();
}catch(Exception e) {
log.error(e.getMessage());
}
return null;
}
}