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;
@Configuration
public class RabbitMQConfig {
@Bean
public Queue updateBalanceQueue() {
return new Queue(ConstantEnum.UPDATE_BALANCE_QUEUE.getKey(), true);
}
@Bean
public Queue updateAuxiliaryBalanceQueue() {
return new Queue(ConstantEnum.UPDATE_AUXILIARY_BALANCE_QUEUE.getKey(), true);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange(ConstantEnum.TOPIC_EXCHANGE.getKey());
}
@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;
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", "更新科目发生队列名称"),
UPDATE_BALANCE_QUEUE_ROUTING_KEY("updateBalanceQueueRoutingKey", "updateBalanceQueue队列的路由键"),
UPDATE_AUXILIARY_BALANCE_QUEUE("updateAuxiliaryBalanceQueue", "更新辅助核算发生队列名称"),
UPDATE_AUXILIARY_BALANCE_QUEUE_ROUTING_KEY("updateAuxiliaryBalanceQueueRoutingKey", "updateAuxiliaryBalanceQueue队列的路由键"),
TOPIC_EXCHANGE("topicExchange", "topic类型的交换器"),
VOUCHER_NOTE_LIMIT("30", "凭证明细摘要限制记录数");
private String key;
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;
}
}