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");
}
}
}
}
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;
}
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;
}
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);
}
Aggregations