0

I have written a small program to start to Hive Server. Command to start to Hive Server is in shell file. When I call the shell file to start Hive Server it tends to start it and get Hang. Is there any problem in program?

Code:

            try
        {
            String cmd = "/home/hadoop/sqoop-1.3.0-cdh3u1/bin/StartServer.sh"; // this is the command to execute in the Unix shell

            // create a process for the shell
            ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
            pb.redirectErrorStream(true); // use this to capture messages sent to stderr
            Process shell = pb.start();
            InputStream shellIn = shell.getInputStream(); // this captures the output from the command
            // wait for the shell to finish and get the return code
            // at this point you can process the output issued by the command

            // for instance, this reads the output and writes it to System.out:
            int c;
            while ((c = shellIn.read()) != -1) 
            {
                System.out.write(c);
            }

            // close the stream
            shellIn.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            e.printStackTrace(pw);
            pw.flush();
            System.exit(1);
        }

Please let me know this. Is something I missed in the program?

Thanks.

6
  • Why are you reading from the shell's input? Shouldn't you be reading its output? Commented May 3, 2012 at 10:41
  • you are starting a server which wont exit.. Therefore your program will loop inside the while loop.. If you just want to start the server use this approach :java-samples.com/showtutorial.php?tutorialid=8 Commented May 3, 2012 at 10:42
  • @PaulTomblin: I am reading because I want to print all the process on the console. That was not necessary. Nut due to reading does it creating the problem??? Commented May 3, 2012 at 10:43
  • 1
    alternatively if you really want control over the process do this entire thing in a seperate thread so that your thread will block and not your entire program Commented May 3, 2012 at 10:44
  • you can see the output from the server that you want to see right? Commented May 3, 2012 at 10:44

2 Answers 2

2

It looks like your program is doing what you've told it to do.

The first few lines should indeed start the Hive server. The lines after that, read from the standard output of the server process, and echo each character to your Java process' console. Your Java process sits in a loop (making blocking I/O calls) for as long as the Hive server's output stream exists.

That is, your Java process will sit in a loop, echoing output, for as long as the Hive server is running.

Is this what you want it to do? If so, then it obviously won't be able to exit. If not, then there's no reason for you to read from the server's input stream at all, and your Java program can exit after it has started the server process. Alternatively, if you want to listen for output and do other things in your Java process, you'll need to use multiple threads in order to do two things at once.

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I can see you are starting server, i.e. application that starts and does not terminates soon. It remains running. This means that the STDOUT of this applcation (the server) is not closed (unless you are killing the server).

Method shellIn.read() is blocking. It reads the next byte from input stream and returns when the byte is read or when stream is closed.

So, in your case the stream is never closed, therefore your program got stuck: it is waiting forever for the input (and probably reading it).

To solve this problem you need separate thread: either in java or in OS. You can run your server from separate java thread or compose command line to run server in background (for example using trailing &).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.