|
1 | 1 | /* |
2 | | - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2021 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.amqp.rabbit.retry; |
18 | 18 |
|
| 19 | +import java.util.function.Supplier; |
| 20 | + |
19 | 21 | import org.apache.commons.logging.Log; |
20 | 22 | import org.apache.commons.logging.LogFactory; |
21 | 23 |
|
22 | 24 | import org.springframework.amqp.AmqpRejectAndDontRequeueException; |
23 | 25 | import org.springframework.amqp.core.Message; |
24 | 26 | import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException; |
| 27 | +import org.springframework.util.Assert; |
25 | 28 |
|
26 | 29 | /** |
27 | 30 | * MessageRecover that causes the listener container to reject |
|
35 | 38 | */ |
36 | 39 | public class RejectAndDontRequeueRecoverer implements MessageRecoverer { |
37 | 40 |
|
38 | | - protected Log logger = LogFactory.getLog(RejectAndDontRequeueRecoverer.class); // NOSONAR protected |
| 41 | + private final Supplier<String> messageSupplier; |
| 42 | + |
| 43 | + protected final Log logger = LogFactory.getLog(getClass()); // NOSONAR protected |
| 44 | + |
| 45 | + /** |
| 46 | + * Construct an instance with the default exception message. |
| 47 | + */ |
| 48 | + public RejectAndDontRequeueRecoverer() { |
| 49 | + this(() -> "Retry Policy Exhausted"); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Construct an instance with the provided exception message. |
| 54 | + * @param message the message. |
| 55 | + * @since 2.3.7 |
| 56 | + */ |
| 57 | + public RejectAndDontRequeueRecoverer(String message) { |
| 58 | + this(() -> message); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Construct an instance with the provided exception message supplier. |
| 63 | + * @param messageSupplier the message supplier. |
| 64 | + * @since 2.3.7 |
| 65 | + */ |
| 66 | + public RejectAndDontRequeueRecoverer(Supplier<String> messageSupplier) { |
| 67 | + Assert.notNull(messageSupplier, "'messageSupplier' cannot be null"); |
| 68 | + this.messageSupplier = messageSupplier; |
| 69 | + } |
39 | 70 |
|
40 | 71 | @Override |
41 | 72 | public void recover(Message message, Throwable cause) { |
42 | 73 | if (this.logger.isWarnEnabled()) { |
43 | 74 | this.logger.warn("Retries exhausted for message " + message, cause); |
44 | 75 | } |
45 | | - throw new ListenerExecutionFailedException("Retry Policy Exhausted", |
| 76 | + throw new ListenerExecutionFailedException(this.messageSupplier.get(), |
46 | 77 | new AmqpRejectAndDontRequeueException(cause), message); |
47 | 78 | } |
48 | 79 |
|
|
0 commit comments