Search in sources :

Example 1 with TcpConnection

use of org.springframework.integration.ip.tcp.connection.TcpConnection in project spring-integration by spring-projects.

the class TcpSendingMessageHandler method doWrite.

/**
 * Method that actually does the write.
 * @param message The message to write.
 * @return the connection.
 */
protected TcpConnection doWrite(Message<?> message) {
    TcpConnection connection = null;
    try {
        connection = obtainConnection(message);
        if (logger.isDebugEnabled()) {
            logger.debug("Got Connection " + connection.getConnectionId());
        }
        connection.send(message);
    } catch (Exception e) {
        String connectionId = null;
        if (connection != null) {
            connectionId = connection.getConnectionId();
        }
        if (e instanceof MessageHandlingException) {
            throw (MessageHandlingException) e;
        }
        throw new MessageHandlingException(message, "Failed to handle message using " + connectionId, e);
    }
    return connection;
}
Also used : TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 2 with TcpConnection

use of org.springframework.integration.ip.tcp.connection.TcpConnection in project spring-integration by spring-projects.

the class TcpSendingMessageHandler method handleMessageInternal.

/**
 * Writes the message payload to the underlying socket, using the specified
 * message format.
 * @see org.springframework.messaging.MessageHandler#handleMessage(org.springframework.messaging.Message)
 */
@Override
public void handleMessageInternal(final Message<?> message) throws MessageHandlingException {
    if (this.serverConnectionFactory != null) {
        // We don't own the connection, we are asynchronously replying
        Object connectionId = message.getHeaders().get(IpHeaders.CONNECTION_ID);
        TcpConnection connection = null;
        if (connectionId != null) {
            connection = this.connections.get(connectionId);
        }
        if (connection != null) {
            try {
                connection.send(message);
            } catch (Exception e) {
                logger.error("Error sending message", e);
                connection.close();
                if (e instanceof MessageHandlingException) {
                    throw (MessageHandlingException) e;
                } else {
                    throw new MessageHandlingException(message, "Error sending message", e);
                }
            } finally {
                if (this.isSingleUse) {
                    // close after replying
                    connection.close();
                }
            }
        } else {
            logger.error("Unable to find outbound socket for " + message);
            MessageHandlingException messageHandlingException = new MessageHandlingException(message, "Unable to find outbound socket");
            publishNoConnectionEvent(messageHandlingException, (String) connectionId);
            throw messageHandlingException;
        }
        return;
    } else {
        // we own the connection
        TcpConnection connection = null;
        try {
            connection = doWrite(message);
        } catch (MessageHandlingException e) {
            // retry - socket may have closed
            if (e.getCause() instanceof IOException) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Fail on first write attempt", e);
                }
                connection = doWrite(message);
            } else {
                throw e;
            }
        } finally {
            if (connection != null && this.isSingleUse && this.clientConnectionFactory.getListener() == null) {
                // if there's no collaborating inbound adapter, close immediately, otherwise
                // it will close after receiving the reply.
                connection.close();
            }
        }
    }
}
Also used : TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) IOException(java.io.IOException) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 3 with TcpConnection

use of org.springframework.integration.ip.tcp.connection.TcpConnection in project spring-integration by spring-projects.

the class ConnectionToConnectionTests method testConnectGuts.

