Skip to content

Commit c1b2e25

Browse files
author
David R. MacIver
committed
refactor ConnectionFactory to allow configuration of the socket prior to connection
1 parent 0bdf458 commit c1b2e25

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
import java.util.HashMap;
3737
import java.util.Map;
3838

39+
import java.net.Socket;
40+
import java.net.InetSocketAddress;
41+
3942
import javax.net.SocketFactory;
4043
import javax.net.ssl.SSLContext;
4144
import javax.net.ssl.TrustManager;
@@ -153,7 +156,26 @@ protected FrameHandler createFrameHandler(Address addr)
153156
String hostName = addr.getHost();
154157
int portNumber = addr.getPort();
155158
if (portNumber == -1) portNumber = AMQP.PROTOCOL.PORT;
156-
return new SocketFrameHandler(_factory, hostName, portNumber);
159+
Socket socket = _factory.createSocket();
160+
configureSocket(socket);
161+
socket.connect(new InetSocketAddress(hostName, portNumber));
162+
return new SocketFrameHandler(socket);
163+
}
164+
165+
/**
166+
* Provides a hook to insert custom configuration of the sockets used
167+
* to connect to an AMQP server before they connect.
168+
*
169+
* The default behaviour of this method is to disable Nagle's algorithm to get
170+
* more consistently low latency.
171+
* However it may be overridden freely and there is no requirement to retain
172+
* this behaviour.
173+
*
174+
* @param socket The socket that is to be used for the Connection
175+
*/
176+
protected void configureSocket(Socket socket) throws IOException{
177+
//disable Nagle's algorithm, for more consistently low latency
178+
socket.setTcpNoDelay(true);
157179
}
158180

159181
private Connection newConnection(Address[] addrs,

src/com/rabbitmq/client/impl/SocketFrameHandler.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@
4747
*/
4848

4949
public class SocketFrameHandler implements FrameHandler {
50-
/** Host we connect to */
51-
public final String _host;
52-
53-
/** Port number we connect to */
54-
public final int _port;
55-
5650
/** The underlying socket */
5751
public final Socket _socket;
5852

@@ -66,33 +60,21 @@ public class SocketFrameHandler implements FrameHandler {
6660
// twice simultaneously.
6761

6862
/**
69-
* Instantiate a SocketFrameHandler.
70-
* @param factory the socket factory to use to build our Socket - may be SSLSocketFactory etc
71-
* @param hostName the host name
72-
* @param portNumber the port number
73-
* @throws IOException if there is a problem accessing the connection
63+
* @param socket the socket to use
7464
*/
75-
public SocketFrameHandler(SocketFactory factory,
76-
String hostName,
77-
int portNumber)
78-
throws IOException
79-
{
80-
_host = hostName;
81-
_port = portNumber;
82-
_socket = factory.createSocket(_host, _port);
83-
//disable Nagle's algorithm, for more consistently low latency
84-
_socket.setTcpNoDelay(true);
65+
public SocketFrameHandler(Socket socket) throws IOException{
66+
_socket = socket;
8567

8668
_inputStream = new DataInputStream(new BufferedInputStream(_socket.getInputStream()));
8769
_outputStream = new DataOutputStream(new BufferedOutputStream(_socket.getOutputStream()));
8870
}
8971

9072
public String getHost() {
91-
return _host;
73+
return _socket.getInetAddress().getHostName();
9274
}
9375

9476
public int getPort() {
95-
return _port;
77+
return _socket.getPort();
9678
}
9779

9880
public void setTimeout(int timeoutMs)

test/src/com/rabbitmq/examples/TestMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected FrameHandler createFrameHandler(Address addr)
101101
String hostName = addr.getHost();
102102
int portNumber = addr.getPort();
103103
if (portNumber == -1) portNumber = AMQP.PROTOCOL.PORT;
104-
return new SocketFrameHandler(getSocketFactory(), hostName, portNumber) {
104+
return new SocketFrameHandler(getSocketFactory().createSocket(hostName, portNumber)) {
105105
public void sendHeader() throws IOException {
106106
sendHeader(protocolMajor, protocolMinor);
107107
}

0 commit comments

Comments
 (0)