Search code examples
rabbitmqspring-cloud-stream

With Spring Cloud Stream RabbitMQ, is it possible to zip outbound and unzip inbound message


With Spring Cloud Stream RabbitMQ application, I am trying to compress (zip) the message before sending it by defining my own RabbitTemplate bean but it does not work.

Is there a way to do it by configuration (application.properties) or by another method ?


Solution

  • The binder configuration automatically wires in a GZipPostProcessor on the sending side and a DelegatingDecompressingPostProcessor on the consumer side.

    If you want to change it to a ZipPostProcessor, use a binder customizer to set it on the binder:

    /**
     * Set a {@link org.springframework.amqp.core.MessagePostProcessor} to compress
     * messages. Defaults to a
     * {@link org.springframework.amqp.support.postprocessor.GZipPostProcessor}.
     * @param compressingPostProcessor the post processor.
     */
    public void setCompressingPostProcessor(
            MessagePostProcessor compressingPostProcessor) {
    

    You have to set the producer binding property compress to true to enable compression on the sending side.

    On the receiving side, the DelegatingDecompressingPostProcessor looks at the content encoding message property to determine whether decompression is needed and which decompressor to use.

    EDIT

    I've just noticed that the binder sets the compression level to 0 (NO_COMPRESSION). Use spring.cloud.stream.rabbit.binder.compression-level=9 (BEST_COMPRESSION) or spring.cloud.stream.rabbit.binder.compression-level=1 (BEST_SPEED).

    See java.util.zip.Deflater for available levels.

    https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit/issues/353

    This only applies if using gzip.