db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kathey Marsden <kmarsdende...@sbcglobal.net>
Subject Questions about Network Server API Behavior
Date Mon, 26 Jun 2006 22:01:57 GMT
   I was playing with starting  Network Server and  redirectect console 
output from an  application  using the NetworkServerControl API.   And I 
noticed a few things.

1) I had to set the file to autoflush to see  the console output go to 
the file.
    I tend to think this is a bug.  Should Network Server automatically 
flush the PrintWriter after operations?

2) If another server was started on the same port, 
NetworkServerControl.start()  prints the exception to the console 
output, but does not throw an exception
  I think that this also is a bug and Network Server should throw an 
exception if another server is up, but wanted to check.  It may have 
been changed not to when  derby.drda.startNetworkServer was added but I  
am not sure.

3) It seems like it would be useful to have a start method with a 
timeout and/or derby property to determine the timeout period so that 
users would not need to write the associated ping code with start.

Thoughts on this?  Below  is the program output and code.

$ java ServerStartSample
TEST 1: Normal startServer(600)
PASS: Server started as expected
TEST 2: startServer(20)with a server already on port 1527
FAIL: Server should have failed to come up with something else on 1527
$



import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;

import org.apache.derby.drda.NetworkServerControl;

public class ServerStartSample {

    private static final String SERVER_START_LOG = "ServerStartSample.log";
    private NetworkServerControl derbyServer;
    private PrintWriter starterWriter;
   
  
    /**
     *
     */
    public ServerStartSample() throws Exception{
      
       starterWriter = new PrintWriter(new FileOutputStream(new 
File(SERVER_START_LOG)),true);
       
    }

    /**
     * @param args
     */
    public static void main(String[] args)  throws Exception{
        testStartServer();
    }

    private static void testStartServer()  throws Exception {
        ServerStartSample me = new ServerStartSample();
        boolean started =false;
        // TEST #1 Normal startup
        try {
            // give the server 10 minutes to come up
            System.out.println("TEST 1: Normal startServer(" + 600 + ")");
            me.startServer(600);
            System.out.println("PASS: Server started as expected");
            me.stopServer();
            Thread.sleep(20000);
        } catch (Exception e)
        {
            System.out.println("FAIL: Unexpected Exception starting 
server");
            e.printStackTrace();   
        }
       
        //TEST #2 - Try to start a server with a server started already.
        try {
            System.out.println("TEST 2: startServer(" + 20 + ")" + "" +
            "with a server already on port 1527");
            new NetworkServerControl().start(new PrintWriter(System.out));
            me.startServer(20);
            System.out.println("FAIL: Server should have failed to come 
up with something else on 1527");
            me.stopServer();
        } catch (Exception e)
        {
           System.out.println("PASS:Expected Exception starting server 
with port 1527 used" + e.getMessage());
        }

    }


    /**
     * @param msg
     */
    private void logMessage(String msg) {
      starterWriter.println("INFO: " + msg);
    }

   
    /**
     * @param secondsToVerify
     */
    private void stopServer(){

        try {
            derbyServer.shutdown();
        } catch (Exception e) {
           logError("Server failed to shutdown", e);
        }
      
    }

    /**
     *
     * @param secondsToTimeout number of seconds to wait for server to start
     * @throws Exception
     */
    public void startServer(int secondsToTimeout) throws Exception
    {
        derbyServer = new NetworkServerControl();
        boolean connected = false;
        derbyServer.start(starterWriter);
    
        for (int i = 0; i < secondsToTimeout && ! connected ; i++)
        {
            try {
                    derbyServer.ping();
                    connected = true;
                    logMessage("Server successfully started. Connected 
after " + i + " seconds.");
            }
            catch (Exception e)
            {
                if (i < secondsToTimeout)
                {
                   //logMessage("Ping results after " + i + " seconds:" +
                    //           e.getMessage());                 
                    Thread.sleep(1000);
                    continue;
                }
                else
                {
                    logError(" Failed to connect to server after " +  i  +
                            " seconds",e);
                    throw e;
                }
            }
        }  
    }
   

    private void logError(String msg , Exception e) {
        starterWriter.println("ERROR: " + msg);
        e.printStackTrace(starterWriter);  
    }

    public PrintWriter getStarterWriter() {
        return starterWriter;
    }
   
    public void finalize()
    {
     starterWriter.close();  
    }

 
}






Mime
View raw message