INT-1279 TCP ib and ob Gateways Using Connection Factories - namespace and docs to follow
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.ip.tcp;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.gateway.AbstractMessagingGateway;
|
||||
import org.springframework.integration.ip.IpHeaders;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpConnection;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpListener;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpSender;
|
||||
|
||||
/**
|
||||
* Inbound Gateway using a server connection factory - threading is controlled by the
|
||||
* factory. For java.net connections, each socket can process only one message at a time.
|
||||
* For java.nio connections, messages may be multiplexed but the client will need to
|
||||
* provide correlation logic. If the client is a {@link TcpOutboundGateway} multiplexing
|
||||
* is not used, but multiple concurrent connections can be used if the connection factory uses
|
||||
* single-use connections. For true asynchronous bi-directional communication, a pair of
|
||||
* inbound / outbound channel adapters should be used.
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class TcpInboundGateway extends AbstractMessagingGateway implements TcpListener, TcpSender {
|
||||
|
||||
protected AbstractServerConnectionFactory connectionFactory;
|
||||
|
||||
private Map<String, TcpConnection> connections = new ConcurrentHashMap<String, TcpConnection>();
|
||||
|
||||
public boolean onMessage(Message<?> message) {
|
||||
Message<?> reply = this.sendAndReceiveMessage(message);
|
||||
String connectionId = (String) message.getHeaders().get(IpHeaders.CONNECTION_ID);
|
||||
TcpConnection connection = connections.get(connectionId);
|
||||
if (connection == null) {
|
||||
logger.error("Connection " + connectionId + " not found");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
connection.send(reply);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to send reply", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the associated connection factory is listening.
|
||||
*/
|
||||
public boolean isListening() {
|
||||
return connectionFactory.isListening();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param connectionFactory the Connection Factory
|
||||
*/
|
||||
public void setConnectionFactory(AbstractServerConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
connectionFactory.registerListener(this);
|
||||
connectionFactory.registerSender(this);
|
||||
}
|
||||
|
||||
public void addNewConnection(TcpConnection connection) {
|
||||
connections.put(connection.getConnectionId(), connection);
|
||||
}
|
||||
|
||||
public void removeDeadConnection(TcpConnection connection) {
|
||||
connections.remove(connection.getConnectionId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ip.tcp;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageTimeoutException;
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.ip.IpHeaders;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpConnection;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpListener;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpSender;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* TCP outbound gateway that uses a client connection factory. If the factory is configured
|
||||
* for single-use connections, each request is sent on a new connection; if the factory does not use
|
||||
* single use connections, each request is blocked until the previous response is received
|
||||
* (or times out). Asynchronous requests/responses over the same connection are not
|
||||
* supported - use a pair of outbound/inbound adapters for that use case.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler
|
||||
implements TcpSender, TcpListener {
|
||||
|
||||
protected AbstractConnectionFactory connectionFactory;
|
||||
|
||||
private Map<String, AsyncReply> pendingReplies = new ConcurrentHashMap<String, AsyncReply>();
|
||||
|
||||
private Semaphore semaphore = new Semaphore(1, true);
|
||||
|
||||
private long replyTimeout = 10000;
|
||||
|
||||
private long requestTimeout = 10000;
|
||||
|
||||
@Override
|
||||
protected Object handleRequestMessage(Message<?> requestMessage) {
|
||||
Assert.notNull(connectionFactory, this.getClass().getName() +
|
||||
" requires a client connection factory");
|
||||
boolean haveSemaphore = false;
|
||||
try {
|
||||
boolean singleUseConnection = this.connectionFactory.isSingleUse();
|
||||
if (!singleUseConnection) {
|
||||
logger.debug("trying semaphore");
|
||||
if (!this.semaphore.tryAcquire(this.requestTimeout, TimeUnit.MILLISECONDS)) {
|
||||
throw new MessageTimeoutException(requestMessage, "Timed out waiting for connection");
|
||||
}
|
||||
haveSemaphore = true;
|
||||
logger.debug("got semaphore");
|
||||
}
|
||||
TcpConnection connection = this.connectionFactory.getConnection();
|
||||
AsyncReply reply = new AsyncReply();
|
||||
pendingReplies.put(connection.getConnectionId(), reply);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Added " + connection.getConnectionId());
|
||||
}
|
||||
connection.send(requestMessage);
|
||||
Message<?> replyMessage = reply.getReply();
|
||||
if (replyMessage == null) {
|
||||
throw new MessageTimeoutException(requestMessage, "Timed out waiting for response");
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Respose " + replyMessage);
|
||||
}
|
||||
return replyMessage;
|
||||
} catch (Exception e) {
|
||||
if (e instanceof MessagingException) {
|
||||
throw (MessagingException) e;
|
||||
}
|
||||
throw new MessagingException("Failed to send or receive", e);
|
||||
} finally {
|
||||
if (haveSemaphore) {
|
||||
this.semaphore.release();
|
||||
logger.debug("released semaphore");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onMessage(Message<?> message) {
|
||||
String connectionId = (String) message.getHeaders().get(IpHeaders.CONNECTION_ID);
|
||||
if (connectionId == null) {
|
||||
logger.error("Cannot correlate response - no connection id");
|
||||
return false;
|
||||
}
|
||||
AsyncReply reply = pendingReplies.get(connectionId);
|
||||
if (reply == null) {
|
||||
logger.error("Cannot correlate response - no pending reply");
|
||||
return false;
|
||||
}
|
||||
reply.setReply(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isListening() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setConnectionFactory(AbstractConnectionFactory connectionFactory) {
|
||||
Assert.isTrue(connectionFactory instanceof AbstractClientConnectionFactory,
|
||||
this.getClass().getName() + " requires a client connection factory");
|
||||
this.connectionFactory = connectionFactory;
|
||||
connectionFactory.registerListener(this);
|
||||
connectionFactory.registerSender(this);
|
||||
}
|
||||
|
||||
public void addNewConnection(TcpConnection connection) {
|
||||
// do nothing - no asynchronous multiplexing supported
|
||||
}
|
||||
|
||||
public void removeDeadConnection(TcpConnection connection) {
|
||||
// do nothing - no asynchronous multiplexing supported
|
||||
}
|
||||
|
||||
/**
|
||||
* @param replyTimeout the replyTimeout to set
|
||||
*/
|
||||
public void setReplyTimeout(long timeout) {
|
||||
this.replyTimeout = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param requestTimeout the requestTimeout to set
|
||||
*/
|
||||
public void setRequestTimeout(long requestTimeout) {
|
||||
this.requestTimeout = requestTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class used to coordinate the asynchronous reply to its request.
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
class AsyncReply {
|
||||
private CountDownLatch latch;
|
||||
private Message<?> reply;
|
||||
public AsyncReply() {
|
||||
this.latch = new CountDownLatch(1);
|
||||
}
|
||||
/**
|
||||
* Sender blocks here until the reply is received, or we time out
|
||||
* @return The return message or null if we time out
|
||||
* @throws Exception
|
||||
*/
|
||||
public Message<?> getReply() throws Exception {
|
||||
if (!this.latch.await(replyTimeout, TimeUnit.MILLISECONDS)) {
|
||||
return null;
|
||||
}
|
||||
return this.reply;
|
||||
}
|
||||
public void setReply(Message<?> reply) {
|
||||
this.reply = reply;
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,8 +46,9 @@ public class TcpReceivingChannelAdapter
|
||||
|
||||
protected ConnectionFactory serverConnectionFactory;
|
||||
|
||||
public void onMessage(Message<?> message) {
|
||||
public boolean onMessage(Message<?> message) {
|
||||
sendMessage(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -329,10 +329,13 @@ public abstract class AbstractConnectionFactory
|
||||
this.interceptorFactoryChain.getInterceptorFactories()) {
|
||||
TcpConnectionInterceptor wrapper = factory.getInterceptor();
|
||||
wrapper.setTheConnection(connection);
|
||||
// if no ultimate listener, register each wrapper in turn
|
||||
// if no ultimate listener or sender, register each wrapper in turn
|
||||
if (this.listener == null) {
|
||||
connection.registerListener(wrapper);
|
||||
}
|
||||
if (this.sender == null) {
|
||||
connection.registerSender(wrapper);
|
||||
}
|
||||
connection = wrapper;
|
||||
}
|
||||
return connection;
|
||||
|
||||
@@ -110,11 +110,11 @@ public abstract class AbstractTcpConnectionInterceptor implements TcpConnectionI
|
||||
return this.theConnection.isServer();
|
||||
}
|
||||
|
||||
public void onMessage(Message<?> message) {
|
||||
public boolean onMessage(Message<?> message) {
|
||||
if (this.tcpListener == null) {
|
||||
throw new NoListenerException("No listener registered for message reception");
|
||||
}
|
||||
this.tcpListener.onMessage(message);
|
||||
return this.tcpListener.onMessage(message);
|
||||
}
|
||||
|
||||
public void send(Message<?> message) throws Exception {
|
||||
|
||||
@@ -31,7 +31,8 @@ public interface TcpListener {
|
||||
/**
|
||||
* Called by a TCPConnection when a new message arrives.
|
||||
* @param message The message.
|
||||
* @return true if the message was intercepted
|
||||
*/
|
||||
public abstract void onMessage(Message<?> message);
|
||||
public abstract boolean onMessage(Message<?> message);
|
||||
|
||||
}
|
||||
@@ -103,6 +103,7 @@ public class TcpNetConnection extends AbstractTcpConnection {
|
||||
Message<?> message = null;
|
||||
boolean okToRun = true;
|
||||
logger.debug("Reading...");
|
||||
boolean intercepted = false;
|
||||
while (okToRun) {
|
||||
try {
|
||||
message = this.mapper.toMessage(this);
|
||||
@@ -127,7 +128,7 @@ public class TcpNetConnection extends AbstractTcpConnection {
|
||||
logger.warn("Unexpected message - no inbound adapter registered with connection " + message);
|
||||
continue;
|
||||
}
|
||||
listener.onMessage(message);
|
||||
intercepted = listener.onMessage(message);
|
||||
} catch (Exception e) {
|
||||
if (e instanceof NoListenerException) {
|
||||
if (this.singleUse) {
|
||||
@@ -143,9 +144,10 @@ public class TcpNetConnection extends AbstractTcpConnection {
|
||||
}
|
||||
/*
|
||||
* For single use sockets, we close after receipt if we are on the client
|
||||
* side, or the server side has no outbound adapter registered
|
||||
* side, and the data was not intercepted,
|
||||
* or the server side has no outbound adapter registered
|
||||
*/
|
||||
if (this.singleUse && this.server && this.sender == null) {
|
||||
if (this.singleUse && ((!this.server && !intercepted) || (this.server && this.sender == null))) {
|
||||
logger.debug("Closing single use socket after inbound message " + this.connectionId);
|
||||
this.close();
|
||||
okToRun = false;
|
||||
|
||||
@@ -191,9 +191,10 @@ public class TcpNioConnection extends AbstractTcpConnection {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean intercepted = false;
|
||||
try {
|
||||
if (message != null) {
|
||||
listener.onMessage(message);
|
||||
intercepted = listener.onMessage(message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (e instanceof NoListenerException) {
|
||||
@@ -209,9 +210,10 @@ public class TcpNioConnection extends AbstractTcpConnection {
|
||||
}
|
||||
/*
|
||||
* For single use sockets, we close after receipt if we are on the client
|
||||
* side, or the server side has no outbound adapter registered
|
||||
* side, and the data was not intercepted,
|
||||
* or the server side has no outbound adapter registered
|
||||
*/
|
||||
if (this.singleUse && this.server && this.sender == null) {
|
||||
if (this.singleUse && ((!this.server && !intercepted) || (this.server && this.sender == null))) {
|
||||
logger.debug("Closing single use cbannel after inbound message " + this.connectionId);
|
||||
this.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user