rabbitmq 生产消息

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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值