spring-cloud-streamspring-rabbitspring-reactivespring-cloud-function

Spring Cloud Function and Dead Letter Exchange


I am trying to write a reactive Spring Cloud Function service using RabbitMQ which will consume off one queue and produce to an exchange.

I have 2 questions.

  1. Why am I getting the error below in the logs.
  2. How would I do a reject with a doOnError? The doOnError has access to only the throwable, and not the message to do a reject.

Here is the application code. It is copied from this question Spring Reactive Stream - Unexpected Shutdown

@SpringBootApplication
public class StreamApplication {

  public static void main(String[] args) {
    SpringApplication.runStreamApplication args);
  }

  @Bean
  public Function<Flux<Message<String>>, Flux<Message<String>>> transform() {

    return inbound -> inbound.map(msg -> {
      try {
        System.out.println("ACKING MESSAGE");
        Channel channel = msg.getHeaders().get(AmqpHeaders.CHANNEL, Channel.class);
        channel.basicAck(msg.getHeaders().get(AmqpHeaders.DELIVERY_TAG, Long.class), false);
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      return msg;
    });
  }
}

Here is the application.yml. It has 2 different binders for the incoming from queue, and outgoing to exchange.

spring:
  cloud:
    stream:
      override-cloud-connectors: true
      rabbit:
        bindings:
          events-processor:
            producer:
              bindQueue: false
              declareExchange: false
              routing-key-expression: headers.eventType
          events:
            consumer:
              acknowledge-mode: MANUAL   
              prefetch: 10
              #auto-bind-dlq: false
              dead-letter-exchange: dead-letter-exchange
              bindQueue: false
              declareExchange: false
      function:
        definition: transform
        bindings:
          transform-in-0: events
          transform-out-0: events-processor
      bindings:
        events:
          destination: queue
          binder: consumerrabbit
          group: events-processor
        events-processor:
          destination: activity-events
          binder: producerrabbit
      binders:
        producerrabbit:
          defaultCandidate: false
          inheritEnvironment: false
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: host
                port: port
                username: username
                password: password
                virtual-host: virtual-host
        consumerrabbit:
          defaultCandidate: true
          inheritEnvironment: false
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: host
                port: port
                username: username
                password: password
                virtual-host: virtual-host

Here are the logs for the startup and for when the application receives an event. I am not sure why during startup the channel has a subscriber, but as soon as it gets a message it says it has no subscribers.

