Search in sources :

Example 6 with ConnectionResponseMessage

use of org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage in project nifi by apache.

the class AbstractNodeProtocolSender method requestConnection.

@Override
public ConnectionResponseMessage requestConnection(final ConnectionRequestMessage msg) throws ProtocolException, UnknownServiceAddressException {
    Socket socket = null;
    try {
        socket = createSocket();
        try {
            // marshal message to output stream
            final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
            marshaller.marshal(msg, socket.getOutputStream());
        } catch (final IOException ioe) {
            throw new ProtocolException("Failed marshalling '" + msg.getType() + "' protocol message due to: " + ioe, ioe);
        }
        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.CONNECTION_RESPONSE + "' protocol message from " + socket.getRemoteSocketAddress() + " due to: " + ioe, ioe);
        }
        if (MessageType.CONNECTION_RESPONSE == response.getType()) {
            final ConnectionResponseMessage connectionResponse = (ConnectionResponseMessage) response;
            return connectionResponse;
        } else {
            throw new ProtocolException("Expected message type '" + MessageType.CONNECTION_RESPONSE + "' but found '" + response.getType() + "'");
        }
    } finally {
        SocketUtils.closeQuietly(socket);
    }
}
Also used : ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Example 7 with ConnectionResponseMessage

use of org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage in project nifi by apache.

the class TestJaxbProtocolUtils method testRoundTripConnectionResponse.

@Test
public void testRoundTripConnectionResponse() throws JAXBException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ConnectionResponseMessage msg = new ConnectionResponseMessage();
    final NodeIdentifier nodeId = new NodeIdentifier("id", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, true);
    final DataFlow dataFlow = new StandardDataFlow(new byte[0], new byte[0], new byte[0], new HashSet<>());
    final List<NodeConnectionStatus> nodeStatuses = Collections.singletonList(new NodeConnectionStatus(nodeId, DisconnectionCode.NOT_YET_CONNECTED));
    final List<ComponentRevision> componentRevisions = Collections.singletonList(ComponentRevision.fromRevision(new Revision(8L, "client-1", "component-1")));
    msg.setConnectionResponse(new ConnectionResponse(nodeId, dataFlow, "instance-1", nodeStatuses, componentRevisions));
    JaxbProtocolUtils.JAXB_CONTEXT.createMarshaller().marshal(msg, baos);
    final Object unmarshalled = JaxbProtocolUtils.JAXB_CONTEXT.createUnmarshaller().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
    assertTrue(unmarshalled instanceof ConnectionResponseMessage);
    final ConnectionResponseMessage unmarshalledMsg = (ConnectionResponseMessage) unmarshalled;
    final List<ComponentRevision> revisions = msg.getConnectionResponse().getComponentRevisions();
    assertEquals(1, revisions.size());
    assertEquals(8L, revisions.get(0).getVersion().longValue());
    assertEquals("client-1", revisions.get(0).getClientId());
    assertEquals("component-1", revisions.get(0).getComponentId());
    assertEquals(revisions, unmarshalledMsg.getConnectionResponse().getComponentRevisions());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) ComponentRevision(org.apache.nifi.cluster.protocol.ComponentRevision) Revision(org.apache.nifi.web.Revision) ComponentRevision(org.apache.nifi.cluster.protocol.ComponentRevision) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) Test(org.junit.Test)

Aggregations

ConnectionResponseMessage (org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage)7 ConnectionResponse (org.apache.nifi.cluster.protocol.ConnectionResponse)5 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)4 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)4 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 ConnectionRequest (org.apache.nifi.cluster.protocol.ConnectionRequest)3 ConnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage)3 IOException (java.io.IOException)2 DataFlow (org.apache.nifi.cluster.protocol.DataFlow)2 ClusterCoordinationProtocolSenderListener (org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener)2 EventReporter (org.apache.nifi.events.EventReporter)2 RevisionManager (org.apache.nifi.web.revision.RevisionManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1