/** * sends a message view the given channel, using the given callbacks. * * @return true if the message was successfully sent or false when an error occurred and the error hanlding logic was activated * */ private boolean internalSendMessage(Channel targetChannel, BytesReference message, Runnable onRequestSent) throws IOException { boolean success; try { sendMessage(targetChannel, message, onRequestSent); success = true; } catch (IOException ex) { // passing exception handling to deal with this and raise disconnect events and decide the right logging level onException(targetChannel, ex); success = false; } return success; }
boolean success = false; try { sendMessage(channel, new BytesArray(e.getMessage().getBytes(StandardCharsets.UTF_8)), closeChannel); success = true; } finally {
/** * Sends back an error response to the caller via the given channel * @param nodeVersion the caller node version * @param channel the channel to send the response to * @param error the error to return * @param requestId the request ID this response replies to * @param action the action this response replies to */ public void sendErrorResponse(Version nodeVersion, Channel channel, final Exception error, final long requestId, final String action) throws IOException { try (BytesStreamOutput stream = new BytesStreamOutput()) { stream.setVersion(nodeVersion); RemoteTransportException tx = new RemoteTransportException( nodeName(), new InetSocketTransportAddress(getLocalAddress(channel)), action, error); threadPool.getThreadContext().writeTo(stream); stream.writeException(tx); byte status = 0; status = TransportStatus.setResponse(status); status = TransportStatus.setError(status); final BytesReference bytes = stream.bytes(); final BytesReference header = buildHeader(requestId, status, nodeVersion, bytes.length()); Runnable onRequestSent = () -> transportServiceAdapter.onResponseSent(requestId, action, error); sendMessage(channel, new CompositeBytesReference(header, bytes), onRequestSent); } }