/** * Writes a response message to the underlying channel while ensuring that at most one * {@link ResponseStatusCode#isFinalResponse() final} response is written. * <p>The caller must make sure that the provided response status code matches the content of the message.</p> * <p>Note: this method should be used instead of writing to the channel directly when multiple threads * are expected to produce response messages concurrently.</p> * <p>Attempts to write more than one final response message will be ignored.</p> * @see #writeAndFlush(ResponseMessage) */ public void writeAndFlush(final ResponseStatusCode code, final Object responseMessage) { final boolean messageIsFinal = code.isFinalResponse(); if(finalResponseWritten.compareAndSet(false, messageIsFinal)) { context.getChannelHandlerContext().writeAndFlush(responseMessage); } else { final String logMessage = String.format("Another final response message was already written for request %s, ignoring response code: %s", context.getRequestMessage().getRequestId(), code); logger.warn(logMessage); } } }
/** * Writes a response message to the underlying channel while ensuring that at most one * {@link ResponseStatusCode#isFinalResponse() final} response is written. * <p>The caller must make sure that the provided response status code matches the content of the message.</p> * <p>Note: this method should be used instead of writing to the channel directly when multiple threads * are expected to produce response messages concurrently.</p> * <p>Attempts to write more than one final response message will be ignored.</p> * @see #writeAndFlush(ResponseMessage) */ public void writeAndFlush(final ResponseStatusCode code, final Object responseMessage) { final boolean messageIsFinal = code.isFinalResponse(); if(finalResponseWritten.compareAndSet(false, messageIsFinal)) { context.getChannelHandlerContext().writeAndFlush(responseMessage); } else { final String logMessage = String.format("Another final response message was already written for request %s, ignoring response code: %s", context.getRequestMessage().getRequestId(), code); logger.warn(logMessage); } } }