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 : 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]]
錯誤並不總是存在 請幫助
我認爲這個故事完全相反 - '有效載荷值不能爲空'。這意味着爲listener容器配置的轉換不能正確地反序列化正文。看起來'RabbitTemplate'使用默認的'SimpleMessageConverter'並使用標準的JAva序列化對''MqMessage''進行序列化,但容器工廠基於其他一些轉換器,甚至可能會提到'mappingJackson2MessageConverter'。您絕對應該在消費者方面使用相同的轉換器 –