2
votes

I have bunch of files and in every file I have bunch of objects. I want to put all these objects into queue, but I want to be sure that all objects from single file are accepted, so I can remove it. I added @Transactional annotation to method where I publish messages:

public class Sender {

  @Autowired
  private RabbitTemplate template;

  @Autowired
  private Queue queue;

  public Sender(RabbitTemplate template) {
    this.template = template;
    template.setChannelTransacted(true);
  }


  @Transactional
  public void send(List<String> messages) {
    messages.forEach(msg -> template.convertAndSend(queue.getName(), msg));
  }

}

Everything works OK - rollback is performed when I throw an Exception somewhere in send(List<String> messages) method. But I also want to limit maximum size of queue, because I know that publisher publish messages much faster than consumer is able to consume them, so I added max-length parameter to the queue:

@Bean
  public Queue queue() {
    return new Queue("test", true, false, false, Map.of("x-max-length", 3, "overflow", "reject-publish"));
  }

Unfortunately transaction is committed even if max-length is exceeded.

Is it possible to rollback transaction when message is rejected because of max-length limit?

1

1 Answers

2
votes

The root cause was overflow parameter instead of x-overflow. The default overflow strategy is to delete oldest message, so everything is as expected.