2017-09-11 46 views
2

我在這裏看到了一些相關的問題,但沒有爲我工作,兔子不會序列化來自另一個應用程序的消息。RabbitMQ沒有序列化消息,錯誤轉換

Caused by: org.springframework.amqp.AmqpException: No method found for class [B 

在我的配置類下面接收消息。

@Configuration 
public class RabbitConfiguration implements RabbitListenerConfigurer{ 

    public final static String EXCHANGE_NAME = "wallet-accounts"; 
    public final static String QUEUE_PAYMENT = "wallet-accounts.payment"; 
    public final static String QUEUE_RECHARGE = "wallet-accounts.recharge"; 

    @Bean 
    public List<Declarable> ds() { 
     return queues(QUEUE_PAYMENT, QUEUE_RECHARGE); 
    } 

    @Autowired 
    private ConnectionFactory rabbitConnectionFactory; 

    @Bean 
    public AmqpAdmin amqpAdmin() { 
     return new RabbitAdmin(rabbitConnectionFactory); 
    } 

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

    private List<Declarable> queues(String ... names){ 
     List<Declarable> result = new ArrayList<>(); 

     for (int i = 0; i < names.length; i++) { 
      result.add(makeQueue(names[i])); 
      result.add(makeBinding(names[i])); 
     } 
     return result; 
    } 

    private static Binding makeBinding(String queueName){ 
     return new Binding(queueName, DestinationType.QUEUE, EXCHANGE_NAME, queueName, null); 
    } 

    private static Queue makeQueue(String name){ 
     return new Queue(name); 
    } 

    @Bean 
    public MappingJackson2MessageConverter jackson2Converter() { 
     MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); 
     return converter; 
    } 

    @Bean 
    public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() { 
     DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory(); 
     factory.setMessageConverter(jackson2Converter()); 
     return factory; 
    } 

    @Override 
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) { 
     registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory()); 
    } 
} 

使用這種其他配置,誤差幾乎是一樣的:

Caused by: org.springframework.amqp.support.converter.MessageConversionException: failed to resolve class name. Class not found [br.com.beblue.wallet.payment.application.accounts.PaymentEntryCommand] 

配置:

@Configuration 
public class RabbitConfiguration { 

    public final static String EXCHANGE_NAME = "wallet-accounts"; 

    public final static String QUEUE_PAYMENT = "wallet-accounts.payment"; 
    public final static String QUEUE_RECHARGE = "wallet-accounts.recharge"; 

    @Bean 
    public List<Declarable> ds() { 
     return queues(QUEUE_PAYMENT, QUEUE_RECHARGE); 
    } 

    @Autowired 
    private ConnectionFactory rabbitConnectionFactory; 

    @Bean 
    public AmqpAdmin amqpAdmin() { 
     return new RabbitAdmin(rabbitConnectionFactory); 
    } 

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

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

    private List<Declarable> queues(String ... names){ 
     List<Declarable> result = new ArrayList<>(); 

     for (int i = 0; i < names.length; i++) { 
      result.add(makeQueue(names[i])); 
      result.add(makeBinding(names[i])); 
     } 
     return result; 
    } 

    private static Binding makeBinding(String queueName){ 
     return new Binding(queueName, DestinationType.QUEUE, EXCHANGE_NAME, queueName, null); 
    } 

    private static Queue makeQueue(String name){ 
     return new Queue(name); 
    } 
} 

誰能告訴我有什麼不對這些設置,或缺少了什麼?

回答

3

找不到類[B方法

意味着有一個默認SimpleMessageConverter無法轉換傳入application/json。它只是不知道那content-type,只是回落到byte[]返回。

類未發現[br.com.beblue.wallet.payment.application.accounts.PaymentEntryCommand]

就是說Jackson2JsonMessageConverter不能轉換您application/json因爲進入__TypeId__頭,代表類的該內容在本地類路徑中找不到。

那麼,絕對你的配置DefaultMessageHandlerMethodFactory沒有意義的AMQP轉換。您應該考慮使用SimpleRabbitListenerContainerFactory bean定義及其setMessageConverter。是的,考慮注入適當的org.springframework.amqp.support.converter.MessageConverter實現。

https://docs.spring.io/spring-amqp/docs/1.7.3.RELEASE/reference/html/_reference.html#async-annotation-conversion

從春天啓動的角度有SimpleRabbitListenerContainerFactoryConfigurer配置對此事:

https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/#boot-features-using-amqp-receiving

+0

泰阿爾喬姆,問題是,我沒有在消費方式配置工廠: @RabbitListener(id =「listenerPayment」,queues = RabbitConfiguration.QUEUE_PAYMENT,containerFactory =「myFactory」) –