CountDownLatchとシャットダウンフックの例

S2JMS-Serverにあった。

org.seasar.jms.server.Main

    /**
     * S2JMS-Serverプロセスを開始し、シャットダウンまで待機します。
     * 
     * @param args
     *            コマンドライン引数
     * @throws Exception
     *             S2JMS-Serverプロセスの初期化中に例外が発生した場合にスローされます
     */
    protected void run(final String[] args) throws Exception {
        final String dicon = getDicon(args);
        final String classpathArg = getClasspath(args);
        setupClasspath(classpathArg.split(File.pathSeparator));
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                destoryS2Container();
                latch.countDown();
            }
        });
        s2container = createS2Container(dicon);
        logger.log(Level.INFO, "IJMS-SERVER3001");

        try {
            latch.await();
        } catch (final InterruptedException ignore) {
            destoryS2Container();
        }
    }

参考:
2006-01-14 - 日記