use of org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage in project nifi by apache.
the class StandardClusterCoordinationProtocolSender method requestNodeConnectionStatus.
@Override
public NodeConnectionStatus requestNodeConnectionStatus(final String hostname, final int port) {
Objects.requireNonNull(hostname);
final NodeConnectionStatusRequestMessage msg = new NodeConnectionStatusRequestMessage();
final byte[] msgBytes;
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
marshaller.marshal(msg, baos);
msgBytes = baos.toByteArray();
} catch (final IOException e) {
throw new ProtocolException("Failed to marshal NodeIdentifierRequestMessage", e);
}
try (final Socket socket = createSocket(hostname, port, true)) {
// marshal message to output stream
socket.getOutputStream().write(msgBytes);
final ProtocolMessage response;
try {
// unmarshall response and return
final ProtocolMessageUnmarshaller<ProtocolMessage> unmarshaller = protocolContext.createUnmarshaller();
response = unmarshaller.unmarshal(socket.getInputStream());
} catch (final IOException ioe) {
throw new ProtocolException("Failed unmarshalling '" + MessageType.RECONNECTION_RESPONSE + "' protocol message due to: " + ioe, ioe);
}
if (MessageType.NODE_CONNECTION_STATUS_RESPONSE == response.getType()) {
return ((NodeConnectionStatusResponseMessage) response).getNodeConnectionStatus();
} else {
throw new ProtocolException("Expected message type '" + MessageType.NODE_CONNECTION_STATUS_RESPONSE + "' but found '" + response.getType() + "'");
}
} catch (final IOException ioe) {
throw new ProtocolException("Failed to request Node Identifer from " + hostname + ":" + port, ioe);
}
}
use of org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage in project nifi by apache.
the class TestJaxbProtocolUtils method testRoundTripConnectionStatusRequest.
@Test
public void testRoundTripConnectionStatusRequest() throws JAXBException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final NodeConnectionStatusRequestMessage msg = new NodeConnectionStatusRequestMessage();
JaxbProtocolUtils.JAXB_CONTEXT.createMarshaller().marshal(msg, baos);
final Object unmarshalled = JaxbProtocolUtils.JAXB_CONTEXT.createUnmarshaller().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
assertTrue(unmarshalled instanceof NodeConnectionStatusRequestMessage);
}
Aggregations