Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.advantageous.qbit.admin;
  
 
 import java.util.List;
This is a utility class for when you are running in a PaaS like Heroku or Docker. It also allows you to share stat, health and system manager setup. If statsD is enabled then this will look for the statsd port and host from STATSD_PORT and STATSD_HOST environment variables. If you use the admin builder, the port for the admin will be from the environment variable QBIT_ADMIN_PORT. It can be overridden from system properties as well see AdminBuilder for more details. The main end point port will be read from the environment variable WEB_PORT and if not found then read from PORT. Defaults for ports and hosts can be overridden by their respective builders.
 
 public class ManagedServiceBuilder {


    
Holds the system manager used to register proper shutdown with JVM.
 
     private QBitSystemManager systemManager;

    
Holds the stats service builder used to collect system stats.
 
     private StatServiceBuilder statServiceBuilder;

    
EndpointServerBuilder used to hold the main EndpointServerBuilder.
 
     private EndpointServerBuilder endpointServerBuilder;

    
Used to create LocalStatsCollection.
 
Used to create the main http server.
 
     private HttpServerBuilder httpServerBuilder;

    
Enables local stats collection.
 
     private boolean enableLocalStats=true;

    
Enables sending stats to stats D.
 
     private boolean enableStatsD=false;

    
Enables local health stats collection.
 
     private boolean enableLocalHealth=true;

    
Used to create local health collector.
 
     private HealthServiceBuilder healthServiceBuilder;

    
Health service used to collect health info from service queues.
 
     private HealthServiceAsync healthService;

    
Enables the collection of stats.
 
     private boolean enableStats = true;

    
How often stats should be flushed.
 
     private int sampleStatFlushRate = 5;

    
How often timings should be collected defaults to every 100 calls.
 
     private int checkTimingEveryXCalls = 100;

    
Event manager for services, service queues, and end point servers.
 
     private EventManager eventManager;

    
Factory to create QBit parts.
 
     private Factory factory;

    
Builder to hold context information about endpoints.
 
     private ContextMetaBuilder contextMetaBuilder;

    
Endpoint services that will be exposed through contextMetaBuilder.
 
     private List<ObjectendpointServices;

    
The builder for the admin.
 
     private AdminBuilder adminBuilder;
 
 
    public void enableConsulServiceDiscovery(String dataCenter) {
        final ConsulServiceDiscoveryBuilder consulServiceDiscoveryBuilder = ConsulServiceDiscoveryBuilder.consulServiceDiscoveryBuilder();
        consulServiceDiscoveryBuilder.setDatacenter(dataCenter);
         = () -> consulServiceDiscoveryBuilder.build();
    }
    public void enableConsulServiceDiscovery(String dataCenterString host) {
        final ConsulServiceDiscoveryBuilder consulServiceDiscoveryBuilder = ConsulServiceDiscoveryBuilder.consulServiceDiscoveryBuilder();
        consulServiceDiscoveryBuilder.setDatacenter(dataCenter);
        consulServiceDiscoveryBuilder.setConsulHost(host);
         = () -> consulServiceDiscoveryBuilder.build();
    }
    public void enableConsulServiceDiscovery(String dataCenterString hostint port) {
        final ConsulServiceDiscoveryBuilder consulServiceDiscoveryBuilder = ConsulServiceDiscoveryBuilder.consulServiceDiscoveryBuilder();
        consulServiceDiscoveryBuilder.setDatacenter(dataCenter);
        consulServiceDiscoveryBuilder.setConsulHost(host);
        consulServiceDiscoveryBuilder.setConsulPort(port);
         = () -> consulServiceDiscoveryBuilder.build();
    }
        return ;
    }
    public ManagedServiceBuilder setServiceDiscoverySupplier(Supplier<ServiceDiscoveryserviceDiscoverySupplier) {
        this. = serviceDiscoverySupplier;
        return this;
    }
        if ( == null) {
            if (!=null) {
                 = .get();
            }
        }
        return ;
    }
    public ManagedServiceBuilder setServiceDiscovery(ServiceDiscovery serviceDiscovery) {
        this. = serviceDiscovery;
        return this;
    }
        if ( == null) {
             = StatsDReplicatorBuilder.statsDReplicatorBuilder();
            final String statsDPort = System.getenv("STATSD_PORT");
            if (statsDPort!=null && !statsDPort.isEmpty()) {
                .setPort(Integer.parseInt(statsDPort));
            }
            final String statsDHost = System.getenv("STATSD_HOST");
            if (statsDHost!=null && !statsDHost.isEmpty()) {
                .setHost(statsDHost);
            }
        }
        return ;
    }
            final StatsDReplicatorBuilder statsDReplicatorBuilder) {
        this. = statsDReplicatorBuilder;
        return this;
    }
    public AdminBuilder getAdminBuilder() {
        if ( == null) {
             = AdminBuilder.adminBuilder();
            final String qbitAdminPort = System.getenv("QBIT_ADMIN_PORT");
            if (qbitAdminPort!=null && !qbitAdminPort.isEmpty()) {
                .setPort(Integer.parseInt(qbitAdminPort));
            }
        }
        return ;
    }
    public ManagedServiceBuilder setAdminBuilder(AdminBuilder adminBuilder) {
        this. = adminBuilder;
        return this;
    }
        if ( == null) {
             = ContextMetaBuilder.contextMetaBuilder();
        }
        return ;
    }
    public ManagedServiceBuilder setContextMetaBuilder(final ContextMetaBuilder contextMetaBuilder) {
        this. = contextMetaBuilder;
        return this;
    }
    public List<ObjectgetEndpointServices() {
        if ( == null) {
             = new ArrayList<>();
        }
        return ;
    }
    public ManagedServiceBuilder setEndpointServices(final List<ObjectendpointServices) {
        this. = endpointServices;
        return this;
    }
    public ManagedServiceBuilder addEndpointService(final Object endpointService) {
        getContextMetaBuilder().addService(endpointService.getClass());
        getEndpointServices().add(endpointService);
        return this;
    }
    public Factory getFactory() {
        if ( == null) {
             = QBit.factory();
        }
        return ;
    }
    public ManagedServiceBuilder setFactory(final Factory factory) {
        this. = factory;
        return this;
    }
    public EventManager getEventManager() {
        if ( == null) {
             = getFactory().systemEventManager();
        }
        return ;
    }
    public ManagedServiceBuilder setEventManager(EventManager eventManager) {
        this. = eventManager;
        return this;
    }
    public ManagedServiceBuilder setHealthServiceBuilder(final HealthServiceBuilder healthServiceBuilder) {
        this. = healthServiceBuilder;
        return this;
    }
        if ( == null) {
             = HealthServiceBuilder.healthServiceBuilder();
        }
        return ;
    }
    public static ManagedServiceBuilder managedServiceBuilder() {
        return new ManagedServiceBuilder();
    }
        if ( == null) {
             = HttpServerBuilder.httpServerBuilder();
            String port = System.getenv("WEB_PORT");
            if (Str.isEmpty(port)) {
                port = System.getenv("PORT");
            }
            if (!Str.isEmpty(port)) {
                .setPort(Integer.parseInt(port));
            }
        }
        return ;
    }
    public ManagedServiceBuilder setHttpServerBuilder(HttpServerBuilder httpServerBuilder) {
        this. = httpServerBuilder;
        return this;
    }
    public boolean isEnableStatsD() {
        return ;
    }
    public ManagedServiceBuilder setEnableStatsD(boolean enableStatsD) {
        this. = enableStatsD;
        return this;
    }
        if (==null) {
             = LocalStatsCollectorBuilder.localStatsCollectorBuilder();
        }
        return ;
    }
        this. = localStatsCollectorBuilder;
        return this;
    }
    public boolean isEnableLocalStats() {
        return ;
    }
    public ManagedServiceBuilder setEnableLocalStats(boolean enableLocalStats) {
        this. = enableLocalStats;
        return this;
    }
    public boolean isEnableLocalHealth() {
        return ;
    }
    public ManagedServiceBuilder setEnableLocalHealth(boolean enableLocalHealth) {
        this. = enableLocalHealth;
        return this;
    }
        if ( == null) {
             = new QBitSystemManager();
        }
        return ;
    }
    public ManagedServiceBuilder setSystemManager(QBitSystemManager systemManager) {
        this. = systemManager;
        return this;
    }
        if ( == null) {
             =  StatServiceBuilder.statServiceBuilder();
            if () {
            }
            if () {
            }
            .build();
        }
        return ;
    }
    public ManagedServiceBuilder setStatServiceBuilder(StatServiceBuilder statServiceBuilder) {
        this. = statServiceBuilder;
        return this;
    }
        if (==null) {
             = EndpointServerBuilder.endpointServerBuilder();
            if (isEnableStats()) {
            }
            if (isEnableLocalStats()) {
                .setEnableStatEndpoint(true);
            }
            if ( != null) {
                .setServices();
            }
        }
        return ;
    }
        EndpointServerBuilder endpointServerBuilder = EndpointServerBuilder.endpointServerBuilder();
        endpointServerBuilder.setEnableHealthEndpoint(isEnableLocalHealth());
        endpointServerBuilder.setSystemManager(this.getSystemManager());
        endpointServerBuilder.setHealthService(getHealthService());
        endpointServerBuilder.setStatsFlushRateSeconds(getSampleStatFlushRate());
        endpointServerBuilder.setCheckTimingEveryXCalls(getCheckTimingEveryXCalls());
        endpointServerBuilder.setServiceDiscovery(getServiceDiscovery());
        if (isEnableStats()) {
            endpointServerBuilder.setStatsCollector(getStatServiceBuilder().buildStatsCollectorWithAutoFlush());
        }
        if (isEnableLocalStats()) {
                endpointServerBuilder.setEnableStatEndpoint(true);
                endpointServerBuilder.setStatsCollection(getLocalStatsCollectorBuilder().build());
        }
        return endpointServerBuilder;
    }
        ServiceBundleBuilder serviceBundleBuilder = ServiceBundleBuilder.serviceBundleBuilder();
        serviceBundleBuilder.setSystemManager(this.getSystemManager());
        serviceBundleBuilder.setHealthService(getHealthService());
        serviceBundleBuilder.setCheckTimingEveryXCalls(this.getCheckTimingEveryXCalls());
        serviceBundleBuilder.setStatsFlushRateSeconds(this.getSampleStatFlushRate());
        if (isEnableStats()) {
            serviceBundleBuilder.setStatsCollector(getStatServiceBuilder().buildStatsCollector());
        }
        return serviceBundleBuilder;
    }
    public ServiceBuilder createServiceBuilderForServiceObject(final Object serviceObject) {
        ServiceBuilder serviceBuilder = ServiceBuilder.serviceBuilder();
        serviceBuilder.setSystemManager(this.getSystemManager());
        serviceBuilder.setServiceObject(serviceObject);
        final String bindStatHealthName =  AnnotationUtils.readServiceName(serviceObject);
        if (isEnableLocalHealth()) {
            serviceBuilder.registerHealthChecks(getHealthService(), bindStatHealthName);
        }
        if (isEnableStats()) {
            serviceBuilder.registerStatsCollections(bindStatHealthName,
        }
        return serviceBuilder;
    }
    public ManagedServiceBuilder setEndpointServerBuilder(EndpointServerBuilder endpointServerBuilder) {
        this. = endpointServerBuilder;
        return this;
    }
        if ( == null) {
            if ( == null) {
                HealthServiceBuilder builder = getHealthServiceBuilder();
                 = builder.setAutoFlush().buildAndStart();
            }
            return ;
        } else {
            return .buildHealthSystemReporter();
        }
    }
    public ManagedServiceBuilder setHealthService(HealthServiceAsync healthServiceAsync) {
        this. = healthServiceAsync;
        return this;
    }
    public boolean isEnableStats() {
        return ;
    }
    public ManagedServiceBuilder setEnableStats(boolean enableStats) {
        this. = enableStats;
        return this;
    }
    public int getSampleStatFlushRate() {
        return ;
    }
    public ManagedServiceBuilder setSampleStatFlushRate(int sampleStatFlushRate) {
        this. = sampleStatFlushRate;
        return this;
    }
    public int getCheckTimingEveryXCalls() {
        return ;
    }
    public ManagedServiceBuilder setCheckTimingEveryXCalls(int checkTimingEveryXCalls) {
        this. = checkTimingEveryXCalls;
        return this;
    }
New to GrepCode? Check out our FAQ X