@SuppressWarnings("unchecked")
private void testConnectGuts(AbstractClientConnectionFactory client, AbstractServerConnectionFactory server, String gatewayName, boolean expectExceptionOnClose) throws Exception {
    TestingUtilities.waitListening(server, null);
    client.setPort(server.getPort());
    client.start();
    for (int i = 0; i < 100; i++) {
        TcpConnection connection = client.getConnection();
        connection.send(MessageBuilder.withPayload("Test").build());
        Message<?> message = serverSideChannel.receive(10000);
        assertNotNull(message);
        MessageHistory history = MessageHistory.read(message);
        // org.springframework.integration.test.util.TestUtils
        Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, gatewayName, 0);
        assertNotNull(componentHistoryRecord);
        assertTrue(componentHistoryRecord.get("type").equals("ip:tcp-inbound-gateway"));
        assertNotNull(message);
        assertEquals("Test", new String((byte[]) message.getPayload()));
    }
    int clientOpens = 0;
    int clientCloses = 0;
    int serverOpens = 0;
    int serverCloses = 0;
    int clientExceptions = 0;
    Message<TcpConnectionEvent> eventMessage;
    int i = 0;
    while (i++ < (expectExceptionOnClose ? 600 : 400) && (eventMessage = (Message<TcpConnectionEvent>) events.receive(10000)) != null) {
        TcpConnectionEvent event = eventMessage.getPayload();
        if (event.getConnectionFactoryName().startsWith("client")) {
            if (event instanceof TcpConnectionOpenEvent) {
                clientOpens++;
            } else if (event instanceof TcpConnectionCloseEvent) {
                clientCloses++;
            } else if (event instanceof TcpConnectionExceptionEvent) {
                clientExceptions++;
            }
        } else if (event.getConnectionFactoryName().startsWith("server")) {
            if (event instanceof TcpConnectionOpenEvent) {
                serverOpens++;
            } else if (event instanceof TcpConnectionCloseEvent) {
                serverCloses++;
            }
        }
    }
    assertEquals(100, clientOpens);
    assertEquals(100, clientCloses);
    if (expectExceptionOnClose) {
        assertEquals(100, clientExceptions);
    }
    assertEquals(100, serverOpens);
    assertEquals(100, serverCloses);
}
Also used : MessageHistory(org.springframework.integration.history.MessageHistory) TcpConnectionCloseEvent(org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent) TcpConnectionOpenEvent(org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent) Message(org.springframework.messaging.Message) TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) Properties(java.util.Properties) TcpConnectionEvent(org.springframework.integration.ip.tcp.connection.TcpConnectionEvent) TcpConnectionExceptionEvent(org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent)

Example 4 with TcpConnection

use of org.springframework.integration.ip.tcp.connection.TcpConnection in project spring-integration by spring-projects.

the class ConnectionToConnectionTests method testConnectRaw.

@Test
public void testConnectRaw() throws Exception {
    ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
    clientNet.setSerializer(serializer);
    serverNet.setDeserializer(serializer);
    clientNet.start();
    TcpConnection connection = clientNet.getConnection();
    connection.send(MessageBuilder.withPayload("Test").build());
    connection.close();
    Message<?> message = serverSideChannel.receive(10000);
    assertNotNull(message);
    MessageHistory history = MessageHistory.read(message);
    // org.springframework.integration.test.util.TestUtils
    Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "gwNet", 0);
    assertNotNull(componentHistoryRecord);
    assertTrue(componentHistoryRecord.get("type").equals("ip:tcp-inbound-gateway"));
    assertNotNull(message);
    assertEquals("Test", new String((byte[]) message.getPayload()));
}
Also used : MessageHistory(org.springframework.integration.history.MessageHistory) ByteArrayRawSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer) TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with TcpConnection

use of org.springframework.integration.ip.tcp.connection.TcpConnection in project spring-integration by spring-projects.

the class ConnectionToConnectionTests method testLookup.

@Test
public void testLookup() throws Exception {
    clientNet.start();
    TcpConnection connection = clientNet.getConnection();
    assertFalse(connection.getConnectionId().contains("localhost"));
    connection.close();
    clientNet.setLookupHost(true);
    connection = clientNet.getConnection();
    assertTrue(connection.getConnectionId().contains("localhost"));
    connection.close();
    clientNet.setLookupHost(false);
    connection = clientNet.getConnection();
    assertFalse(connection.getConnectionId().contains("localhost"));
    connection.close();
}
Also used : TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) Test(org.junit.Test)

Aggregations

TcpConnection (org.springframework.integration.ip.tcp.connection.TcpConnection)9 IOException (java.io.IOException)3 Test (org.junit.Test)3 MessageHandlingException (org.springframework.messaging.MessageHandlingException)3 Properties (java.util.Properties)2 MessageHistory (org.springframework.integration.history.MessageHistory)2 TcpConnectionCloseEvent (org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent)2 MessagingException (org.springframework.messaging.MessagingException)2 MessageTimeoutException (org.springframework.integration.MessageTimeoutException)1 TcpConnectionEvent (org.springframework.integration.ip.tcp.connection.TcpConnectionEvent)1 TcpConnectionExceptionEvent (org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent)1 TcpConnectionOpenEvent (org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent)1 ByteArrayRawSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer)1 Message (org.springframework.messaging.Message)1