spring-projects / spring-amqp

Spring AMQP - support for Spring programming model with AMQP, especially but not limited to RabbitMQ

Home Page:https://spring.io/projects/spring-amqp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BatchingRabbitTemplate Overwrites Custom Headers of Batched Messages

umut-saribiyik88 opened this issue · comments

Description:

When sending multiple messages using the 'BatchingRabbitTemplate', each message has its own custom headers. However, the BatchingRabbitTemplate removes all custom headers from each message in the batch and instead applies the headers from the first message to all messages in the batch.

Steps to Reproduce:

Create multiple messages, each with distinct custom message headers.
Use BatchingRabbitTemplate to send these messages as a batch.
Observe the headers of each message in the batch.

Used following template:

@Bean
public BatchingRabbitTemplate batchingRabbitTemplate(ConnectionFactory connectionFactory) {
    BatchingStrategy strategy = new SimpleBatchingStrategy(20, 25_000, 10_000);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(1);
    scheduler.initialize();
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(strategy, scheduler);
    template.setConnectionFactory(connectionFactory);
    template.setMessageConverter(converter());
    return template;
}

Create Message with custom header and send (Example):

batchingRabbitTemplate.convertAndSend(exchange.getValue(), routingKey.getValue(), myObject, message -> {
    MessageProperties properties = message.getMessageProperties();
    properties.setPriority(priority);
    properties.setType("Sample-type");
    properties.setHeader("custom-header", "custom-value");
    return message;
});

Expected Behavior:

Each message should retain its original custom headers when sent in a batch.

Actual Behavior:

All messages in the batch receive the headers of the first message, losing their individual custom headers.

Environment:

  • Rabbit-amqp version: 3.0.13
  • Java Version: 17

Thank you very much!

See Javadocs:

/**
 * A simple batching strategy that supports only one exchange/routingKey; includes a batch
 * size, a batched message size limit and a timeout. The message properties from the first
 * message in the batch is used in the batch message. Each message is preceded by a 4 byte
 * length field.
 *
 * @author Gary Russell
 * @since 1.4.1
 *
 */
public class SimpleBatchingStrategy implements BatchingStrategy {

So, what you show is really expected.
Even if we do batching, still only a single AMQP message is emitted.
You might look into a custom BatchingStrategy which would batch those headers as well.

Hi @artembilan

Thank you for the clarification.