<commit_before>package de.ahus1.hystrix.util;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.commons.configuration.AbstractConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicBooleanProperty;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.jmx.BaseConfigMBean;
import com.netflix.config.jmx.ConfigMBean;
import com.netflix.hystrix.Hystrix;
import com.netflix.hystrix.strategy.HystrixPlugins;

/**
 * Setup and teardown of Hystrix configuration at start/end of application
 * lifecycle.
 * 
 * @author <NAME> (msg systems ag) 2014
 * @version 3.3
 */
@WebListener
public class HystrixSetupListener implements ServletContextListener {

    private static Logger LOG = LoggerFactory
            .getLogger(ZabbixCommandMetricsProvider.class);

    /** for register and un-register. */
    static final String OBJ_NAME_LOG4J_BEAN =
            "de.ahus1.archaius:type=ArchaiusMBean";

    /** Object name for JMX binding. */
    private ObjectName name;

    private final static DynamicBooleanProperty enablezabbix =
            DynamicPropertyFactory.getInstance().getBooleanProperty(
                    "hystrixdemo.enablezabbix", false);

    private HystrixZabbixMetricsPublisher zabbix;

    private final static DynamicBooleanProperty enableriemann =
            DynamicPropertyFactory.getInstance().getBooleanProperty(
                    "hystrixdemo.enableriemann", false);

    private HystrixRiemannEventNotifier riemann;

    /**
     * Setup Servo and other Hystrix elements, and JMX Registration of Archaius.
     * 
     * @param sce
     *            servlet context event
     */
    public void contextInitialized(ServletContextEvent sce) {
        AbstractConfiguration conf = ConfigurationManager.getConfigInstance();

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

        // register Archaius as MBean to allow runtime configuration.
        try {
            name =
                    new ObjectName(OBJ_NAME_LOG4J_BEAN
                            + sce.getServletContext().getContextPath());
            BaseConfigMBean bean = new BaseConfigMBean(conf);
            StandardMBean mbean = new StandardMBean(bean, ConfigMBean.class);
            mbs.registerMBean(mbean, name);
        } catch (InstanceAlreadyExistsException | MalformedObjectNameException
                | NotCompliantMBeanException | MBeanRegistrationException e) {
            throw new RuntimeException(e);
        }

        // expose all Hystrix methods as JMX Beans
        // HystrixPlugins.getInstance().registerMetricsPublisher(
        // HystrixServoMetricsPublisher.getInstance());

        setupZabbix();

        setupRiemann();

    }

    private void setupRiemann() {
        riemann = new HystrixRiemannEventNotifier();
        HystrixPlugins.getInstance().registerEventNotifier(riemann);

        enableriemann.addCallback(new Runnable() {
            @Override
            public void run() {
                try {
                    if (enableriemann.getValue()) {
                        LOG.info("starting riemann");
                        riemann.start();
                    } else {
                        LOG.info("stopping riemann");
                        riemann.stop();
                    }
                } catch (Exception e) {
                    LOG.warn("exception", e);
                }
            }
        });

        if (enableriemann.getValue() == true) {
            try {
                riemann.start();
            } catch (IOException e) {
                /*
                 * TODO: find a way for lazy registration as soon as Riemann is
                 * up and running
                 */
                LOG.error("unable to register Riemann listener", e);
            }
        }
    }

