com.bodybuilding.turbine.web.AppInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.bodybuilding.turbine.web.AppInitializer.java

Source

/*
 * Copyright (C) 2015 Bodybuilding.com
 *
 * 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.bodybuilding.turbine.web;

import com.bodybuilding.turbine.servlet.ClusterListServlet;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.turbine.init.TurbineInit;
import com.netflix.turbine.monitor.cluster.ClusterMonitorFactory;
import com.netflix.turbine.plugins.DefaultAggregatorFactory;
import com.netflix.turbine.plugins.PluginsFactory;
import com.netflix.turbine.streaming.servlet.TurbineStreamServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * Initializes Turbine, Hystris Dashboard, and ClusterListServlet
 */
@EnableAutoConfiguration
@Configuration
@EnableHystrixDashboard
@SpringBootApplication
public class AppInitializer extends SpringBootServletInitializer implements WebApplicationInitializer {
    private static final Logger log = LoggerFactory.getLogger(AppInitializer.class);

    public static void main(String[] args) {
        SpringApplication.run(AppInitializer.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(AppInitializer.class);
    }

    @Bean
    public ServletRegistrationBean hystrixStreamServlet() {
        return new ServletRegistrationBean(new TurbineStreamServlet(), "/turbine.stream");
    }

    @Bean
    public ServletRegistrationBean clusterListServlet() {
        return new ServletRegistrationBean(new ClusterListServlet(), "/clusterlist");
    }

    @Bean
    public DashboardUrlInitializer dashboardUrlInitializer() {
        return new DashboardUrlInitializer();
    }

    @PostConstruct
    public void init() {
        log.info("Initing Turbine server");
        ClusterMonitorFactory clusterMonitorFactory = createClusterMonitorFactory();
        log.info("Using ClusterMonitorFactory impl: {}", clusterMonitorFactory.getClass());
        PluginsFactory.setClusterMonitorFactory(clusterMonitorFactory);
        TurbineInit.init();
    }

    @PreDestroy
    public void stop() {
        log.info("Stopping Turbine server");
        TurbineInit.stop();
    }

    public static class DashboardUrlInitializer implements ServletContextInitializer {

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.setInitParameter("hystrix.dashboard.url", "/hystrix/monitor?stream=");
        }
    }

    public static ClusterMonitorFactory createClusterMonitorFactory() {
        ClusterMonitorFactory instance = null;
        String className = DynamicPropertyFactory.getInstance().getStringProperty("ClusterMonitorFactory.impl",
                "com.netflix.turbine.plugins.DefaultAggregatorFactory").get();
        try {
            Class clazz = Class.forName(className);
            instance = (ClusterMonitorFactory) clazz.newInstance();
        } catch (Exception e) {
            log.error("Could not load ClusterMonitorFactory impl class {} falling back to DefaultAggregatorFactory",
                    className, e);
            instance = new DefaultAggregatorFactory();
        }

        return instance;
    }
}