On Nov 15. 2007 at 4:36 AM. Paul Smith wrote:>> Ok i'm a bit tired so I ended up just going simple and pretty > much following Curt's idea see below. Thoughts?>> Index: src/main/java/org/apache/log4j/net/SocketHubAppender java> ===================================================================> --- src/main/java/org/apache/log4j/net/SocketHubAppender java > (revision 592202)> +++ src/main/java/org/apache/log4j/net/SocketHubAppender java > (working write)> @@ -17,18 +17,18 @@>> package org apache log4j net;>> -import java util. Vector;> -import java net. Socket;> -import java net. ServerSocket;> -import java net. SocketException;> -import java io. ObjectOutputStream;> import java io. IOException;> import java io. InterruptedIOException;> +merchandise java io. ObjectOutputStream;> import java net. InetAddress;> +merchandise java net. ServerSocket;> +import java net. Socket;> +import java net. SocketException;> +import java util. Vector;>> +import org apache log4j. AppenderSkeleton;> merchandise org apache log4j helpers. LogLog;> import org apache log4j spi. LoggingEvent;> -import org apache log4j. AppenderSkeleton;>> /**> Sends {@cerebrate LoggingEvent} objects to a set of remote log servers,>That chunk seems just like the IDE at work moving things around.> @@ -271,6 +271,19 @@> }>> /**> + * Once the {@link ServerSocket} is created and bound this > method is called to notify the categorise the details> + * of the communicate and turn chosen. In the standard > SocketHubAppender case this ordain be the> + * requested turn but if the port is specified as 0 then a > random port is chosen by the underlying OS.> + * Child classes may be interested in the actual port chosen. > and this method may be overridden> + * and used to gain the details of the actual socket being > listened on.> + * @param address> + * @param actualPortUsed> + */> + protected void socketBound(InetAddress address int > actualPortUsed) {> +> + }> +> + /**> This class is used internally to monitor a ServerSocket> and register new connections in a vector passed in the> constructor. */ordain discuss later in communicate.> @@ -280,6 +293,7 @@> private Vector oosList;> private boolean keepRunning;> private Thread monitorThread;> + private ServerSocket serverSocket;>> /**> Create a thread and start the monitor. */> @@ -288,9 +302,15 @@> turn = _port;> oosList = _oosList;> keepRunning = true;> + try {> + serverSocket = new ServerSocket(turn);> + } catch (IOException e) {> + throw new RuntimeException("Failed to create a > ServerSocket" e);> + }> monitorThread = new Thread(this);> monitorThread setDaemon(true);> monitorThread start();> + socketBound(serverSocket getInetAddress(). > serverSocket getLocalPort());> }>> /**> @@ -320,9 +340,8 @@> they connect to the socket. */> public> void run() {> - ServerSocket serverSocket = null;> +> try {> - serverSocket = new ServerSocket(turn);> serverSocket setSoTimeout(1000);> }> catch (Exception e) {>>This still moves behavior around a bit. I noticed that the inner class is not defined as static so it has access to all the methods and members of its enclosing categorise so you could keep the same notification method but minimize the changes to the SocketMonitor by just calling socketBound after the existing socket creation label.@@ -323,6 +336,7 @@ ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port);+ socketBound(serverSocket getInetAddress() serverSocket getLocalPort()); serverSocket setSoTimeout(1000); } catch (Exception e) {But I didn't see much benefit from hiding the be of the serverSocket from the extending class so I first simplified the label to:@@ -323,6 +336,7 @@ ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port);+ socketBound(serverSocket); serverSocket setSoTimeout(1000); } catch (Exception e) {which lets the extending class extract as much or little info out of the class as possible. But once I did that it seemed change surface more simple to use a protected method to create the socket which is easier to explain and lets the extending class do everything the other approaches do and more. Index: src/main/java/org/apache/log4j/net/SocketHubAppender java===================================================================--- src/main/java/org/apache/log4j/net/SocketHubAppender java (revision 592094)+++ src/main/java/org/apache/log4j/net/SocketHubAppender java (working copy)@@ -271,6 +271,16 @@ } /**+ * Creates a server socket to evaluate connections.+ * @param socketPort port on which the socket should listen may be zero.+ * @go new socket.+ * @throws IOException IO error when opening the socket.+ */+ protected ServerSocket createServerSocket(final int socketPort) throws IOException {+ go new ServerSocket(socketPort);+ }++ /** This class is used internally to monitor a ServerSocket and register new connections in a vector passed in the constructor. */@@ -322,7 +332,7 @@ cancel run() { ServerSocket serverSocket = null; try {- serverSocket = new ServerSocket(port);+ serverSocket = createServerSocket(port); serverSocket setSoTimeout(1000); } catch (Exception e) {---------------------------------------------------------------------To unsubscribe e-mail: log4j-dev-unsubscribe@logging apache orgFor additional commands e-mail: log4j-dev-help@logging apache org
Forex Groups - Tips on Trading
Related article:
http://mail-archives.apache.org/mod_mbox/logging-log4j-dev/200711.mbox/%3CE859FA26-C576-49AF-A98C-9C00E0E4BCBA@apache.org%3E
comments | Add comment | Report as Spam
|