Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 // Contributors: Dan MacDonald <dan@redknee.com>
 package ch.qos.logback.core.net;
 
 
This is the base class for module specific SocketAppender implementations.

Author(s):
Ceki Gülcü
Sébastien Pennec
 
 
 public abstract class SocketAppenderBase<E> extends AppenderBase<E> {

  
The default port number of remote logging server (4560).
 
   static final int DEFAULT_PORT = 4560;

  
The default reconnection delay (30000 milliseconds or 30 seconds).
 
   static final int DEFAULT_RECONNECTION_DELAY = 30000;

  
We remember host name as String in addition to the resolved InetAddress so that it can be returned via getOption().
 
   protected String remoteHost;
 
   protected InetAddress address;
   protected int port = ;
   protected ObjectOutputStream oos;
 
   private Connector connector;
 
   protected int counter = 0;

  
Start this appender.
 
   public void start() {
     int errorCount = 0;
     if ( == 0) {
       errorCount++;
       addError("No port was configured for appender"
           + 
           + " For more information, please visit http://logback.qos.ch/codes.html#socket_no_port");
     }
 
     if ( == null) {
       errorCount++;
       addError("No remote address was configured for appender"
           + 
           + " For more information, please visit http://logback.qos.ch/codes.html#socket_no_host");
     }
 
     connect();
 
     if (errorCount == 0) {
       this. = true;
     }
   }

  
Strop this appender.

This will mark the appender as closed and call then cleanUp() method.

 
   @Override
   public void stop() {
     if (!isStarted())
       return;
 
    this. = false;
    cleanUp();
  }

  
Drop the connection to the remote host and release the underlying connector thread if it has been created
  public void cleanUp() {
    if ( != null) {
      try {
        .close();
      } catch (IOException e) {
        addError("Could not close oos."e);
      }
       = null;
    }
    if ( != null) {
      addInfo("Interrupting the connector.");
      . = true;
       = null// allow gc
    }
  }
  void connect(InetAddress addressint port) {
    if (this. == null)
      return;
    try {
      // First, close the previous connection if any.
      cleanUp();
       = new ObjectOutputStream(new Socket(addressport).getOutputStream());
    } catch (IOException e) {
      String msg = "Could not connect to remote logback server at ["
          + address.getHostName() + "].";
      if ( > 0) {
        msg += " We will try again later.";
        fireConnector(); // fire the connector thread
      }
      addInfo(msge);
    }
  }
  protected void append(E event) {
    if (event == null)
      return;
    if ( == null) {
      addError("No remote host is set for SocketAppender named \""
          + this.
          + "\". For more information, please visit http://logback.qos.ch/codes.html#socket_no_host");
      return;
    }
    if ( != null) {
      try {
        postProcessEvent(event);
        Serializable serEvent = getPST().transform(event);
        .writeObject(serEvent);
        .flush();
        if (++ >= .) {
           = 0;
          // Failing to reset the object output stream every now and
          // then creates a serious memory leak.
          // System.err.println("Doing oos.reset()");
          .reset();
        }
      } catch (IOException e) {
        if ( != null) {
          try {
            .close();
          } catch (IOException ignore) {
          }
        }
         = null;
        addWarn("Detected problem with connection: " + e);
        if ( > 0) {
          fireConnector();
        }
      }
    }
  }
  protected abstract void postProcessEvent(E event);
  protected abstract PreSerializationTransformer<E> getPST();
  void fireConnector() {
    if ( == null) {
      addInfo("Starting a new connector thread.");
       = new Connector();
      .setDaemon(true);
      .start();
    }
  }
  protected static InetAddress getAddressByName(String host) {
    try {
      return InetAddress.getByName(host);
    } catch (Exception e) {
      // addError("Could not find address of [" + host + "].", e);
      return null;
    }
  }

  
The RemoteHost property takes the name of of the host where a corresponding server is running.
  public void setRemoteHost(String host) {
     = getAddressByName(host);
     = host;
  }

  
Returns value of the RemoteHost property.
  public String getRemoteHost() {
    return ;
  }

  
The Port property takes a positive integer representing the port where the server is waiting for connections.
  public void setPort(int port) {
    this. = port;
  }

  
Returns value of the Port property.
  public int getPort() {
    return ;
  }

  
The reconnectionDelay property takes a positive integer representing the number of milliseconds to wait between each failed connection attempt to the server. The default value of this option is 30000 which corresponds to 30 seconds.

Setting this option to zero turns off reconnection capability.

  public void setReconnectionDelay(int delay) {
    this. = delay;
  }

  
Returns value of the reconnectionDelay property.
  public int getReconnectionDelay() {
    return ;
  }

  
  
The Connector will reconnect when the server becomes available again. It does this by attempting to open a new connection every reconnectionDelay milliseconds.

It stops trying whenever a connection is established. It will restart to try reconnect to the server when previously open connection is dropped.

Author(s):
Ceki Gülcü
Since:
0.8.4
  class Connector extends Thread {
    boolean interrupted = false;
    public void run() {
      Socket socket;
      while (!) {
        try {
          sleep();
          SocketAppenderBase.this.addInfo("Attempting connection to " + .getHostName());
          socket = new Socket();
          synchronized (this) {
             = new ObjectOutputStream(socket.getOutputStream());
             = null;
            addInfo("Connection established. Exiting connector thread.");
            break;
          }
        } catch (InterruptedException e) {
          addInfo("Connector interrupted. Leaving loop.");
          return;
        } catch (java.net.ConnectException e) {
          addInfo("Remote host " + .getHostName()
              + " refused connection.");
        } catch (IOException e) {
          addInfo("Could not connect to " + .getHostName()
              + ". Exception is " + e);
        }
      }
      // addInfo("Exiting Connector.run() method.");
    }
  }
New to GrepCode? Check out our FAQ X