rabbitmq熟考ノート

springboot + rabbitmq熟考ノート

<dependency>
  <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
package com.hzrys.atplatform.finance.config;

import com.hzrys.atplatform.finance.enums.ConstantEnum;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMQConfig
 * @Description TODO
 * @Author Administrator
 * @Date 2019/1/4 0004 下午 2:29
 * @Version 1.0
 **/
@Configuration
public class RabbitMQConfig {
    
    


    /**
     * 创建更新科目发生队列
     *
     * @return
     */
    @Bean
    public Queue updateBalanceQueue() {
    
    
        return new Queue(ConstantEnum.UPDATE_BALANCE_QUEUE.getKey(), true);
    }


    /**
     * 创建更新辅助核算发生队列
     *
     * @return
     */
    @Bean
    public Queue updateAuxiliaryBalanceQueue() {
    
    
        return new Queue(ConstantEnum.UPDATE_AUXILIARY_BALANCE_QUEUE.getKey(), true);
    }


    /**
     * 创建一个 topic 类型的交换器
     *
     * @return
     */
    @Bean
    public TopicExchange exchange() {
    
    
        return new TopicExchange(ConstantEnum.TOPIC_EXCHANGE.getKey());
    }


    /**
     * 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange)
     *
     * @param exchange
     * @return
     */
    @Bean
    public Binding updateBalanceBinding(TopicExchange exchange) {
    
    
        return BindingBuilder.bind(updateBalanceQueue()).to(exchange).with(ConstantEnum.UPDATE_BALANCE_QUEUE_ROUTING_KEY.getKey());
    }

    @Bean
    public Binding updateAuxiliaryBalanceBinding(TopicExchange exchange) {
    
    
        return BindingBuilder.bind(updateAuxiliaryBalanceQueue()).to(exchange).with(ConstantEnum.UPDATE_AUXILIARY_BALANCE_QUEUE_ROUTING_KEY.getKey());
    }

    @Bean
    public ConnectionFactory connectionFactory() {
    
    
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.152.34", 5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
    
    
        return new RabbitTemplate(connectionFactory);
    }
}

package com.hzrys.atplatform.finance.enums;

/**
 * 模块 常量枚举
 *
 * @Author sdfs
 */
public enum ConstantEnum {
    
    
    /**
     * 科目下拉列表分类
     */
    SUBJECT_SELECT_LIST("select", "下拉选择列表"),

    SUBJECT_ADD_LIST("add", "添加列表"),

    SUBJECT_ALL_LIST("all","所有信息列表"),
    /**
     * 科目借贷方向 -借方
     */
    SUBJECT_DIRECTION_DEBIT("1", "借方"),
    /**
     * 科目借贷方向 -贷方
     */
    SUBJECT_DIRECTION_CREDIT("-1", "贷方"),
    /**
     * 更新科目发生 队列名称
     */
    UPDATE_BALANCE_QUEUE("updateBalanceQueue", "更新科目发生队列名称"),
    /**
     * updateBalanceQueue队列的路由键
     */
    UPDATE_BALANCE_QUEUE_ROUTING_KEY("updateBalanceQueueRoutingKey", "updateBalanceQueue队列的路由键"),
    /**
     * 更新辅助核算发生 队列名称
     */
    UPDATE_AUXILIARY_BALANCE_QUEUE("updateAuxiliaryBalanceQueue", "更新辅助核算发生队列名称"),
    /**
     * updateAuxiliaryBalanceQueue队列的路由键
     */
    UPDATE_AUXILIARY_BALANCE_QUEUE_ROUTING_KEY("updateAuxiliaryBalanceQueueRoutingKey", "updateAuxiliaryBalanceQueue队列的路由键"),
    /**
     * topic 类型的交换器
     */
    TOPIC_EXCHANGE("topicExchange", "topic类型的交换器"),
    /**
     * 凭证明细摘要 限制记录数
     */
    VOUCHER_NOTE_LIMIT("30", "凭证明细摘要限制记录数");
    /**
     * 常量key
     */
    private String key;
    /**
     * 常量desc
     */
    private String desc;

    ConstantEnum(String key, String desc) {
    
    
        this.key = key;
        this.desc = desc;
    }

    public static ConstantEnum getConstantEnum(String key) {
    
    
        for (ConstantEnum ce : ConstantEnum.values()) {
    
    
            if (ce.getKey().equals(key)) {
    
    
                return ce;
            }
        }
        return null;
    }

    public String getKey() {
    
    
        return key;
    }

    public String getDesc() {
    
    
        return desc;
    }
}

おすすめ

転載: blog.csdn.net/u011445756/article/details/88123680