minor refactor to make it easier for derivations to expose exceptions on initialization

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@467078 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-10-23 18:48:18 +00:00
parent 7a25bcf203
commit e73f5aabf6
1 changed files with 103 additions and 95 deletions

View File

@ -17,6 +17,16 @@
*/ */
package org.apache.activemq.transport.tcp; package org.apache.activemq.transport.tcp;
import org.apache.activemq.Service;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportThreadSupport;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.net.SocketFactory;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
@ -31,17 +41,6 @@ import java.net.UnknownHostException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.net.SocketFactory;
import org.apache.activemq.Service;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportThreadSupport;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** /**
* An implementation of the {@link Transport} interface using raw tcp/ip * An implementation of the {@link Transport} interface using raw tcp/ip
* *
@ -85,7 +84,8 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
this.socketFactory = socketFactory; this.socketFactory = socketFactory;
try { try {
this.socket = socketFactory.createSocket(); this.socket = socketFactory.createSocket();
} catch (SocketException e) { }
catch (SocketException e) {
this.socket = null; this.socket = null;
} }
this.remoteLocation = remoteLocation; this.remoteLocation = remoteLocation;
@ -132,7 +132,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
log.trace("TCP consumer thread starting"); log.trace("TCP consumer thread starting");
while (!isStopped()) { while (!isStopped()) {
try { try {
Object command = wireFormat.unmarshal(dataIn); Object command = readCommand();
doConsume(command); doConsume(command);
} }
catch (SocketTimeoutException e) { catch (SocketTimeoutException e) {
@ -151,6 +151,10 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
} }
} }
protected Object readCommand() throws IOException {
return wireFormat.unmarshal(dataIn);
}
// Properties // Properties
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -285,7 +289,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
super.doStart(); super.doStart();
} }
protected void connect() throws SocketException, IOException { protected void connect() throws Exception {
if (socket == null && socketFactory == null) { if (socket == null && socketFactory == null) {
throw new IllegalStateException("Cannot connect if the socket or socketFactory have not been set"); throw new IllegalStateException("Cannot connect if the socket or socketFactory have not been set");
@ -305,25 +309,29 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
if (socket != null) { if (socket != null) {
if( localAddress!=null ) if (localAddress != null) {
socket.bind(localAddress); socket.bind(localAddress);
}
// If it's a server accepted socket.. we don't need to connect it // If it's a server accepted socket.. we don't need to connect it
// to a remote address. // to a remote address.
if (remoteAddress != null) { if (remoteAddress != null) {
if (connectionTimeout >= 0) { if (connectionTimeout >= 0) {
socket.connect(remoteAddress, connectionTimeout); socket.connect(remoteAddress, connectionTimeout);
} else { }
else {
socket.connect(remoteAddress); socket.connect(remoteAddress);
} }
} }
} else { }
else {
// For SSL sockets.. you can't create an unconnected socket :( // For SSL sockets.. you can't create an unconnected socket :(
// This means the timout option are not supported either. // This means the timout option are not supported either.
if (localAddress != null) { if (localAddress != null) {
socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort(), localAddress.getAddress(), localAddress.getPort()); socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort(), localAddress.getAddress(), localAddress.getPort());
} else { }
else {
socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort()); socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort());
} }
} }
@ -341,7 +349,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
} }
} }
protected void initializeStreams() throws IOException { protected void initializeStreams() throws Exception {
TcpBufferedInputStream buffIn = new TcpBufferedInputStream(socket.getInputStream(), 8 * 1024); TcpBufferedInputStream buffIn = new TcpBufferedInputStream(socket.getInputStream(), 8 * 1024);
this.dataIn = new DataInputStream(buffIn); this.dataIn = new DataInputStream(buffIn);
TcpBufferedOutputStream buffOut = new TcpBufferedOutputStream(socket.getOutputStream(), 16 * 1024); TcpBufferedOutputStream buffOut = new TcpBufferedOutputStream(socket.getOutputStream(), 16 * 1024);