Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Created June 8, 2017 17:58
Show Gist options
  • Save garyrussell/6dac9031feb9cfa73269d61b1c4f6f36 to your computer and use it in GitHub Desktop.
Save garyrussell/6dac9031feb9cfa73269d61b1c4f6f36 to your computer and use it in GitHub Desktop.
Spring Cloud Stream App That Consumes its own Dead Letters
spring.cloud.stream.bindings.input.destination=foo
spring.cloud.stream.bindings.input.group=foo
spring.cloud.stream.rabbit.bindings.input.consumer.autoBindDlq=true
spring.cloud.stream.rabbit.bindings.input.consumer.deadLetterRoutingKey=foo
spring.cloud.stream.rabbit.bindings.input.consumer.republishToDlq=true
spring.cloud.stream.bindings.errors.group=foo.dlq
spring.cloud.stream.bindings.errors.destination=foo
spring.cloud.stream.rabbit.bindings.errors.bindQueue=false
spring.cloud.stream.rabbit.bindings.errors.declareExchange=false
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.SubscribableChannel;
/**
* @author Gary Russell
*/
@SpringBootApplication
@EnableBinding(Gitter2Application.Foo.class)
public class Gitter2Application {
public static void main(String[] args) {
SpringApplication.run(Gitter2Application.class, args);
}
@StreamListener(Sink.INPUT)
public void listen(String in) {
throw new RuntimeException("fail");
}
@ServiceActivator(inputChannel = "errors")
public void handle(Message<?> failedMessage) {
System.out.println("Failed message " + failedMessage);
}
public interface Foo extends Sink {
@Input("errors")
SubscribableChannel errors();
}
}
@GADNT
Copy link

GADNT commented Jun 19, 2017

Hi Gary

thanks for this ..it's something that I meant to be in the first place but I observed that the serviceactivator are invoked two times ..one for input channel and one for the case when actually is needed to handle the error thrown.

Below you can find the logs when the message hits the channel and also the error was thrown:

2017-06-19 17:08:54.745 INFO 6640 --- [ main] c.g.junk.messaging.MessagingApplication : Started MessagingApplication in 11.024 seconds (JVM running for 11.852)
Failed message GenericMessage [payload=byte[7], headers={amqp_receivedDeliveryMode=NON_PERSISTENT, amqp_receivedExchange=foo, amqp_deliveryTag=1, amqp_consumerQueue=foo.foo.dlq, amqp _redelivered=false, id=fc4d2446-a23e-753f-b7ad-2007e2ce4fd2, amqp_consumerTag=amq.ctag-DAmbewkA_74PTHkn0Tr4dw, timestamp=1497881353500}]
2017-06-19 17:09:16.525 WARN 6640 --- [ foo.foo-1] o.s.a.r.retry.RepublishMessageRecoverer : Republishing failed message to exchange DLX
Failed message GenericMessage [payload=byte[7], headers={amqp_receivedRoutingKey=foo.foo, amqp_receivedExchange=DLX, x-exception-message=Exception thrown while invoking com.gad.junk. messaging.MessagingApplication#listen[1 args]; nested exception is java.lang.RuntimeException: fail, amqp_deliveryTag=2, x-original-routingKey=, amqp_consumerQueue=foo.foo.dlq, x-ori ginal-exchange=foo, amqp_redelivered=false, id=aeff6307-a89f-e59c-587c-9eaf8931410a, amqp_consumerTag=amq.ctag-DAmbewkA_74PTHkn0Tr4dw,
x-exception-stacktrace=org.springframework.amqp .rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapTo
No modification to your code was made ..just a new name for class and I am using Rabbitmq for messages.

Do you have any idea what is happening?

Thank you
Gabriel

@garyrussell
Copy link
Author

That doesn't happen for me.

It looks like you, somehow, have the dlq also bound to the foo exchange...

amqp_receivedExchange=foo,amqp_consumerQueue=foo.foo.dlq

In the first message. This will cause any message sent to the foo exchange will go to both the main queue and the DLQ.

The DLQ should only be bound to the DLX exchange.

@GADNT
Copy link

GADNT commented Jun 20, 2017

I think I have found the issue. I have adjusted this:
spring.cloud.stream.rabbit.bindings.errors.bindQueue=false spring.cloud.stream.rabbit.bindings.errors.declareExchange=false to:
spring.cloud.stream.rabbit.bindings.errors.consumer.bindQueue=false spring.cloud.stream.rabbit.bindings.errors.consumer.declareExchange=false .
Now I see a normal behavior , the DLQ handle just the error.
Also an important note is : I was using springboot 1.4.2.RELEASE and springCloudVersion = 'Camden.SR5'..it should be upgraded to springCloudVersion = 'Camden.SR7'.

Thank for your guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment