0

I have generated Proto classes from Protobuf and I would like to exchange the bytes between two Spring Cloud microservices over RabbitMQ but I wasn't able to figure out what is the proper way of serializing/deserializing.

Producer

 @Bean
 private fun produce(): Message<ByteArray>? {
    val protoObj = protoObj {
        id = UUID.randomUUID().toString()
        name = "name"
    }

    return MessageBuilder.withPayload(protoObj.toByteArray())
        .setHeader("Content-Type", "application/x-brotobuf").build()
}

Consumer

@Bean
fun consume(): Consumer<ByteArray> = Consumer {
    val protoObj = ProtoObj.parseFrom(it)
    logger.info("Received: {}", protoObj)
}

Exception

org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.cloud.stream.function.FunctionConfiguration$FunctionToDestinationBinder$1@7d801f4c]; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length., failedMessage=GenericMessage [payload=byte[32], headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedExchange=my_exchange, amqp_deliveryTag=1, deliveryAttempt=3, amqp_consumerQueue=my_exchange.my_queue, amqp_redelivered=false, amqp_receivedRoutingKey=data.key, amqp_timestamp=Thu Dec 15 14:54:12 EET 2022, amqp_messageId=3abf6bf4-5716-70d7-982e-824248b86119, id=a28f1235-ff3b-7c48-33c3-dac864f49728, amqp_consumerTag=amq.ctag-66oI3hsHrZpWVJQDwaLQ0g, sourceData=(Body:'{"validUtf8":true,"empty":false}' MessageProperties [headers={Content-Type=application/x-brotobuf}, timestamp=Thu Dec 15 14:54:12 EET 2022, messageId=3abf6bf4-5716-70d7-982e-824248b86119, contentType=application/json, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=my_exchange, receivedRoutingKey=data.key, deliveryTag=1, consumerTag=amq.ctag-66oI3hsHrZpWVJQDwaLQ0g, consumerQueue=my_queue]), contentType=application/json, Content-Type=application/x-brotobuf, timestamp=1671108941161}]```
cksrc
  • 2,062
  • 3
  • 24
  • 39

0 Answers0