Source:
<code>private Mono<Message<?>> doSendAndReceiveMessageReactive(MessageChannel requestChannel, Object object, boolean error) { Message<?> requestMessage = null; try { requestMessage = object instanceof Message<?> ? (Message<?>) object : this.requestMapper.toMessage(object); requestMessage = this.historyWritingPostProcessor.postProcessMessage(requestMessage); Object originalReplyChannelHeader = requestMessage.getHeaders().getReplyChannel(); Object originalErrorChannelHeader = requestMessage.getHeaders().getErrorChannel(); FutureReplyChannel replyChannel = new FutureReplyChannel(); requestMessage = MessageBuilder.fromMessage(requestMessage) .setReplyChannel(replyChannel) .setHeader(this.messagingTemplate.getSendTimeoutHeader(), null) .setHeader(this.messagingTemplate.getReceiveTimeoutHeader(), null) .setErrorChannel(replyChannel) .build(); Message<?> theRequestMessage = requestMessage; return Mono.just(theRequestMessage) .doOnSubscribe(s -> { if (!error && this.countsEnabled) { this.messageCount.incrementAndGet(); } }) <START> .flatMap(message -> { <END> long sendTimeout = sendTimeout(message); requestChannel.send(message, sendTimeout); boolean sent = sendTimeout >= 0 ? requestChannel.send(message, sendTimeout) : requestChannel.send(message); if (!sent) { return Mono.error( new MessageDeliveryException(message, "Failed to send message to channel '" + requestChannel + "' within timeout: " + sendTimeout)); } return Mono.fromFuture(replyChannel.messageFuture); }) .<Message<?>>map(replyMessage -> MessageBuilder.fromMessage(replyMessage) .setHeader(MessageHeaders.REPLY_CHANNEL, originalReplyChannelHeader) .setHeader(MessageHeaders.ERROR_CHANNEL, originalErrorChannelHeader) .build()) .onErrorResume(t -> handleSendError(theRequestMessage, t) ); } catch (Exception e) { return handleSendError(requestMessage, e); } }</code><technical_language>here. Mono.just + flatMap unnecessary. @smaldini</technical_language>

Target:
private Mono<Message<?>> doSendAndReceiveMessageReactive(MessageChannel requestChannel, Object object, boolean error) { return Mono.defer(() -> { Message<?> message; try { message = object instanceof Message<?> ? (Message<?>) object : this.requestMapper.toMessage(object); message = this.historyWritingPostProcessor.postProcessMessage(message); } catch (Exception e) { throw new MessageMappingException("Cannot map to message: " + object, e); } Object originalReplyChannelHeader = message.getHeaders().getReplyChannel(); Object originalErrorChannelHeader = message.getHeaders().getErrorChannel(); FutureReplyChannel replyChannel = new FutureReplyChannel(); Message<?> requestMessage = MutableMessageBuilder.fromMessage(message) .setReplyChannel(replyChannel) .setHeader(this.messagingTemplate.getSendTimeoutHeader(), null) .setHeader(this.messagingTemplate.getReceiveTimeoutHeader(), null) .setErrorChannel(replyChannel) .build(); if (requestChannel instanceof ReactiveStreamsSubscribableChannel) { ((ReactiveStreamsSubscribableChannel) requestChannel) .subscribeTo(Mono.just(requestMessage)); } else { long sendTimeout = sendTimeout(requestMessage); boolean sent = sendTimeout >= 0 ? requestChannel.send(requestMessage, sendTimeout) : requestChannel.send(requestMessage); if (!sent) { throw new MessageDeliveryException(requestMessage, "Failed to send message to channel '" + requestChannel + "' within timeout: " + sendTimeout); } } return Mono.fromFuture(replyChannel.messageFuture) .doOnSubscribe(s -> { if (!error && this.countsEnabled) { this.messageCount.incrementAndGet(); } }) .<Message<?>>map(replyMessage -> MessageBuilder.fromMessage(replyMessage) .setHeader(MessageHeaders.REPLY_CHANNEL, originalReplyChannelHeader) .setHeader(MessageHeaders.ERROR_CHANNEL, originalErrorChannelHeader) .build()) .onErrorResume(t -> error ? Mono.error(t) : handleSendError(requestMessage, t)); }); }