Files
TKDataSave/src/main/java/com/yupi/springbootinit/config/RabbitMQConfig.java

92 lines
2.9 KiB
Java
Raw Normal View History

2025-06-12 19:44:47 +08:00
package com.yupi.springbootinit.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
2025-08-27 16:42:13 +08:00
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.HeadersExchange;
2025-06-12 19:44:47 +08:00
import org.springframework.amqp.core.Queue;
2025-08-27 16:42:13 +08:00
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
2025-06-12 19:44:47 +08:00
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
2025-08-27 16:42:13 +08:00
2025-06-12 19:44:47 +08:00
private static final String QUEUE = "HOST_INFO_QUEUE";
2025-08-27 16:42:13 +08:00
public static final String EXCHANGE_NAME = "user.headers.exchange";
public static final String AI_CHAT_EXCHANGE_NAME = "ai.chat.headers.exchange";
public static final String BIG_BROTHER_EXCHANGE_NAME = "big.brother.headers.exchange";
public static final String WEB_AI_EXCHANGE_NAME = "web.ai.headers.exchange";
2025-06-12 19:44:47 +08:00
//创建队列
//true:表示持久化
//队列在默认情况下放到内存rabbitmq重启后就丢失了如果希望重启后队列
//数据还能使用,就需要持久化
@Bean
public Queue hostInfoQueue(){
return new Queue(QUEUE,true);
}
//
// @Bean
// public MessageConverter messageConverter(){
// return new Jackson2JsonMessageConverter();
// }
2025-08-27 16:42:13 +08:00
@Bean
public HeadersExchange userHeadersExchange() {
return ExchangeBuilder.headersExchange(EXCHANGE_NAME)
.durable(true)
.build();
}
@Bean
public HeadersExchange aiChatHeadersExchange() {
return ExchangeBuilder.headersExchange(AI_CHAT_EXCHANGE_NAME)
.durable(true)
.build();
}
@Bean
public HeadersExchange bigBrotherHeadersExchange() {
return ExchangeBuilder.headersExchange(BIG_BROTHER_EXCHANGE_NAME)
.durable(true)
.build();
}
@Bean
public HeadersExchange webAiHeadersExchange() {
return ExchangeBuilder.headersExchange(WEB_AI_EXCHANGE_NAME)
.durable(true)
.build();
}
2025-08-27 16:42:13 +08:00
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory cf) {
return new RabbitAdmin(cf);
}
2025-06-12 19:44:47 +08:00
@Bean
public MessageConverter messageConverter() {
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
om.registerModule(new JavaTimeModule());
return new Jackson2JsonMessageConverter(om);
2025-06-12 19:44:47 +08:00
}
}