0
votes

Currently, in my project, I'm using spring cloud stream with rabbitmq. Everything is okay, but the problem is when in StreamListener I set a condition for the message type. If RabbitMQ doesn't match type (Cannot find a @StreamListener matching for a message with id: ZZZZZZ) then this message disappeared from the queue. I don't want to remove this message if the message has a bad type. Is some solution for this problem?

1

1 Answers

1
votes

There are two ways you can do it:

  • Throw an exception if the message has bad type. In this case, the message will not be removed from the queue since it wasn't processed successfully and no ACK was received.
  • Insert the message in the queue yourself once you have detected the type is bad.

Both approaches can suffer what is called an infinite loop. The message is processed, it is of bad type, it is re-inserted, and this repeats. To avoid this you can add some policy of reinserting, like: exponenetial delay, or a limited number of re-insertions etc.

But some doubt arises: why is your service consuming messages that it shouldn't? Perhaps you need a specific processor this messages? In this case you can route to the suitable processor.