2020-09-07 12:02:04.076  INFO 10652 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'application-1.events' has 1 subscriber(s).
2020-09-07 18:02:08.848  INFO 10652 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'application-1.events-processor' has 1 subscriber(s).
2020-09-07 12:02:05.081  INFO 10652 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel events
2020-09-07 12:02:05.145  INFO 10652 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel errorChannel
2020-09-07 12:02:05.178  INFO 10652 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel events-processor
...................other startup log messages...............
2020-09-07 18:05:09.896  INFO 10652 --- [nts-processor-1] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'application-1.events' has 0 subscriber(s).
ACKING MESSAGE
2020-09-07 18:05:12.900 ERROR 10652 --- [nts-processor-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application-1.events'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload={
  "data": {
  
  }
}, headers={amqp_receivedDeliveryMode=NON_PERSISTENT, amqp_receivedExchange=source, amqp_deliveryTag=1, deliveryAttempt=3, amqp_consumerQueue=source.events-processor, amqp_channel=Cached Rabbit Channel: AMQChannel(amqp:), conn: Proxy@43e869ea Shared Rabbit Connection: SimpleConnection@213451a4 [delegate=amqp:], amqp_redelivered=false, id=35cdfa7f-7f75-5502-aede-6ec9569145f0, amqp_consumerTag=amq.ctag-Z87p6nKN4PLJKDbZQpXnVw, sourceData=(Body:'[B@20dc391b(byte[401])' MessageProperties [headers={}, contentLength=0, receivedDeliveryMode=NON_PERSISTENT, redelivered=false, receivedExchange=source, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-Z87p6nKN4PLJKDbZQpXnVw, consumerQueue=source.events-processor]), contentType=application/json, timestamp=1599501912897}], failedMessage=GenericMessage [payload={
  "data": {
  
  }
}, headers={amqp_receivedDeliveryMode=NON_PERSISTENT, amqp_receivedExchange=source, amqp_deliveryTag=1, deliveryAttempt=3, amqp_consumerQueue=source.events-processor, amqp_channel=Cached Rabbit Channel: AMQChannel(amqp://H), conn: Proxy@43e869ea Shared Rabbit Connection: SimpleConnection@213451a4 [delegate=amqp://], amqp_redelivered=false, id=35cdfa7f-7f75-5502-aede-6ec9569145f0, amqp_consumerTag=amq.ctag-Z87p6nKN4PLJKDbZQpXnVw, sourceData=(Body:'[B@20dc391b(byte[401])' MessageProperties [headers={}, contentLength=0, receivedDeliveryMode=NON_PERSISTENT, redelivered=false, receivedExchange=source, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-Z87p6nKN4PLJKDbZQpXnVw, consumerQueue=source.events-processor]), contentType=application/json, timestamp=1599501912897}]

Solution

  • Figured out how to have a Function send messages to a DLQ when failed. I added a Consumer also since they are related.

    I believe we need to ack or reject the message, but when rejecting we want to return a Flux.empty() so nothing gets published to the downstream exchange.

    Code rejects any message with fail as payload, and acks any other message.

    @SpringBootApplication
    public class DemoApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
      }
    
      @Bean
      public Function<Flux<Message<String>>, Flux<Message<String>>> transform() {
        return inbound -> inbound.flatMap(msg -> {
          try {
            Channel channel = msg.getHeaders().get(AmqpHeaders.CHANNEL, Channel.class);
            Long deliveryTag = msg.getHeaders().get(AmqpHeaders.DELIVERY_TAG, Long.class);
    
            if (msg.getPayload().startsWith("fail")) {
              channel.basicReject(deliveryTag, false);
            } else {
              channel.basicAck(deliveryTag, false);
              return Flux.just(msg);
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
    
          return Flux.empty();
        });
      }
    
      @Bean
      public Consumer<Flux<Message<String>>> consumer() {
        return inbound -> inbound
            .map(msg -> {
              try {
                Channel channel = msg.getHeaders().get(AmqpHeaders.CHANNEL, Channel.class);
                Long deliveryTag = msg.getHeaders().get(AmqpHeaders.DELIVERY_TAG, Long.class);
    
                if (msg.getPayload().startsWith("fail")) {
                  channel.basicReject(deliveryTag, false);
                } else {
                  channel.basicAck(deliveryTag, false);
                }
              } catch (IOException e) {
                e.printStackTrace();
              }
              return msg.getPayload();
            })
            .subscribe(System.out::println);
      }
    }
    
    spring:
      profiles: consumer
      cloud:
        stream:
          function:
            definition: consumer
          bindings:
            consumer-in-0:
              destination: consumer
              group: queue
          rabbit:
            bindings:
              consumer-in-0:
                consumer:
                  acknowledge-mode: MANUAL
                  autoBindDlq: true
                  declareDlx: true
                  bindQueue: true
                  deadLetterExchange: dead-letter-exchange
                  prefetch: 10
    ---
    spring:
      profiles: processor
      cloud:
        stream:
          function:
            definition: transform
          bindings:
            transform-in-0:
              destination: processorIn
              group: queue
            transform-out-0:
              destination: processorOut
          rabbit:
            bindings:
              transform-in-0:
                consumer:
                  acknowledge-mode: MANUAL
                  autoBindDlq: true
                  declareDlx: true
                  bindQueue: true
                  deadLetterExchange: dead-letter-exchange
                  prefetch: 10
              transform-out-0:
                producer:
                  bindQueue: true
                  declareExchange: true