Mr.Ki.D.
Mr.Ki.D.

Reputation: 121

Spring AMQP Error: Listener method could not be invoked with the incoming message

I have classes:

public class MqMessage implements Serializable {
    private String event;
    private String absolutePath;
    private boolean isDirectory;
    private Integer hash;
    private Node node;

    get/set
}

Configuration class:

public class RabbitConfiguration {
    
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory =
                new CachingConnectionFactory("localhost");
        return connectionFactory;
    }

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

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setExchange("exchange-events");
        return template;
    }

    //объявляем очередь
    @Bean
    public Queue myQueue1() {
        return new Queue("queue-events");
    }

    @Bean
    public FanoutExchange fanoutExchangeA() {
        return new FanoutExchange("exchange-events");
    }

    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(myQueue1()).to(fanoutExchangeA());
    }

Send message

    public class ServerHandler implements EventHandler {
    
        //сама структура отражающая состояние файлов, содеражащая метоы для работы с ними
        @Autowired
        Node fileTreeRoot;
    
        SimpleMessageConverter simpleMessageConverter;
    
        @Override
        public void setRoot(Node fileTreeRoot) {
            this.fileTreeRoot = fileTreeRoot;
        }
        @Autowired
        RabbitTemplate rabbitTemplate;
    
        //логика обработки событий
        @Override
        public void eventHandle(String event, String path) {
    
            /*business logic */
            rabbitTemplate.setExchange("exchange-events");
    
            rabbitTemplate.convertAndSend(new MqMessage(event,fileTreeRoot));
            return;
        }
    
        public ServerHandler() {
        
    }

Listener:

public class Client {
Node rootNodeClient = new Node();
EventHandler handlerClient = new ClientHandler();


@RabbitListener(queues = "queue-events")
public void onMessage(MqMessage message) {
    System.out.println(message.getNode().hashCode());
    rootNodeClient = message.getNode();
}

a have error only start app

2017-08-08 12:58:02.128 WARN 5024 --- [cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message

Caused by: org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException: Could not resolve method parameter at index 0 in public void prcjt.client.Client.onMessage(prcjt.message.MqMessage): 1 error(s): [Error in object 'message': codes []; arguments []; default message [Payload value must not be empty]]

Error does not always exist Help please

Upvotes: 2

Views: 16033

Answers (3)

Sri
Sri

Reputation: 46

I faced this issue for couple of reasons:

  1. Not using "implements Serializable" (which I see you are already using)

  2. No constructor in model class. (Use empty constructor)

    public ClassToGetQData() {
        super();
        // TODO Auto-generated constructor stub
    }
    

Also, using @JsonIgnoreProperties(ignoreUnknown = true) helped me with missing data elements in incoming data.

Upvotes: 1

Vikash kumar Yadav
Vikash kumar Yadav

Reputation: 171

Using mappingJackson2MessageConverted did not work for me. After lots of investigation found that I was using wrong converter. Add the below code in Listener service, it worked for me.

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

Upvotes: 4

Dave Pateral
Dave Pateral

Reputation: 1475

Caused by: org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException: Could not resolve method parameter at index 0

From the exception information, it seems that spring could not resolve MqMessage in the listener correctly, you can try to add a mappingJackson2MessageConverter to the client. Refer to this link.

Upvotes: 1

Related Questions