Search in sources :

Example 6 with TcpConnection

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

the class TcpOutboundGateway method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    Assert.notNull(this.connectionFactory, this.getClass().getName() + " requires a client connection factory");
    boolean haveSemaphore = false;
    TcpConnection connection = null;
    String connectionId = null;
    try {
        if (!this.isSingleUse) {
            logger.debug("trying semaphore");
            if (!this.semaphore.tryAcquire(this.requestTimeout, TimeUnit.MILLISECONDS)) {
                throw new MessageTimeoutException(requestMessage, "Timed out waiting for connection");
            }
            haveSemaphore = true;
            if (logger.isDebugEnabled()) {
                logger.debug("got semaphore");
            }
        }
        connection = this.connectionFactory.getConnection();
        AsyncReply reply = new AsyncReply(this.remoteTimeoutExpression.getValue(this.evaluationContext, requestMessage, Long.class));
        connectionId = connection.getConnectionId();
        this.pendingReplies.put(connectionId, reply);
        if (logger.isDebugEnabled()) {
            logger.debug("Added pending reply " + connectionId);
        }
        connection.send(requestMessage);
        Message<?> replyMessage = reply.getReply();
        if (replyMessage == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Remote Timeout on " + connectionId);
            }
            // The connection is dirty - force it closed.
            this.connectionFactory.forceClose(connection);
            throw new MessageTimeoutException(requestMessage, "Timed out waiting for response");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Response " + replyMessage);
        }
        return replyMessage;
    } catch (Exception e) {
        logger.error("Tcp Gateway exception", e);
        if (e instanceof MessagingException) {
            throw (MessagingException) e;
        }
        throw new MessagingException("Failed to send or receive", e);
    } finally {
        if (connectionId != null) {
            this.pendingReplies.remove(connectionId);
            if (logger.isDebugEnabled()) {
                logger.debug("Removed pending reply " + connectionId);
            }
            if (this.isSingleUse) {
                connection.close();
            }
        }
        if (haveSemaphore) {
            this.semaphore.release();
            if (logger.isDebugEnabled()) {
                logger.debug("released semaphore");
            }
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException)

Example 7 with TcpConnection

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

the class TcpSendingMessageHandler method obtainConnection.

protected TcpConnection obtainConnection(Message<?> message) {
    TcpConnection connection = null;
    Assert.notNull(this.clientConnectionFactory, "'clientConnectionFactory' cannot be null");
    try {
        connection = this.clientConnectionFactory.getConnection();
    } catch (Exception e) {
        logger.error("Error creating connection", e);
        throw new MessageHandlingException(message, "Failed to obtain a connection", 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 8 with TcpConnection

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

the class TcpInboundGateway method doOnMessage.

private boolean doOnMessage(Message<?> message) {
    Message<?> reply = this.sendAndReceiveMessage(message);
    if (reply == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("null reply received for " + message + " nothing to send");
        }
        return false;
    }
    String connectionId = (String) message.getHeaders().get(IpHeaders.CONNECTION_ID);
    TcpConnection connection = null;
    if (connectionId != null) {
        connection = this.connections.get(connectionId);
    }
    if (connection == null) {
        publishNoConnectionEvent(message, connectionId);
        logger.error("Connection not found when processing reply " + reply + " for " + message);
        return false;
    }
    try {
        connection.send(reply);
    } catch (Exception e) {
        logger.error("Failed to send reply", e);
    }
    return false;
}
Also used : TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) MessagingException(org.springframework.messaging.MessagingException)

Example 9 with TcpConnection

use of org.springframework.integration.ip.tcp.connection.TcpConnection in project faf-java-server by FAForever.

the class LegacyAdapterConfigTest method onConnectionClosed.

@Test
public void onConnectionClosed() throws Exception {
    TcpConnection connection = mock(TcpConnection.class);
    when(connection.getConnectionId()).thenReturn("1");
    serverProperties.setPort(0);
    instance.onConnectionClosed(new TcpConnectionCloseEvent(connection, null));
    verify(clientConnectionService).removeConnection("1", Protocol.V1_LEGACY_UTF_16);
}
Also used : TcpConnectionCloseEvent(org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent) 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