    private void setupZabbix() {
        try {
            zabbix = new HystrixZabbixMetricsPublisher();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        enablezabbix.addCallback(new Runnable() {
            @Override
            public void run() {
                try {
                    if (enablezabbix.getValue()) {
                        LOG.info("starting zabbix");
                        zabbix.start();
                    } else {
                        LOG.info("stopping zabbix");
                        zabbix.stop();
                    }
                } catch (Exception e) {<commit_msg>ensure that zabbix publisher is registered.<commit_after>package de.ahus1.hystrix.util;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.commons.configuration.AbstractConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicBooleanProperty;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.jmx.BaseConfigMBean;
import com.netflix.config.jmx.ConfigMBean;
import com.netflix.hystrix.Hystrix;
import com.netflix.hystrix.strategy.HystrixPlugins;

/**
 * Setup and teardown of Hystrix configuration at start/end of application
 * lifecycle.
 * 
 * @author <NAME> (msg systems ag) 2014
 * @version 3.3
 */
@WebListener
public class HystrixSetupListener implements ServletContextListener {

    private static Logger LOG = LoggerFactory
            .getLogger(ZabbixCommandMetricsProvider.class);

    /** for register and un-register. */
    static final String OBJ_NAME_LOG4J_BEAN =
            "de.ahus1.archaius:type=ArchaiusMBean";

    /** Object name for JMX binding. */
    private ObjectName name;

    private final static DynamicBooleanProperty enablezabbix =
            DynamicPropertyFactory.getInstance().getBooleanProperty(
                    "hystrixdemo.enablezabbix", false);

    private HystrixZabbixMetricsPublisher zabbix;

    private final static DynamicBooleanProperty enableriemann =
            DynamicPropertyFactory.getInstance().getBooleanProperty(
                    "hystrixdemo.enableriemann", false);

    private HystrixRiemannEventNotifier riemann;

    /**
     * Setup Servo and other Hystrix elements, and JMX Registration of Archaius.
     * 
     * @param sce
     *            servlet context event
     */
    public void contextInitialized(ServletContextEvent sce) {
        AbstractConfiguration conf = ConfigurationManager.getConfigInstance();

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

        // register Archaius as MBean to allow runtime configuration.
        try {
            name =
                    new ObjectName(OBJ_NAME_LOG4J_BEAN
                            + sce.getServletContext().getContextPath());
            BaseConfigMBean bean = new BaseConfigMBean(conf);
            StandardMBean mbean = new StandardMBean(bean, ConfigMBean.class);
            mbs.registerMBean(mbean, name);
        } catch (InstanceAlreadyExistsException | MalformedObjectNameException
                | NotCompliantMBeanException | MBeanRegistrationException e) {
            throw new RuntimeException(e);
        }

        // expose all Hystrix methods as JMX Beans
        // HystrixPlugins.getInstance().registerMetricsPublisher(
        // HystrixServoMetricsPublisher.getInstance());

        setupZabbix();

        setupRiemann();

    }

    private void setupRiemann() {
        riemann = new HystrixRiemannEventNotifier();
        HystrixPlugins.getInstance().registerEventNotifier(riemann);

        enableriemann.addCallback(new Runnable() {
            @Override
            public void run() {
                try {
                    if (enableriemann.getValue()) {
                        LOG.info("starting riemann");
                        riemann.start();
                    } else {
                        LOG.info("stopping riemann");
                        riemann.stop();
                    }
                } catch (Exception e) {
                    LOG.warn("exception", e);
                }
            }
        });

        if (enableriemann.getValue() == true) {
            try {
                riemann.start();
            } catch (IOException e) {
                /*
                 * TODO: find a way for lazy registration as soon as Riemann is
                 * up and running
                 */
                LOG.error("unable to register Riemann listener", e);
            }
        }
    }

    private void setupZabbix() {
        try {
            zabbix = new HystrixZabbixMetricsPublisher();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        HystrixPlugins.getInstance().registerMetricsPublisher(zabbix);

        enablezabbix.addCallback(new Runnable() {
            @Override
            public void run() {
                try {
                    if (enablezabbix.getValue()) {
                        LOG.info("starting zabbix");
                        zabbix.start();
                    } else {
                        LOG.info("stopping zabbix");
                        zabbix.stop();
                    }
                } catch (Exception e) {
{'fraction_non_alphanumeric': 0.08148637178157125, 'fraction_numerical': 0.0022635103272658683, 'mean_word_length': 2.2191863995142684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280428', 'n_tokens_mistral': 2812, 'n_tokens_neox': 2619, 'n_words': 572}