2016-06-23 9 views
0

Ich versuche Nachricht zu senden und empfangen Antwortcode folgende VerwendungNicht in der Lage Antwort zu verarbeiten von template.convertSendAndReceive() empfangen

MessageProperties props =MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN) 
      .setMessageId("MSG12345").setHeader("type", "type1").setCorrelationId(UUID.randomUUID().toString().getBytes()).build(); 
Message message = MessageBuilder.withBody(input.getBytes()).andProperties(props).build(); 
Message response = (Message) template.convertSendAndReceive("key", message); 

Aber seine wirft ava.lang.ClassCastException: java.lang.String kann nicht zu org.springframework.amqp.core.Message

Mai sein, weil ich Anfrage mit java (spring-amqp) Programm senden und der Empfänger ist ein Python (Pika) -Programm. Recevier sendet mir ein JSON-Objekt im String-Format, aber ich bin nicht in der Lage, damit umzugehen.

Antwort

0

Ihr Problem, dass Sie RabbitTemplate.convertSendAndReceive() verwenden:

/** 
* Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a 
* specific routing key and attempt to receive a response, converting that to a Java object. Implementations will 
* normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
* 
* @param routingKey the routing key 
* @param message a message to send 
* @return the response if there is one 
* @throws AmqpException if there is a problem 
*/ 
Object convertSendAndReceive(String routingKey, Object message) throws AmqpException; 

Selbst wenn Ihr payload ist Message und wir haben wir:

protected Message convertMessageIfNecessary(final Object object) { 
    if (object instanceof Message) { 
     return (Message) object; 
    } 
    return getRequiredMessageConverter().toMessage(object, new MessageProperties()); 
} 

Es wandelt eine reply in ein Zielobjekt aus body:

return this.getRequiredMessageConverter().fromMessage(replyMessage); 

und geben Sie nicht wie erwartet Message zurück.

Also, Sie müssen wirklich auf String werfen und mit Ihrem JSON schon alleine fertig werden.