在 RabbitMQ 2.3.6 中从消息队列中侦听消息时更改了数据类型

时间:2021-05-03 14:07:06

标签: rabbitmq spring-rabbit

我在 Spring 应用程序中使用 Spring Rabbit 2.3.6。我有评论实体列表并通过此功能发送到消息队列

public void deletePost(long postId){
    Post post = postRepository.findById(postId)
            .orElseThrow(() -> new PostNotFoundException(String.valueOf(postId)));
    List<Comment> comments = postBase.getComments(post);
    postRepository.delete(post);

    //post event
    Map<String, Object> inputs= new HashMap<>();
    inputs.put(InputParam.OBJECT_ID, postId);
    inputs.put(InputParam.COMMENTS, comments);
    messagingUtil.postEvent(SnwObjectType.POST, SnwActionType.DELETE, inputs);
}

收听留言

@RabbitListener(queues = MessagingConfig.QUEUE)
public void consumeMessageFromQueue(Map<String, Object> inputs) {
        
}

这是我听完消息后收到的数据。评论列表中每个元素的数据类型都发生了变化,我不能将其用作评论。

enter image description here

你有什么解决数据类型不被改变的方法吗?

更新:消息配置

@Configuration
public class MessagingConfig {
    public static final String QUEUE = "javatechie_queue";
    public static final String EXCHANGE = "javatechie_exchange";
    public static final String ROUTING_KEY = "javatechie_routingKey";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

    @Bean
    public MessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate template(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(converter());
        return rabbitTemplate;
    }
}

0 个答案:

没有答案
相关问题