Rabbit消息侦听器的执行失败,并且未设置ErrorHandler。无法使用参数类型= [class [B],调用目标方法,

时间:2014-04-23 09:56:05

标签: java spring rabbitmq spring-amqp

我正在使用spring amqp rabbitmq,并使用

发送消息
Message message = MessageBuilder
.withBody(item.toString().getBytes())
.setReplyTo("importReply")
.setCorrelationId(item.toString().getBytes()).build();

我的留言处理程序是

public class Foundation {   
    public Message importExchange(Message exchange) {
        System.out.println("Command:" + exchange.getBody());        
                Message message = MessageBuilder
                .withBody(exchange.getBody().toString().getBytes()).setCorrelationId(exchange.getMessageProperties().getCorrelationId()                                  .toString().getBytes()).build();

        return message; 
    }
}

我已经使用

连接了它
<rabbit:listener-container
    connection-factory="rabbitConnectionFactory" concurrency="10">
    <rabbit:listener queues="${rabbitmq.import.queue}"
        ref="foundation" method="importExchange" />
    <rabbit:listener queues="${rabbitmq.import.reply.queue}"
        ref="importExchangeItemWriter" method="replyHandler" />
</rabbit:listener-container>

但我得到了以下的执行

Execution of Rabbit message listener failed, and no ErrorHandler has been set.
org.springframework.amqp.rabbit.listener.ListenerExecutionFailedException: Failed to invoke target method 'importExchange' with argument type = [class [B], value = [{[B@427829d8}]
    at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:483)
    at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:374)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:647)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:573)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:75)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:154)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1111)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:556)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:904)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:888)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$500(SimpleMessageListenerContainer.java:75)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:989)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoSuchMethodException: com.stockopedia.symfony.Foundation.importExchange([B)
    at java.lang.Class.getMethod(Class.java:1665)
    at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:178)
    at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:466)
    ... 12 more

和类似的问题也在生产者方面的回复手册

public void replyHandler(Message message) {
    System.out.println("In Reply Handler:" + message.getMessageProperties().getCorrelationId());

}

另外,如果importExchange中有任何异常,如何在replyHandler中获得异常?

2 个答案:

答案 0 :(得分:7)

由于您使用POJO进行消息监听,因此无法处理消息。

您的Foundation#importExchange应该接受邮件正文(在您的情况下为byte[]) 并返回适合回复邮件正文的内容。

replyHandler只需实施MessageListener

框架将为您提供相关内容。

答案 1 :(得分:2)

或者您可以添加.setContentType="text/plain",转换器将能够为您转换为String。