Example usage for org.springframework.integration.ip.tcp.connection TcpConnection getConnectionId

List of usage examples for org.springframework.integration.ip.tcp.connection TcpConnection getConnectionId

Introduction

In this page you can find the example usage for org.springframework.integration.ip.tcp.connection TcpConnection getConnectionId.

Prototype

String getConnectionId();

Source Link

Usage

From source file:org.openwms.common.comm.tcp.CustomTcpMessageMapper.java

@Override
public Message<?> toMessage(TcpConnection connection) throws Exception {
    Object data = connection.getPayload();
    LOGGER.debug("Incoming:" + data);
    if (data != null) {
        Message<?> message = this.inboundMessageConverter.toMessage(data, null);
        AbstractIntegrationMessageBuilder<?> messageBuilder = this.getMessageBuilderFactory()
                .fromMessage(message);//  w  w w.j  av a  2s . com
        this.addStandardHeaders(connection, messageBuilder);
        this.addCustomHeaders(connection, messageBuilder);
        return messageBuilder.build();
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Null payload from connection " + connection.getConnectionId());
        }
        return null;
    }
}

From source file:org.springframework.integration.ip.addons.CachingTcpConnectionFactory.java

protected TcpConnection getConnectionNoWait(TcpConnection connection) throws Exception {
    synchronized (this.targetConnectionFactory) {
        while (connection == null && this.available.size() > 0) {
            connection = retrieveConnection(-1);
        }/*from   ww w  . j a  va  2 s . co m*/
        if (connection == null) {
            if (this.getAllocated() < this.poolSize) {
                connection = newConnection();
                this.inUse.add(connection);
                if (logger.isDebugEnabled()) {
                    logger.debug("Created new connection " + connection.getConnectionId());
                }
            }
        }
    }
    return connection;
}

From source file:org.springframework.integration.ip.addons.CachingTcpConnectionFactory.java

protected TcpConnection retrieveConnection(int timeout) throws Exception {
    TcpConnection connection;
    if (timeout > 0) {
        connection = this.available.poll(timeout, TimeUnit.MILLISECONDS);
    } else {//from   w  w w . j  ava2  s. co  m
        connection = this.available.poll();
    }
    if (connection == null) {
        return null;
    }
    if (!connection.isOpen()) {
        if (logger.isDebugEnabled()) {
            logger.debug(connection.getConnectionId() + " is closed, trying another");
        }
        return null;
    }
    synchronized (this.targetConnectionFactory) {
        this.inUse.add(connection);
    }
    if (logger.isDebugEnabled() && connection != null) {
        logger.debug("Retrieved " + connection.getConnectionId() + " from cache");
    }
    return connection;
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

private void doTestCloseOnSendError(TcpConnection conn1, TcpConnection conn2,
        CachingClientConnectionFactory cccf) throws Exception {
    TcpConnection cached1 = cccf.getConnection();
    try {//from  w  w  w  .j a  v a2s  .  co  m
        cached1.send(new GenericMessage<String>("foo"));
        fail("Expected IOException");
    } catch (IOException e) {
        assertEquals("Foo", e.getMessage());
    }
    // Before INT-3163 this failed with a timeout - connection not returned to pool after failure on send()
    TcpConnection cached2 = cccf.getConnection();
    assertTrue(cached1.getConnectionId().contains(conn1.getConnectionId()));
    assertTrue(cached2.getConnectionId().contains(conn2.getConnectionId()));
}

From source file:org.springframework.integration.ip.tcp.connection.ClientModeConnectionManager.java

public void run() {
    synchronized (this.clientConnectionFactory) {
        try {//from  w w w.  j av  a  2s .c  om
            TcpConnection connection = this.clientConnectionFactory.getConnection();
            if (connection != lastConnection) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Connection " + connection.getConnectionId() + " established");
                }
                lastConnection = connection;
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace("Connection " + connection.getConnectionId() + " still OK");
                }
            }
        } catch (Exception e) {
            logger.error("Could not establish connection using " + this.clientConnectionFactory, e);
        }
    }
}

From source file:org.springframework.integration.ip.tcp.connection.TcpMessageMapper.java

public Message<Object> toMessage(TcpConnection connection) throws Exception {
    Message<Object> message = null;
    Object payload = connection.getPayload();
    if (payload != null) {
        MessageBuilder<Object> messageBuilder = MessageBuilder.withPayload(payload);
        this.addStandardHeaders(connection, messageBuilder);
        this.addCustomHeaders(connection, messageBuilder);
        message = messageBuilder.build();
    } else {//  w  ww. j av  a 2s .  com
        if (logger.isWarnEnabled()) {
            logger.warn("Null payload from connection " + connection.getConnectionId());
        }
    }
    return message;
}

From source file:org.springframework.integration.ip.tcp.connection.TcpMessageMapper.java

protected final void addStandardHeaders(TcpConnection connection, MessageBuilder<?> messageBuilder) {
    String connectionId = connection.getConnectionId();
    messageBuilder.setHeader(IpHeaders.HOSTNAME, connection.getHostName())
            .setHeader(IpHeaders.IP_ADDRESS, connection.getHostAddress())
            .setHeader(IpHeaders.REMOTE_PORT, connection.getPort())
            .setHeader(IpHeaders.CONNECTION_ID, connectionId);
    if (this.applySequence) {
        messageBuilder.setCorrelationId(connectionId)
                .setSequenceNumber((int) connection.incrementAndGetConnectionSequence());
    }//from   ww  w.ja  v  a  2s  .co  m
}

From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandler.java

/**
 * Method that actually does the write.//from   ww  w .  j  a v  a 2  s.  c  o  m
 * @param message The message to write.
 */
protected void doWrite(Message<?> message) {
    try {
        TcpConnection connection = getConnection();
        if (connection == null) {
            throw new MessageMappingException(message, "Failed to create connection");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Got Connection " + connection.getConnectionId());
        }
        connection.send(message);
    } catch (Exception e) {
        String connectionId = null;
        if (this.connection != null) {
            connectionId = this.connection.getConnectionId();
        }
        this.connection = null;
        if (e instanceof MessageMappingException) {
            throw (MessageMappingException) e;
        }
        throw new MessageMappingException(message, "Failed to map message using " + connectionId, e);
    }
}

From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandler.java

public void addNewConnection(TcpConnection connection) {
    connections.put(connection.getConnectionId(), connection);
}

From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandler.java

public void removeDeadConnection(TcpConnection connection) {
    connections.remove(connection.getConnectionId());
}