You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Kathey Marsden <km...@sbcglobal.net> on 2006/06/27 00:01:57 UTC

Questions about Network Server API Behavior

   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();  
    }

 
}






Re: Questions about Network Server API Behavior

Posted by Kathey Marsden <km...@sbcglobal.net>.
Thanks Sunitha for looking at this and the comments. I filed the 
following issues :

(DERBY-1465) NetworkServerControl.start() should throw an exception and 
not just  print  exceptions  starting network server
(DERBY-1466) Network Server should flush the PrintWriter after console 
output
(DERBY-1467) It would be useful to have a NetworkServerControl start  
API that took a timeout parameter

I don't know if it is better for the sample code to be on the Wiki or 
improve our documentation.
Anyway, I will just leave it in the archives for now.

Kathey


Re: Questions about Network Server API Behavior

Posted by Sunitha Kambhampati <ks...@gmail.com>.
Kathey Marsden wrote:

> 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.
>
I think this is a bug.  It can get confusing for applications.  Lets say 
- App1 starts network server on port 1527. Another application app2  
uses the NetworkServerControl api  to start the server on 1527 and never 
really hits an exception so assumes all is well.  

Also the javadoc for the api for start says   'An exception will be 
thrown if there is an error starting the server.'
http://db.apache.org/derby/docs/10.1/publishedapi/org/apache/derby/drda/NetworkServerControl.html#start(java.io.PrintWriter)

> 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.
>
+1.  It will be useful for users to have a start methodapi that starts 
the server in some timeout or if there is some problem, throws an exception

>  Below  is the program output and code.

Thanks Kathey for posting the program.  It maybe be a good idea to post  
program with code to start the server and associated ping method on the 
wiki .

Sunitha.