Search in sources :

Example 1 with NodeConnectionStatusRequestMessage

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);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NodeConnectionStatusRequestMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Example 2 with NodeConnectionStatusRequestMessage

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NodeConnectionStatusRequestMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 NodeConnectionStatusRequestMessage (org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1 ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)1 NodeConnectionStatusResponseMessage (org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage)1 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)1 Test (org.junit.Test)1