Create listener using Spring Rabbit MQ to return publisher

I am trying to create a listener using spring-amqp (RabbitMQ) that supports publisher return. I couldn't find any example for this case, but I tried the following:

@Component
public class ImportModelHandler {

 @Autowired
 private ModelService modelService;

  public byte[] handleMessage(byte[] file) throws Exception {
try {
  if (file == null || file.length == 0) {
    throw new IllegalArgumentException("File is empty");
  }
  return ModelService.OBJECT_MAPPER.writeValueAsBytes(
      modelService.importModel(file));
} catch (Exception e) {
  return ModelService.OBJECT_MAPPER.writeValueAsBytes(Response.createErrorResponse(e));
}
 }

}

      

Xml config:

  <bean id="importModelListener"
        class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
    <constructor-arg ref="importModelHandler"/>
  </bean>

  <rabbit:connection-factory id="connectionFactory" host="192.168.1.50" port="5672" username="guest" password="guest"
                             publisher-returns="true"/>
  <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>
  <rabbit:admin connection-factory="connectionFactory"/>

  <rabbit:queue name="import.model"/>
  <rabbit:queue name="download.model"/>

  <bean id="rabbitListenerContainerFactory"
        class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="concurrentConsumers" value="3"/>
    <property name="maxConcurrentConsumers" value="10"/>
  </bean>
  <rabbit:annotation-driven container-factory="rabbitListenerContainerFactory"/>

  <rabbit:direct-exchange name="import.model.exchange">
    <rabbit:bindings>
      <rabbit:binding queue="import.model"/>
    </rabbit:bindings>
  </rabbit:direct-exchange>

  <rabbit:listener-container
      connection-factory="connectionFactory">
    <rabbit:listener ref="importModelListener" queue-names="import.model"/>
  </rabbit:listener-container>

      

Then I tried to create some code to call this listener:

URL resource = ImportModelTest.class.getResource("/models/model.jar");
File file = new File(resource.toURI());
byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
Message message = new Message(bytes, messageProperties);
rabbitTemplate.convertAndSend("import.model", message);
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
  @Override
  public void returnedMessage(Message message, int i, String s, String s1, String s2) {
    System.out.println(new String(message.getBody()));
  }
});

      

But I have:

org.springframework.amqp.AmqpException: Cannot determine the ReplyTo value of the message property: The request message does not contain a response property, and no default Exchange response has been set.

How can I create a listener that will use the Publisher Returns mechanism?

+3


source to share





All Articles