|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 6
Listing 3 shows a simple viewer application for my
RemoteAppender1234) is passed to the server via the configuration file in Listing 2. Here's the relevant line:
log4j.appender.REMOTE.Port=1234
The client application waits in a loop until it can connect to the server, and then it just reads messages from the server
and prints them to the console. Nothing earth shattering. The client knows nothing about log4j—it just reads strings and prints
them—so the coupling to the log4j systems is nonexistent. Launch the client with
java Client
Listing 3. Client.java: A client for viewing logging messages
1 import java.net.*;
2 import java.io.*;
3
4 public class Client
5 {
6 public static void main(String[] args) throws Exception
7 {
8 Socket s;
9 while( true )
10 { try
11 {
12 s = new Socket( "localhost", 1234 );
13 break;
14 }
15 catch( java.net.ConnectException e )
16 { // Assume that the host isn't available yet, wait
17 // a moment, then try again.
18 Thread.currentThread().sleep(50);
19 }
20 }
21
22 BufferedReader in = new BufferedReader(
23 new InputStreamReader( s.getInputStream() ) );
24
25 String line;
26 while( (line = in.readLine()) != null )
27 System.err.println( line );
28 }
29 }
Note, by the way, that the client in Listing 3 is a great example of when not to use Java's NIO (new input/output) classes. There's no need for asynchronous reading here, and NIO would complicate the application considerably.
All that's left is the appender itself, which manages the server-side socket and writes the output to the clients that connect to it. (Several clients can receive logging messages from the same appender simultaneously.) The code is in Listing 4.
Starting with the basic structure, the
RemoteAppenderAppenderSkeletongetXxx()setXxx()XxxPort
Note that both the getter and setter methods are
privategetPort()setPort()privateprivate
Archived Discussions (Read only)