我有课程:
public class MqMessage implements Serializable{
private String event;
private String absolutePath;
private boolean isDirectory;
private Integer hash;
private Node node;
get/set
}
配置类: 公共类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());
}
发送消息
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) {
/*bussines-logick
*/
rabbitTemplate.setExchange("exchange-events");
rabbitTemplate.convertAndSend(new MqMessage(event,fileTreeRoot));
return;
}
public ServerHandler() {
}
监听器:
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();
}
只有错误启动应用
2017-08-08 12:58:02.128 WARN 5024 --- [cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler:执行Rabbit消息 听众失败了。
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: 无法使用传入消息
调用侦听器方法引起: org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException: 无法在public void中解析索引0处的方法参数 prcjt.client.Client.onMessage(prcjt.message.MqMessage):1个错误: [对象错误':代码[];参数[];默认消息 [有效载荷值不能为空]]
错误并不总是存在 请帮忙
答案 0 :(得分:1)
Caused by: org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException: Could not resolve method parameter at index 0
从异常信息来看,似乎spring无法正确解析侦听器中的MqMessage
,您可以尝试向客户端添加mappingJackson2MessageConverter
。请参阅this link。
答案 1 :(得分:0)
使用 mappingJackson2MessageConverted
对我不起作用。经过大量调查发现我使用了错误的转换器。在侦听器服务中添加以下代码,它对我有用。
@Bean
public Jackson2JsonMessageConverter converter() {
return new Jackson2JsonMessageConverter();
}