0

I have a Spring Boot application using RabbitMQ. I can send the messages to the queue successfully. I can also retrieve the messages. However, when I retrieve them, the message is not removed from the queue. This results in my receive continually getting the same list of messages over and over.

Question

Can I remove the message once received?

Code

Receiver @Component

public class RabbitMQReceiver {

    @Autowired
    private PhotoStatusProcessorService photoStatusProcessorService;

    @RabbitListener(queues = "${rabbitmq.queuename}")
    public void receive(UUID in) {
        System.out.println("Received RabbitMQ msg = " + in);
        photoStatusProcessorService.processPhotoForId(in);
    }
}

Configuration

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.queuename}")
    String queueName;

//  @Value("${rabbitmq.exchangename}")
//  String exchange;
//
//  @Value("${rabbitmq.routingkeyname}")
//  String routingkey;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

//  @Bean
//  DirectExchange exchange() {
//      return new DirectExchange(exchange);
//  }
//
//  @Bean
//  Binding binding(Queue queue, DirectExchange exchange) {
//      return BindingBuilder.bind(queue).to(exchange).with(routingkey);
//  }

    @Profile("receiver")
    @Bean
    public RabbitMQReceiver receiver() {
        return new RabbitMQReceiver();
    }
}

application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=rabbitmq
spring.rabbitmq.password=****
rabbitmq.exchangename=photo-proccessor.exchange
rabbitmq.queuename=photo-proccessor
rabbitmq.routingkeyname=photo-proccessor.routingkey

YML File

  rabbitmq:
    container_name: rabbitmq
    image: rabbitmq:3.7-management-alpine
    ports:
      - 15672:15672
      - 5672:5672
    environment:
      RABBITMQ_ERLANG_COOKIE: "secretcookie"
      RABBITMQ_DEFAULT_USER: ${AMQP_USERNAME}
      RABBITMQ_DEFAULT_PASS: ${AMQP_PASSWORD}

Output

Received RabbitMQ msg = 9e24ec47-620b-4b93-833c-834a54d98ff8
Received RabbitMQ msg = 7e0e9aa6-f1d0-4154-abe1-617faa64a5fe
Received RabbitMQ msg = 3ade07c3-5f45-4aac-8054-0132bc85880b
Received RabbitMQ msg = 33645c35-a001-4c2c-a6f4-e3b96d3dc974
Received RabbitMQ msg = dcebdc77-6ddc-468e-978e-3714314a1abc
Received RabbitMQ msg = b7428adb-6356-4fd6-9ed1-c298886d95e8
Received RabbitMQ msg = 9e24ec47-620b-4b93-833c-834a54d98ff8
Received RabbitMQ msg = 7e0e9aa6-f1d0-4154-abe1-617faa64a5fe
Received RabbitMQ msg = 3ade07c3-5f45-4aac-8054-0132bc85880b
Received RabbitMQ msg = 33645c35-a001-4c2c-a6f4-e3b96d3dc974
Received RabbitMQ msg = dcebdc77-6ddc-468e-978e-3714314a1abc
Received RabbitMQ msg = b7428adb-6356-4fd6-9ed1-c298886d95e8
Received RabbitMQ msg = 9e24ec47-620b-4b93-833c-834a54d98ff8
Received RabbitMQ msg = 7e0e9aa6-f1d0-4154-abe1-617faa64a5fe
Received RabbitMQ msg = 3ade07c3-5f45-4aac-8054-0132bc85880b
Received RabbitMQ msg = 33645c35-a001-4c2c-a6f4-e3b96d3dc974
Received RabbitMQ msg = dcebdc77-6ddc-468e-978e-3714314a1abc
Received RabbitMQ msg = b7428adb-6356-4fd6-9ed1-c298886d95e8
Received RabbitMQ msg = 9e24ec47-620b-4b93-833c-834a54d98ff8
Received RabbitMQ msg = 7e0e9aa6-f1d0-4154-abe1-617faa64a5fe
Received RabbitMQ msg = 3ade07c3-5f45-4aac-8054-0132bc85880b
Received RabbitMQ msg = 33645c35-a001-4c2c-a6f4-e3b96d3dc974
Received RabbitMQ msg = dcebdc77-6ddc-468e-978e-3714314a1abc
..... forever
Richard
  • 8,193
  • 28
  • 107
  • 228
  • 1
    I am not using Spring to access RabbitMQ but usually you have to ACK the message. –  Apr 28 '19 at 14:43
  • can you share application.properties (or yaml) file – priyank-sriv Apr 28 '19 at 14:45
  • @Sindbad90 Thanks for the reply - added the YML file to the description above. – Richard Apr 28 '19 at 14:47
  • @LutzHorn thanks for the reply. I will investigate how to ACK in Spring. – Richard Apr 28 '19 at 14:48
  • Possible duplicate of [Spring RabbitMQ - using manual channel acknowledgement on a service with @RabbitListener configuration](https://stackoverflow.com/questions/38728668/spring-rabbitmq-using-manual-channel-acknowledgement-on-a-service-with-rabbit) –  Apr 28 '19 at 14:48
  • Pls have a look at this: https://stackoverflow.com/questions/38728668/spring-rabbitmq-using-manual-channel-acknowledgement-on-a-service-with-rabbit – priyank-sriv Apr 28 '19 at 14:58
  • The examples in the above link is for acknowledge-mode=manual. Does anyone know how to make it auto? – Richard Apr 28 '19 at 15:05
  • Auto is the default. There is no way a record will be redlivered unless the channel is somehow closed with unacked messages. Are you sure the producer is not resending? – Gary Russell Apr 28 '19 at 15:27
  • @Richard you can use the `acknowledgeMode = AcknowledgeMode.AUTO` – Abdelghani Roussi Apr 28 '19 at 15:53

0 Answers0