@@ -470,7 +470,7 @@ protected Message<?> sendAndReceiveMessage(Object object) {
470
470
return (Message <?>) doSendAndReceive (object , false );
471
471
}
472
472
473
- @ Nullable
473
+ @ Nullable // NOSONAR
474
474
private Object doSendAndReceive (Object object , boolean shouldConvert ) {
475
475
initializeIfNecessary ();
476
476
Assert .notNull (object , "request must not be null" );
@@ -481,8 +481,7 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
481
481
482
482
registerReplyMessageCorrelatorIfNecessary ();
483
483
484
- Object reply = null ;
485
- Throwable error = null ;
484
+ Object reply ;
486
485
Message <?> requestMessage = null ;
487
486
try {
488
487
if (this .countsEnabled ) {
@@ -491,37 +490,31 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
491
490
if (shouldConvert ) {
492
491
reply = this .messagingTemplate .convertSendAndReceive (channel , object , Object .class ,
493
492
this .historyWritingPostProcessor );
494
- if (reply instanceof Throwable ) {
495
- error = (Throwable ) reply ;
496
- }
497
493
}
498
494
else {
499
495
requestMessage = (object instanceof Message <?>)
500
496
? (Message <?>) object : this .requestMapper .toMessage (object );
501
497
Assert .state (requestMessage != null , () -> "request mapper resulted in no message for " + object );
502
498
requestMessage = this .historyWritingPostProcessor .postProcessMessage (requestMessage );
503
499
reply = this .messagingTemplate .sendAndReceive (channel , requestMessage );
504
- if (reply instanceof ErrorMessage ) {
505
- error = ((ErrorMessage ) reply ).getPayload ();
506
- }
507
500
}
501
+
508
502
if (reply == null && this .errorOnTimeout ) {
509
- if (object instanceof Message ) {
510
- error = new MessageTimeoutException ((Message <?>) object , "No reply received within timeout" );
511
- }
512
- else {
513
- error = new MessageTimeoutException ("No reply received within timeout" );
514
- }
503
+ throwMessageTimeoutException (object , "No reply received within timeout" );
515
504
}
516
505
}
517
506
catch (Exception ex ) {
518
507
if (logger .isDebugEnabled ()) {
519
508
logger .debug ("failure occurred in gateway sendAndReceive: " + ex .getMessage ());
520
509
}
521
- error = ex ;
510
+ reply = ex ;
522
511
}
523
512
524
- if (error != null ) {
513
+ if (reply instanceof Throwable || reply instanceof ErrorMessage ) {
514
+ Throwable error =
515
+ reply instanceof ErrorMessage
516
+ ? ((ErrorMessage ) reply ).getPayload ()
517
+ : (Throwable ) reply ;
525
518
return handleSendAndReceiveError (object , requestMessage , error , shouldConvert );
526
519
}
527
520
return reply ;
@@ -534,46 +527,54 @@ private Object handleSendAndReceiveError(Object object, @Nullable Message<?> req
534
527
MessageChannel errorChan = getErrorChannel ();
535
528
if (errorChan != null ) {
536
529
ErrorMessage errorMessage = buildErrorMessage (requestMessage , error );
537
- Message <?> errorFlowReply = null ;
538
- try {
539
- errorFlowReply = this .messagingTemplate .sendAndReceive (errorChan , errorMessage );
540
- }
541
- catch (Exception errorFlowFailure ) {
542
- throw new MessagingException (errorMessage , "failure occurred in error-handling flow" ,
543
- errorFlowFailure );
544
- }
545
- if (shouldConvert ) {
546
- Object result = (errorFlowReply != null ) ? errorFlowReply .getPayload () : null ;
547
- if (result instanceof Throwable ) {
548
- rethrow ((Throwable ) result , "error flow returned Exception" );
549
- }
550
- return result ;
551
- }
552
- if (errorFlowReply != null && errorFlowReply .getPayload () instanceof Throwable ) {
553
- rethrow ((Throwable ) errorFlowReply .getPayload (), "error flow returned an Error Message" );
554
- }
530
+ Message <?> errorFlowReply = sendErrorMessageAndReceive (errorChan , errorMessage );
555
531
if (errorFlowReply == null && this .errorOnTimeout ) {
556
- if (object instanceof Message ) {
557
- throw new MessageTimeoutException (( Message <?>) object ,
558
- "No reply received from error channel within timeout" );
559
- }
560
- else {
561
- throw new MessageTimeoutException ( "No reply received from error channel within timeout" );
562
- }
532
+ throwMessageTimeoutException (object , "No reply received from error channel within timeout" );
533
+ return null ; // unreachable
534
+ }
535
+ else {
536
+ return shouldConvert && errorFlowReply != null
537
+ ? errorFlowReply . getPayload ()
538
+ : errorFlowReply ;
563
539
}
564
- return errorFlowReply ;
565
540
}
566
541
else {
542
+ Throwable errorToReThrow = error ;
567
543
if (error instanceof MessagingException &&
568
544
requestMessage != null && requestMessage .getHeaders ().getErrorChannel () != null ) {
569
545
// We are in nested flow where upstream expects errors in its own errorChannel header.
570
- error = new MessageHandlingException (requestMessage , error );
546
+ errorToReThrow = new MessageHandlingException (requestMessage , error );
571
547
}
572
- rethrow (error , "gateway received checked Exception" );
548
+ rethrow (errorToReThrow , "gateway received checked Exception" );
573
549
return null ; // unreachable
574
550
}
575
551
}
576
552
553
+ @ Nullable
554
+ private Message <?> sendErrorMessageAndReceive (MessageChannel errorChan , ErrorMessage errorMessage ) {
555
+ Message <?> errorFlowReply ;
556
+ try {
557
+ errorFlowReply = this .messagingTemplate .sendAndReceive (errorChan , errorMessage );
558
+ }
559
+ catch (Exception errorFlowFailure ) {
560
+ throw new MessagingException (errorMessage , "failure occurred in error-handling flow" ,
561
+ errorFlowFailure );
562
+ }
563
+ if (errorFlowReply != null && errorFlowReply .getPayload () instanceof Throwable ) {
564
+ rethrow ((Throwable ) errorFlowReply .getPayload (), "error flow returned an Error Message" );
565
+ }
566
+ return errorFlowReply ;
567
+ }
568
+
569
+ private void throwMessageTimeoutException (Object object , String exceptionMessage ) {
570
+ if (object instanceof Message ) {
571
+ throw new MessageTimeoutException ((Message <?>) object , exceptionMessage );
572
+ }
573
+ else {
574
+ throw new MessageTimeoutException (exceptionMessage );
575
+ }
576
+ }
577
+
577
578
protected Mono <Message <?>> sendAndReceiveMessageReactive (Object object ) {
578
579
initializeIfNecessary ();
579
580
Assert .notNull (object , "request must not be null" );
0 commit comments