com.aalmeida.lightning.LightningDaemon.java Source code

Java tutorial

Introduction

Here is the source code for com.aalmeida.lightning.LightningDaemon.java

Source

/*
 * Copyright (c) 2015 Alexandre Almeida.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aalmeida.lightning;

import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.daemon.DaemonInitException;
import org.apache.log4j.Logger;

public class LightningDaemon implements Daemon {
    private static Logger log = Config.getCoreLogger();
    private Thread thread;
    private boolean stopped = false;

    /* (non-Javadoc)
     * @see org.apache.commons.daemon.Daemon#init(org.apache.commons.daemon.DaemonContext)
     */
    public void init(DaemonContext pArg0) throws DaemonInitException, Exception {
        this.thread = new Thread() {
            public synchronized void start() {
                super.start();
                try {
                    LightningDaemon.log.info("Start -- 1.5.0 - 20140902-2353");
                } catch (Throwable e) {
                    LightningDaemon.log.fatal("Something went wrong", e);
                }
            }

            public void run() {
                while (!LightningDaemon.this.stopped) {
                }
            }
        };
    }

    /* (non-Javadoc)
     * @see org.apache.commons.daemon.Daemon#start()
     */
    public void start() throws Exception {
        this.thread.start();
    }

    /* (non-Javadoc)
     * @see org.apache.commons.daemon.Daemon#stop()
     */
    public void stop() throws Exception {
        this.stopped = true;
        try {
            this.thread.join(1000L);
        } catch (InterruptedException e) {
            System.err.println(e.getMessage());
            throw e;
        }
    }

    /* (non-Javadoc)
     * @see org.apache.commons.daemon.Daemon#destroy()
     */
    public void destroy() {
        this.thread = null;
    }
}