JBoss Startup Script

From Shrubbery

Jump to: navigation, search


On Linux and cygwin, you can use a 'system V'-ish startup script. Example shell scripts can be found in the bin directory of the JBoss installation.

See also:

Waiting for the server to start

One thing about these example scripts is that they do not provide any way to wait for the server to start. This little command line Java program will wait for a listener on a TCP socket to start:

package org.yajul.net;

import java.net.Socket;
import java.io.IOException;

public class WaitForSocket
{
    private static final int WAIT = 1000;

    public static void main(String[] args)
    {
        // args[0] - Host name or IP address
        // args[1] - Port number
        // args[2] - Number of seconds to wait before giving up.
        String host = args[0];
        int port = Integer.parseInt(args[1]);
        long timeout = Long.parseLong(args[2]) * 1000;
        long start = System.currentTimeMillis();
        long remaining = timeout;
        boolean connected = false;
        while (remaining > 0 && !connected)
        {
            Socket s = null;
            try
            {
                System.out.println("Attempting to connect...");
                s = new Socket(host, port);
                connected = true;
            }
            catch (IOException e)
            {
                // ignore
            }
            finally
            {
                if (s != null)
                {
                    try
                    {
                        s.close();
                    }
                    catch (IOException ignore)
                    {
                        // ignore
                    }
                }
            }
            long elapsed = System.currentTimeMillis() - start;
            if (connected)
            {
                System.out.println("Connected after waiting " + elapsed + " milliseconds.");
                System.exit(0);
            }

            remaining = timeout - elapsed;
            // Don't wait if we're already out of time.
            if (remaining <= 0)
                break;
            // Don't wait more than the remaining time.
            long wait = remaining > 0 && remaining < WAIT ? remaining : WAIT;
            try
            {
                System.out.println("Failed to connect.  Retrying in " + wait + " ms.");
                Thread.sleep(wait);
            }
            catch (InterruptedException ignore)
            {
                // ignore
            }
            // Recalculate the remaining time after we've waited. 
            remaining = timeout - (System.currentTimeMillis() - start);
        } // while
        System.exit(1); // Non zero return code means it failed.
    }
}

It can be launched from a shell script using:

java -cp ...some classpath... org.yajul.net.WaitForSocket localhost 8080 60

This will wait 60 seconds for the 8080 socket on the local host to start listening. However, this is definitely not the end of the story. The problem here is that JBoss starts listening on the port 'before' deploying anything from the deploy directory. If a client JVM starts up and attempts to invoke EJBs remotely it will not work. The next section covers how to wait for the resources to be deployed.

Waiting for deployment

TODO: document this

Personal tools