You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by "Pavel G. Sozonovsky" <pa...@alee.com> on 2002/07/16 13:03:53 UTC

Using Xindice inside own code

Hello All,

  I'm newbie to Xindice.
  I'm just trying to use it from my code.
  I create new thread:
    ...
    Thread serverThread = new Thread(new Runnable()
    {
      public void run()
      {
        Xindice.main(new String[]{_path});
      }
    });
    serverThread.start();
    ...
    , where _path is real path to {XINDICE_HOME}/config/system.xml.

    It works. The problem of this code is that I can't know exactly
    when server becomes ready to process requests to database.
    There is one (for me) bad way to set hardcoded pause in main thread:
    ...
    Thread.sleep(SERVER_START_TIME);
    ...
    But it is really bad way.
    Is there any other way to start server and be notified to start
    working with database???

    Thanks for any help.

-- 
Best regards,
Pavel

mailto:pavel@alee.com



Re[2]: Using Xindice inside own code

Posted by "Pavel G. Sozonovsky" <pa...@alee.com>.
Hi Catie,

Thank you for your help.

I've tried to use a bit modified code:

String xindicePath = xindiceDir.getCanonicalPath();
String classpath = System.getProperties().getProperty("java.class.path");
String javaHome = System.getProperties().getProperty("java.home");

String startCommand = javaHome + "\\bin\\java.exe -Xms16m -Xmx128m -Dxindice.home=" +
                      xindicePath+" -classpath " + classpath +
                      " org.apache.xindice.server.Xindice " +
                      xindicePath + "\\config\\system.xml";

Process _serverProcess = Runtime.getRuntime().exec(startCommand, new String[0], xindiceDir);
// ...

But I get another error:

LINE IS:
LINE IS: Xindice 1.0 (Birthday)
LINE IS:
LINE IS: Database: 'db' initializing
LINE IS: Script: 'GET' added to script storage
LINE IS: Service: 'db' started
ERROR LINE IS: java.lang.NullPointerException
ERROR LINE IS:   at org.apache.xindice.server.services.SocketServer.createServerSocket(SocketServer.java:242)
ERROR LINE IS:   at org.apache.xindice.server.services.HTTPServer.run(HTTPServer.java:180)
ERROR LINE IS:   at java.lang.Thread.run(Thread.java:484)


Don't you know what is a problem?
Do I anything wrong?

Thanks again.

Pavel
mailto:pavel@alee.com

> We start Xindice by using Runtime.exec() and sending the Xindice command
> with config parameters for the JVM and for Xindice. We detect whether
> it's running by waiting for an output message of "Server Running" (there
> is probably a better way to do this, but I don't know what it is!).

> The code is below - maybe it will help you...
> Cheers,
> Catie

> /**
>    * Runs the system command to start the DBServer
>    * @todo: make bulletproof, handle errors & exceptions, etc
>    */
>   protected void startCommand()
>   {

>     File execDir = new File( XindiceUtil.getInstance().getXindiceHome()
> );
>     try
>     {
>       _serverProcess = Runtime.getRuntime().exec(START_COMMAND, ENV,
> execDir);
>       InputStream inputStream = _serverProcess.getInputStream();
>       BufferedReader reader = new BufferedReader ( new
> InputStreamReader(inputStream) );

>       InputStream error = _serverProcess.getErrorStream();
>       BufferedReader errRead = new BufferedReader ( new
> InputStreamReader(error) );

>       try
>       {
>         String line = null;
>         boolean serverStarted = false;
>         while ( !serverStarted )
>         {
>             while (errRead.ready())
>             {
>               line = errRead.readLine();
>               System.out.println(line);
>               System.out.println("ERROR LINE IS: " + line);
>             }
>             while(reader.ready())
>             {
>               line = reader.readLine();
>               System.out.println("LINE IS: " + line);
>             }
>             if (line != null && line.indexOf("Server Running") != -1 )
>             {
>                serverStarted = true;
>                setStarted(true);
>             }
>         }
>       }
>       catch (IOException e)
>       {
>         e.printStackTrace();
>         throw new RuntimeException(e.getMessage());
>       }
>     }
>     catch (Exception e)
>     {
>       e.printStackTrace();
>       throw new RuntimeException();
>     }
>   }



RE: Using Xindice inside own code

Posted by Kanarinka <ka...@ikatun.com>.
Hi Pavel,

We start Xindice by using Runtime.exec() and sending the Xindice command
with config parameters for the JVM and for Xindice. We detect whether
it's running by waiting for an output message of "Server Running" (there
is probably a better way to do this, but I don't know what it is!).

The code is below - maybe it will help you...
Cheers,
Catie

/**
   * Runs the system command to start the DBServer
   * @todo: make bulletproof, handle errors & exceptions, etc
   */
  protected void startCommand()
  {

    File execDir = new File( XindiceUtil.getInstance().getXindiceHome()
);
    try
    {
      _serverProcess = Runtime.getRuntime().exec(START_COMMAND, ENV,
execDir);
      InputStream inputStream = _serverProcess.getInputStream();
      BufferedReader reader = new BufferedReader ( new
InputStreamReader(inputStream) );

      InputStream error = _serverProcess.getErrorStream();
      BufferedReader errRead = new BufferedReader ( new
InputStreamReader(error) );

      try
      {
        String line = null;
        boolean serverStarted = false;
        while ( !serverStarted )
        {
            while (errRead.ready())
            {
              line = errRead.readLine();
              System.out.println(line);
              System.out.println("ERROR LINE IS: " + line);
            }
            while(reader.ready())
            {
              line = reader.readLine();
              System.out.println("LINE IS: " + line);
            }
            if (line != null && line.indexOf("Server Running") != -1 )
            {
               serverStarted = true;
               setStarted(true);
            }
        }
      }
      catch (IOException e)
      {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new RuntimeException();
    }
  }