How to receive a protobuf message via RabbitMQ with Spring Integration?

Viewed 1650

I try to receive a protobuf message out of RabbitMQ using Spring Integration.

My integration flow:

public class FacadeIntegrationFlowAdapter extends IntegrationFlowAdapter {

  @SuppressWarnings("rawtypes")
  private final Facade facade;
  private final FacadeProperties facadeProperties;

  @SuppressWarnings("unchecked")
  @Override
  protected IntegrationFlowDefinition<?> buildFlow() {
    return from(facadeProperties.getQueueName())
        .handle(facade::getNewMessages);
  }
}

The getNewMessages method:

@Override
public ExchangeResponse getNewMessages(Message<ExchangeRequest> message) {
    ExchangeRequest request = message.getPayload();
    log.info("Receiving new message: " + request.toString());

This is how I send the message to the queue. It's so simple to make the test easy to follow.

  ExchangeRequest request = ExchangeRequest.newBuilder()
      .addAllAuthors(List.of("author1", "author2"))
      .addAllBooks(List.of("book1", "book2"))
      .build();

  ConnectionFactory connectionFactory = new ConnectionFactory();
  connectionFactory.setUsername("user");
  connectionFactory.setPassword("password");
  connectionFactory.setHost("localhost");
  connectionFactory.setPort(24130);

  try {
    Connection connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    var basicProperties = new AMQP.BasicProperties().builder()
        .contentType("application/x-protobuf")
        .type(request.getDescriptorForType().getFullName())
        .build();

    channel.basicPublish(
        "facade-exchange", "facade-routing-key", basicProperties, request.toByteArray());
  } catch (IOException e) {

Unfortunately, I keep getting the exception:

com.google.protobuf.InvalidProtocolBufferException: Type of the Any message does not match the given class.

However, when I change the getNewMessages method to the following, all seems fine.

  @Override
  public ExchangeResponse getNewMessages(Message message) {
    try {
      Any payload = (Any) message.getPayload();
      ByteString value = payload.getValue();
      ExchangeRequest request = ExchangeRequest.parseFrom(value);
      log.info("Receiving new message: " + request.toString());

Where do I make a mistake? Tx!

0 Answers
Related