Skip to content

Commit e37e9aa

Browse files
committed
Fix new Sonar smells in RepublishMessageRecoverer
1 parent 67bba39 commit e37e9aa

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/retry/RepublishMessageRecoverer.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
*/
5050
public class RepublishMessageRecoverer implements MessageRecoverer {
5151

52-
private static final int ELIPSIS_LENGTH = 3;
52+
private static final int ELLIPSIS_LENGTH = 3;
5353

5454
public static final String X_EXCEPTION_STACKTRACE = "x-exception-stacktrace";
5555

@@ -61,7 +61,7 @@ public class RepublishMessageRecoverer implements MessageRecoverer {
6161

6262
public static final int DEFAULT_FRAME_MAX_HEADROOM = 20_000;
6363

64-
private static final int MAX_EXCEPTION_MESSAGE_SIZE_IN_TRACE = 100 - ELIPSIS_LENGTH;
64+
private static final int MAX_EXCEPTION_MESSAGE_SIZE_IN_TRACE = 100 - ELLIPSIS_LENGTH;
6565

6666
protected final Log logger = LogFactory.getLog(getClass()); // NOSONAR
6767

@@ -165,7 +165,7 @@ public void recover(Message message, Throwable cause) {
165165
headers.put(X_EXCEPTION_MESSAGE, exceptionMessage);
166166
headers.put(X_ORIGINAL_EXCHANGE, messageProperties.getReceivedExchange());
167167
headers.put(X_ORIGINAL_ROUTING_KEY, messageProperties.getReceivedRoutingKey());
168-
Map<? extends String, ? extends Object> additionalHeaders = additionalHeaders(message, cause);
168+
Map<? extends String, ?> additionalHeaders = additionalHeaders(message, cause);
169169
if (additionalHeaders != null) {
170170
headers.putAll(additionalHeaders);
171171
}
@@ -207,31 +207,31 @@ private String[] processStackTrace(Throwable cause, String exceptionMessage) {
207207
String truncatedExceptionMessage = exceptionMessage.length() <= MAX_EXCEPTION_MESSAGE_SIZE_IN_TRACE
208208
? exceptionMessage
209209
: (exceptionMessage.substring(0, MAX_EXCEPTION_MESSAGE_SIZE_IN_TRACE) + "...");
210-
if (this.maxStackTraceLength > 0) {
211-
if (stackTraceAsString.length() + exceptionMessage.length() > this.maxStackTraceLength) {
212-
if (!exceptionMessage.equals(truncatedExceptionMessage)) {
213-
int start = stackTraceAsString.indexOf(exceptionMessage);
214-
stackTraceAsString = stackTraceAsString.substring(0, start)
215-
+ truncatedExceptionMessage
216-
+ stackTraceAsString.substring(start + exceptionMessage.length());
210+
if (this.maxStackTraceLength > 0 &&
211+
stackTraceAsString.length() + exceptionMessage.length() > this.maxStackTraceLength) {
212+
213+
if (!exceptionMessage.equals(truncatedExceptionMessage)) {
214+
int start = stackTraceAsString.indexOf(exceptionMessage);
215+
stackTraceAsString = stackTraceAsString.substring(0, start)
216+
+ truncatedExceptionMessage
217+
+ stackTraceAsString.substring(start + exceptionMessage.length());
218+
}
219+
int adjustedStackTraceLen = this.maxStackTraceLength - truncatedExceptionMessage.length();
220+
if (adjustedStackTraceLen > 0) {
221+
if (stackTraceAsString.length() > adjustedStackTraceLen) {
222+
stackTraceAsString = stackTraceAsString.substring(0, adjustedStackTraceLen);
223+
this.logger.warn("Stack trace in republished message header truncated due to frame_max "
224+
+ "limitations; "
225+
+ "consider increasing frame_max on the broker or reduce the stack trace depth", cause);
226+
truncated = true;
217227
}
218-
int adjustedStackTraceLen = this.maxStackTraceLength - truncatedExceptionMessage.length();
219-
if (adjustedStackTraceLen > 0) {
220-
if (stackTraceAsString.length() > adjustedStackTraceLen) {
221-
stackTraceAsString = stackTraceAsString.substring(0, adjustedStackTraceLen);
222-
this.logger.warn("Stack trace in republished message header truncated due to frame_max "
223-
+ "limitations; "
224-
+ "consider increasing frame_max on the broker or reduce the stack trace depth", cause);
225-
truncated = true;
226-
}
227-
else if (stackTraceAsString.length() + exceptionMessage.length() > this.maxStackTraceLength) {
228-
this.logger.warn("Exception message in republished message header truncated due to frame_max "
229-
+ "limitations; consider increasing frame_max on the broker or reduce the exception "
230-
+ "message size", cause);
231-
truncatedExceptionMessage = exceptionMessage.substring(0,
232-
this.maxStackTraceLength - stackTraceAsString.length() - ELIPSIS_LENGTH) + "...";
233-
truncated = true;
234-
}
228+
else if (stackTraceAsString.length() + exceptionMessage.length() > this.maxStackTraceLength) {
229+
this.logger.warn("Exception message in republished message header truncated due to frame_max "
230+
+ "limitations; consider increasing frame_max on the broker or reduce the exception "
231+
+ "message size", cause);
232+
truncatedExceptionMessage = exceptionMessage.substring(0,
233+
this.maxStackTraceLength - stackTraceAsString.length() - ELLIPSIS_LENGTH) + "...";
234+
truncated = true;
235235
}
236236
}
237237
}
@@ -244,7 +244,7 @@ else if (stackTraceAsString.length() + exceptionMessage.length() > this.maxStack
244244
* @param cause The cause.
245245
* @return A {@link Map} of additional headers to add.
246246
*/
247-
protected Map<? extends String, ? extends Object> additionalHeaders(Message message, Throwable cause) {
247+
protected Map<? extends String, ?> additionalHeaders(Message message, Throwable cause) {
248248
return null;
249249
}
250250

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/retry/RepublishMessageRecovererIntegrationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
*
3939
*/
4040
@RabbitAvailable(queues = RepublishMessageRecovererIntegrationTests.BIG_HEADER_QUEUE)
41-
public class RepublishMessageRecovererIntegrationTests {
41+
class RepublishMessageRecovererIntegrationTests {
4242

43-
public static final String BIG_HEADER_QUEUE = "big.header.queue";
43+
static final String BIG_HEADER_QUEUE = "big.header.queue";
4444

4545
private static final String BIG_EXCEPTION_MESSAGE1 = new String(new byte[10_000]).replace("\u0000", "x");
4646

@@ -49,7 +49,7 @@ public class RepublishMessageRecovererIntegrationTests {
4949
private int maxHeaderSize;
5050

5151
@Test
52-
public void testBigHeader() {
52+
void testBigHeader() {
5353
CachingConnectionFactory ccf = new CachingConnectionFactory(
5454
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
5555
RabbitTemplate template = new RabbitTemplate(ccf);
@@ -74,7 +74,7 @@ public void testBigHeader() {
7474
}
7575

7676
@Test
77-
public void testSmallException() {
77+
void testSmallException() {
7878
CachingConnectionFactory ccf = new CachingConnectionFactory(
7979
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
8080
RabbitTemplate template = new RabbitTemplate(ccf);
@@ -95,7 +95,7 @@ public void testSmallException() {
9595
}
9696

9797
@Test
98-
public void testBigMessageSmallTrace() {
98+
void testBigMessageSmallTrace() {
9999
CachingConnectionFactory ccf = new CachingConnectionFactory(
100100
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
101101
RabbitTemplate template = new RabbitTemplate(ccf);

0 commit comments

Comments
 (0)