spring boot 将配置文件properties 配置数据序列化对象

背景:将properties 配置的mq信息序列化对象进行链接


##properties配置文件mq链接信息
consumer.groupName=consumer_group
consumer.url=127.0.0.1:9411
consumer.user=root
consumer.pwd=1q2w3e4r
consumer.clusterName=sitech
consumer.tenantId=5
consumer.topic=billingMessage
package com.sitech.crmtpd.dto;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @oauth: qiangSW
 * @date: 2020/3/31 13:34
 * @description: com.sitech.crmtpd.dto
 * @doc:
 */
@Component
public class MqInfo {


    public static String groupName;

    public static String url;

    public static String user;

    public static String password;

    public static String clusterName;

    public static String tenantId;

    public static String topic;

    @Value("${consumer.groupName}")
    public  void setGroupName(String groupName) {
        MqInfo.groupName = groupName;
    }

    @Value("${consumer.url}")
    public  void setUrl(String url) {
        MqInfo.url = url;
    }

    @Value("${consumer.user}")
    public  void setUser(String user) {
        MqInfo.user = user;
    }

    @Value("${consumer.pwd}")
    public  void setPassword(String password) {
        MqInfo.password = password;
    }
    @Value("${consumer.clusterName}")
    public  void setClusterName(String clusterName) {
        MqInfo.clusterName = clusterName;
    }

    @Value("${consumer.tenantId}")
    public  void setTenantId(String tenantId) {
        MqInfo.tenantId = tenantId;
    }

    @Value("${consumer.topic}")
    public void setCrmTopic(String topic) {
        MqInfo.topic = topic;
    }


}
package com.sitech.crmtpd.gtgmq;

import com.ctg.mq.api.CTGMQFactory;
import com.ctg.mq.api.IMQProducer;
import com.ctg.mq.api.IMQPushConsumer;
import com.ctg.mq.api.PropertyKeyConst;
import com.ctg.mq.api.exception.MQException;
import com.sitech.crmtpd.dto.MqInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @author LiJie
 * @Description
 * @date 2019/1/29
 */
@Slf4j
@Component
public class ConsumerInst {


    public static IMQPushConsumer consumer = null;

    static {

        Properties consumerConf = new Properties();
        consumerConf.setProperty(PropertyKeyConst.ConsumerGroupName, MqInfo.groupName);
        consumerConf.setProperty(PropertyKeyConst.NamesrvAddr, MqInfo.url);
        consumerConf.setProperty(PropertyKeyConst.NamesrvAuthID, MqInfo.user);
        consumerConf.setProperty(PropertyKeyConst.NamesrvAuthPwd, MqInfo.password);
        consumerConf.setProperty(PropertyKeyConst.ClusterName, MqInfo.clusterName);
        consumerConf.setProperty(PropertyKeyConst.TenantID, MqInfo.tenantId);
        consumer = CTGMQFactory.createPushConsumer(consumerConf);
        int connectResult = 0;
        try {
            connectResult = consumer.connect();
        } catch (MQException e) {

            log.error("消息中间件连接失败{}---{}:", e.getExpCode(), e.getExpDesc(), e);
        }
        if (connectResult != 0) {
            log.error("消息中间件连接失败!");
        }
        log.info("消息中间件连接成功!");
    }
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值