Spring Boot 2 Rabbitmq MessageConverter无法正常工作

时间:2019-03-14 22:27:02

标签: spring-rabbitmq

我正在使用springboot和rabitmq。我这里也有类似的情况。

  1. 我想将消息映射到自定义Java对象
  2. 我还想要传递传递标签,这是消息的专有性
  3. 我也希望通过频道,因为我需要手动确认消息 我的代码就是这样
@RunWith(SpringRunner.class)
public class EPPQ2SubscriberTest {

    @Autowired
    private RabbitListenerEndpointRegistry registry;
    public final String sampleMessage = "{" + "\"header\": {" + "\"RETRY_COUNT\":0," + "\"PUBLISH_EVENT_TYPE\":\"AUTH\""
            + "}," + "\"payLoad\":{" + "\"MTI\": \"120\"," + "\"MTI_REQUEST\": \"120\","
            + "\"PAN\": \"6011000000000000\"" + "}" + "}";

    @Test
    public void message_converter_test() throws Exception {
        SimpleMessageListenerContainer container = (SimpleMessageListenerContainer) this.registry
                .getListenerContainer("messageListener");
        ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
        Message message = MessageBuilder.withBody(sampleMessage.getBytes())
                .andProperties(MessagePropertiesBuilder.newInstance().setContentType("application/json").build())
                .build();
        listener.onMessage(message, mock(Channel.class));
    }

    @Configuration
    @EnableRabbit
    public static class config {
        @Bean
        public ConnectionFactory mockConnectionFactory() {
            return mock(ConnectionFactory.class);
        }

        @Bean
        public MessageConverter messageConverter() {
            Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
            /*
             * DefaultClassMapper classMapper = new DefaultClassMapper();
             * classMapper.setDefaultType(com.discover.dftp.scrubber.domain.Message.class);
             * messageConverter.setClassMapper(classMapper);
             */
            return messageConverter;
        }

        @Bean
        public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
            SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
            factory.setConnectionFactory(mockConnectionFactory());
            factory.setMessageConverter(messageConverter());
            factory.setAutoStartup(false);
            return factory;
        }

        @Bean
        public EPPQ2Subscriber messageListener() {
            return new EPPQ2Subscriber();
        }
    }
}
@Component
public class EPPQ2Subscriber {
    private static final Logger LOGGER = LoggerFactory.getLogger(EPPQ2Subscriber.class);

    // @RabbitListener(queues = "#{queue.getName()}") @TODO I wann to use this in
    // later point in time.. !
    @RabbitListener(id = "messageListener", queues = "TestQueue")
    public void receiveMessage(Message message, Channel channel/* ,@Header(AmqpHeaders.DELIVERY_TAG) long tag */) {
        LOGGER.info("Method receiveMessage invoked");
        message.getMessageProperties().getDeliveryTag();
        LOGGER.info("Result:" + message.getClass() + ":" + message.toString());
    }
}
public class Message implements Serializable {

    private static final long serialVersionUID = 1L;
    private Map<String, Object> header;
    private Map<String, Object> payLoad;

    public Map<String, Object> getHeader() {
        return header;
    }

    public void setHeader(Map<String, Object> header) {
        this.header = header;
    }

    public Map<String, Object> getPayLoad() {
        return payLoad;
    }

    public void setPayLoad(Map<String, Object> payLoad) {
        this.payLoad = payLoad;
    }

    @Override
    public String toString() {
        return "Header [header=" + this.header + ", payLoad=" + this.payLoad + "]";
    }
}

1 个答案:

答案 0 :(得分:0)

@RabbitListener(id = "messageListener", queues = "TestQueue")
public void receiveMessage(Message message, Channel channel/* ,@Header(AmqpHeaders.DELIVERY_TAG) long tag */) {
    LOGGER.info("Method receiveMessage invoked");
    message.getMessageProperties().getDeliveryTag();
    LOGGER.info("Result:" + message.getClass() + ":" + message.toString());
}

我似乎该方法收到了原始的(未转换的)Spring AMQP消息(您正在Message中导入错误的EPPQ2Subscriber类)。