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; import org.springframework.amqp.core.ExchangeBuilder; import org.springframework.amqp.core.HeadersExchange; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; 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 { private static final String QUEUE = "HOST_INFO_QUEUE"; 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"; //创建队列 //true:表示持久化 //队列在默认情况下放到内存,rabbitmq重启后就丢失了,如果希望重启后,队列 //数据还能使用,就需要持久化 @Bean public Queue hostInfoQueue(){ return new Queue(QUEUE,true); } // // @Bean // public MessageConverter messageConverter(){ // return new Jackson2JsonMessageConverter(); // } @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(); } @Bean public RabbitAdmin rabbitAdmin(ConnectionFactory cf) { return new RabbitAdmin(cf); } @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); } }