Package org.apache.falcon.listener

Source Code of org.apache.falcon.listener.HadoopStartupListener

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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 org.apache.falcon.listener;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.hdfs.server.namenode.NameNode;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.JobTracker;
import org.apache.hadoop.mapred.TaskTracker;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;

/**
* Listener for bootstrapping embedded hadoop cluster for integration tests.
*/
public class HadoopStartupListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            FileUtils.deleteDirectory(new File(System.getProperty("hadoop.tmp.dir")));
            final Configuration conf = new Configuration();

            NameNode.format(conf);
            final String[] emptyArgs = {};
            NameNode.createNameNode(emptyArgs, conf);
            DataNode.createDataNode(emptyArgs, conf);
            final JobTracker jobTracker = JobTracker.startTracker(new JobConf(conf));
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        jobTracker.offerService();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        TaskTracker taskTracker = new TaskTracker(new JobConf(conf));
                        taskTracker.run();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Unable to start hadoop cluster", e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}
TOP

Related Classes of org.apache.falcon.listener.HadoopStartupListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.